自定义 cakephp 控制台日志消息



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

2019-07-10 03:15:31 Error: [ParseError] syntax error, unexpected '}'
#0 /app/app/vendor/composer/ClassLoader.php(322): ComposerAutoloadincludeFile('/app/app/vendor...')
#1 [internal function]: ComposerAutoloadClassLoader->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 中删除引擎参数后,它确实调用了我的函数]。欢迎任何建议,谢谢。

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

这就是我让它工作的方式

// 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("/[rn]+/", " ", '{"'.$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));
    }
}

最新更新