【问题标题】:Custom cakephp ConsoleLog message自定义 cakephp ConsoleLog 消息
【发布时间】:2019-11-19 15:19:18
【问题描述】:

我正在尝试获取自定义控制台日志,以便 而不是

2019-07-10 03:15:31 Error: [ParseError] syntax error, unexpected '}'
#0 /app/app/vendor/composer/ClassLoader.php(322): Composer\Autoload\includeFile('/app/app/vendor...')
#1 [internal function]: Composer\Autoload\ClassLoader->loadClass('App\\Controller\\...')

我想要json格式

{"Error":"[ParseError] syntax error, unexpected '}'"}

目前,我已经创建了一个自定义日志适配器

// Log/Engine/ApplicationLog.php

    class ApplicationLog extends BaseLog
        {
        public $Logs;

        public function __construct($options = [])
        {       parent::__construct($options);

            // ...
        }

        public function log($level, $message, array $context = [])
        {
    //        $level = $this->getConfig('type');
            $this->$message = 'Test'.$message;
        }
        public function error($message, array $context = [])
        {
    //        return static::write(__FUNCTION__, '{"error":"'.$message.'"}', $context);
            return write('TESTESTSTESTSTEST');

        }
    }

配置为

// bootstrap.php
Log::setConfig('application', [
    'className' => 'Application',
    'path' => LOGS,
    'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
    'file' => 'error',
    'engine' => 'console',
]);

我正在尝试覆盖 BaseLog 错误方法以根据我的要求更改消息,但它没有调用我的自定义消息 [在从 setConfig 中删除引擎参数后它确实调用了我的函数]。 欢迎提出任何建议,谢谢。

【问题讨论】:

  • “不起作用”不是正确的问题描述。请始终尽可能具体地说明确切会发生什么,以及您希望确切会发生什么。

标签: cakephp cakephp-3.x cakephp-3.7


【解决方案1】:

您不能同时使用engine 选项和className 选项,前者会覆盖后者。 engine 选项是 CakePHP 1.x/2.x 的遗留物,我不知道为什么它仍然存在。

【讨论】:

  • 谢谢!!我摆脱了引擎,这样它至少开始打印我的测试,但在浏览器中,我有什么想法可以在没有引擎参数的情况下在 consoleLog 中获取它?
【解决方案2】:

这就是我的工作方式

// bootstrap.php
Log::setConfig('application', [
    'className' => 'Application',
    'path' => LOGS,
    'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
    'file' => 'error'
]);

删除了引擎参数,因为它干扰了类函数

// Log/Engine/ApplicationLog.php
class ApplicationLog extends BaseLog
{
    protected $_defaultConfig = [
        'stream' => 'php://stderr',// similar to ConsoleLog
        'levels' => null,
        'scopes' => [],
        'outputAs' => null,
    ];


    protected $_output;

    public function __construct($config = [])
    {       parent::__construct($config);
        $config = $this->_config;
        if ($config['stream'] instanceof ConsoleOutput) {
            $this->_output = $config['stream'];
        } elseif (is_string($config['stream'])) {
            $this->_output = new ConsoleOutput($config['stream']);
        } else {
            throw new InvalidArgumentException('`stream` not a ConsoleOutput nor string');
        }

        if (isset($config['outputAs'])) {
            $this->_output->setOutputAs($config['outputAs']);
        }
    }

    public function log($level, $message, array $context = [])
    {
        $message = preg_replace("/[\r\n]+/", " ", '{"'.$level.'":"'.$message.'"}');
        $message = $this->_format($message, $context);

       //$output = date('Y-m-d H:i:s') . ' ' . ucfirst($level) . ': ' . $message;
       //return (bool)$this->_output->write(sprintf('<%s>%s</%s>', $level, $output, $level));

        return (bool)$this->_output->write(sprintf('<%s>%s</%s>', $level, $message, $level));

    }
}

【讨论】:

    猜你喜欢
    • 2011-04-29
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多