add log callback feature

This commit is contained in:
sunxyw 2022-05-13 19:43:52 +08:00
parent 3e10a5b758
commit 9bfa140de1
No known key found for this signature in database
GPG Key ID: CEA01A083E98C578
2 changed files with 32 additions and 9 deletions

View File

@ -57,6 +57,7 @@
"[ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/cghooks add" "[ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/cghooks add"
], ],
"analyse": "phpstan analyse --memory-limit 300M", "analyse": "phpstan analyse --memory-limit 300M",
"cs-fix": "php-cs-fixer fix" "cs-fix": "php-cs-fixer fix",
"test": "phpunit --no-coverage"
} }
} }

View File

@ -72,6 +72,13 @@ class ConsoleLogger extends AbstractLogger
*/ */
protected $static_context = []; protected $static_context = [];
/**
* 日志记录回调
*
* @var callable[]
*/
protected $log_callbacks = [];
/** /**
* 创建一个 ConsoleLogger 实例 * 创建一个 ConsoleLogger 实例
* *
@ -100,6 +107,22 @@ class ConsoleLogger extends AbstractLogger
return self::VERSION; 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 self::$format
); );
$output = $this->interpolate($output, array_merge($this->static_context, $context)); $output = $this->interpolate($output, array_merge($this->static_context, $context));
echo $this->colorize($output, $level) . "\n";
}
/** foreach ($this->log_callbacks as $callback) {
* 添加静态上下文 if ($callback($level, $output, $message, $context) === false) {
*/ return;
public function addStaticContext(array $context): void }
{ }
$this->static_context = array_merge($this->static_context, $context);
echo $this->colorize($output, $level) . "\n";
} }
/** /**