自定义 cakephp 控制台日志消息
Custom cakephp ConsoleLog message
我正在尝试获取自定义控制台日志,以便
而不是
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 中删除引擎参数后它调用了我的函数]。
欢迎提出任何建议,谢谢。
不能同时使用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'
]);
删除了引擎参数,因为它干扰了 class 功能
// 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));
}
}
我正在尝试获取自定义控制台日志,以便 而不是
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 中删除引擎参数后它调用了我的函数]。 欢迎提出任何建议,谢谢。
不能同时使用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'
]);
删除了引擎参数,因为它干扰了 class 功能
// 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));
}
}