我正在尝试获取自定义控制台日志,以便 代替
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中删除引擎参数后,它确实调用了我的函数]。 欢迎提出任何建议,谢谢。
答案 0 :(得分:1)
您不能同时使用engine
选项和className
选项,前者将覆盖后者。 engine
选项来自CakePHP 1.x / 2.x,我不知道为什么它仍然存在。
答案 1 :(得分:0)
这就是我的工作方式
// 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));
}
}