add static context support

This commit is contained in:
sunxyw 2022-05-13 18:33:57 +08:00
parent bfa816f734
commit 5423de6df5
No known key found for this signature in database
GPG Key ID: CEA01A083E98C578

View File

@ -65,6 +65,13 @@ class ConsoleLogger extends AbstractLogger
*/ */
protected static $log_level; protected static $log_level;
/**
* 静态上下文
*
* @var array
*/
protected $static_context = [];
/** /**
* 创建一个 ConsoleLogger 实例 * 创建一个 ConsoleLogger 实例
* *
@ -147,10 +154,18 @@ class ConsoleLogger extends AbstractLogger
[date(self::$date_format), strtoupper(substr(self::$levels[$level], 0, 4)), $message], [date(self::$date_format), strtoupper(substr(self::$levels[$level], 0, 4)), $message],
self::$format self::$format
); );
$output = $this->interpolate($output, $context); $output = $this->interpolate($output, array_merge($this->static_context, $context));
echo $this->colorize($output, $level) . "\n"; echo $this->colorize($output, $level) . "\n";
} }
/**
* 添加静态上下文
*/
public function addStaticContext(array $context): void
{
$this->static_context = array_merge($this->static_context, $context);
}
/** /**
* 转换日志等级 * 转换日志等级
*/ */