From 5423de6df5e9ebac9574ec7305458906dd0580b8 Mon Sep 17 00:00:00 2001 From: sunxyw Date: Fri, 13 May 2022 18:33:57 +0800 Subject: [PATCH] add static context support --- src/ZM/Logger/ConsoleLogger.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/ZM/Logger/ConsoleLogger.php b/src/ZM/Logger/ConsoleLogger.php index ae2eb7b..69732c5 100644 --- a/src/ZM/Logger/ConsoleLogger.php +++ b/src/ZM/Logger/ConsoleLogger.php @@ -65,6 +65,13 @@ class ConsoleLogger extends AbstractLogger */ protected static $log_level; + /** + * 静态上下文 + * + * @var array + */ + protected $static_context = []; + /** * 创建一个 ConsoleLogger 实例 * @@ -147,10 +154,18 @@ class ConsoleLogger extends AbstractLogger [date(self::$date_format), strtoupper(substr(self::$levels[$level], 0, 4)), $message], self::$format ); - $output = $this->interpolate($output, $context); + $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); + } + /** * 转换日志等级 */