mirror of
https://github.com/zhamao-robot/zhamao-logger.git
synced 2026-03-17 20:44:52 +08:00
add log callback feature
This commit is contained in:
parent
3e10a5b758
commit
9bfa140de1
@ -57,6 +57,7 @@
|
||||
"[ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/cghooks add"
|
||||
],
|
||||
"analyse": "phpstan analyse --memory-limit 300M",
|
||||
"cs-fix": "php-cs-fixer fix"
|
||||
"cs-fix": "php-cs-fixer fix",
|
||||
"test": "phpunit --no-coverage"
|
||||
}
|
||||
}
|
||||
|
||||
@ -72,6 +72,13 @@ class ConsoleLogger extends AbstractLogger
|
||||
*/
|
||||
protected $static_context = [];
|
||||
|
||||
/**
|
||||
* 日志记录回调
|
||||
*
|
||||
* @var callable[]
|
||||
*/
|
||||
protected $log_callbacks = [];
|
||||
|
||||
/**
|
||||
* 创建一个 ConsoleLogger 实例
|
||||
*
|
||||
@ -100,6 +107,22 @@ class ConsoleLogger extends AbstractLogger
|
||||
return self::VERSION;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加静态上下文
|
||||
*/
|
||||
public function addStaticContext(array $context): void
|
||||
{
|
||||
$this->static_context = array_merge($this->static_context, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加日志记录回调
|
||||
*/
|
||||
public function addLogCallback(callable $callback): void
|
||||
{
|
||||
$this->log_callbacks[] = $callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印执行栈
|
||||
*/
|
||||
@ -155,15 +178,14 @@ class ConsoleLogger extends AbstractLogger
|
||||
self::$format
|
||||
);
|
||||
$output = $this->interpolate($output, array_merge($this->static_context, $context));
|
||||
echo $this->colorize($output, $level) . "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加静态上下文
|
||||
*/
|
||||
public function addStaticContext(array $context): void
|
||||
{
|
||||
$this->static_context = array_merge($this->static_context, $context);
|
||||
foreach ($this->log_callbacks as $callback) {
|
||||
if ($callback($level, $output, $message, $context) === false) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
echo $this->colorize($output, $level) . "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user