From 9da98fde5a681eac9ec72bd48aaf6925bcf10368 Mon Sep 17 00:00:00 2001 From: sunxyw Date: Wed, 21 Dec 2022 20:58:26 +0800 Subject: [PATCH 1/2] add caller to log format --- src/ZM/Logger/ConsoleLogger.php | 53 ++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/src/ZM/Logger/ConsoleLogger.php b/src/ZM/Logger/ConsoleLogger.php index c59fb0c..2141685 100644 --- a/src/ZM/Logger/ConsoleLogger.php +++ b/src/ZM/Logger/ConsoleLogger.php @@ -17,7 +17,7 @@ class ConsoleLogger extends AbstractLogger * * @var string */ - public static $format = '[%date%] [%level%] %body%'; + public static $format = '[%date%] [%level%] %caller% %body%'; /** * 日志输出日期格式 @@ -203,6 +203,27 @@ public function colorize($string, int $level): string return ConsoleColor::apply($styles, $string)->__toString(); } + /** + * 插入字段到指定字段之前,或之后 + * @param string $field 字段名 + * @param string $alter 要插入的字段 + * @param string $position 插入位置,before 或 after + */ + public function alterFormat(string $field, string $alter, string $position = 'before'): void + { + $format = self::$format; + $pos = strpos($format, $field); + if ($pos === false) { + return; + } + if ($position === 'before') { + $format = substr_replace($format, $alter, $pos, 0); + } else { + $format = substr_replace($format, $alter, $pos + strlen($field), 0); + } + self::$format = $format; + } + /** * {@inheritDoc} */ @@ -215,8 +236,13 @@ public function log($level, $message, array $context = []): void } $output = str_replace( - ['%date%', '%level%', '%body%'], - [date(self::$date_format), strtoupper(substr(self::$levels[$level], 0, 4)), $message], + ['%date%', '%level%', '%body%', '%caller%'], + [ + date(self::$date_format), + strtoupper(substr(self::$levels[$level], 0, 4)), + $message, + $this->getCaller(), + ], self::$format ); $output = $this->interpolate($output, array_merge($this->static_context, $context)); @@ -230,7 +256,7 @@ public function log($level, $message, array $context = []): void if ($this->decorated) { $output = $this->colorize($output, $level) . PHP_EOL; } else { - $output = $output . PHP_EOL; + $output .= PHP_EOL; } // use stream if ($this->stream) { @@ -313,4 +339,23 @@ private function interpolate(string $message, array $context = []): string return strtr($message, $replace); } + + /** + * 获取调用者信息 + */ + private function getCaller(): string + { + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); + $caller = $trace[1] ?? []; + $file = $caller['file'] ?? ''; + if ($file) { + // 截取路径,获取 src 之后至多两级目录 + $path = substr($file, strpos($file, 'src') + 4); + $path = explode(DIRECTORY_SEPARATOR, $path); + $path = array_slice($path, 0, 2); + } else { + $path = ['unknown']; + } + return implode(' > ', $path); + } } From 0ee6e43f066aceccb297484f0c431d26cc897399 Mon Sep 17 00:00:00 2001 From: sunxyw Date: Wed, 21 Dec 2022 21:09:53 +0800 Subject: [PATCH 2/2] change method sign to static --- src/ZM/Logger/ConsoleLogger.php | 42 ++++++++++++++++----------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/ZM/Logger/ConsoleLogger.php b/src/ZM/Logger/ConsoleLogger.php index 2141685..8f11cc9 100644 --- a/src/ZM/Logger/ConsoleLogger.php +++ b/src/ZM/Logger/ConsoleLogger.php @@ -139,6 +139,27 @@ public static function getVersion(): string return self::VERSION; } + /** + * 插入字段到指定字段之前,或之后 + * @param string $field 字段名 + * @param string $alter 要插入的字段 + * @param string $position 插入位置,before 或 after + */ + public static function alterFormat(string $field, string $alter, string $position = 'before'): void + { + $format = self::$format; + $pos = strpos($format, $field); + if ($pos === false) { + return; + } + if ($position === 'before') { + $format = substr_replace($format, $alter, $pos, 0); + } else { + $format = substr_replace($format, $alter, $pos + strlen($field), 0); + } + self::$format = $format; + } + /** * 添加静态上下文 */ @@ -203,27 +224,6 @@ public function colorize($string, int $level): string return ConsoleColor::apply($styles, $string)->__toString(); } - /** - * 插入字段到指定字段之前,或之后 - * @param string $field 字段名 - * @param string $alter 要插入的字段 - * @param string $position 插入位置,before 或 after - */ - public function alterFormat(string $field, string $alter, string $position = 'before'): void - { - $format = self::$format; - $pos = strpos($format, $field); - if ($pos === false) { - return; - } - if ($position === 'before') { - $format = substr_replace($format, $alter, $pos, 0); - } else { - $format = substr_replace($format, $alter, $pos + strlen($field), 0); - } - self::$format = $format; - } - /** * {@inheritDoc} */