2 Commits

Author SHA1 Message Date
sunxyw
0ee6e43f06 change method sign to static 2022-12-21 21:09:53 +08:00
sunxyw
9da98fde5a add caller to log format 2022-12-21 20:58:26 +08:00
3 changed files with 89 additions and 85 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "zhamao/logger",
"description": "Another Colorful Console Logger for CLI Applications",
"description": "Another Console Logger for CLI Applications",
"type": "library",
"license": "Apache-2.0",
"autoload": {
@@ -16,7 +16,7 @@
"authors": [
{
"name": "jerry",
"email": "github@cwcc.me"
"email": "admin@zhamao.me"
},
{
"name": "sunxyw",
@@ -25,7 +25,7 @@
],
"minimum-stability": "stable",
"require": {
"php": "^7.2 || ^7.3 || ^7.4 || ^8.0 || ^8.1 || ^8.2 || ^8.3 || ^8.4 || ^8.5",
"php": "^7.2 || ^7.3 || ^7.4 || ^8.0 || ^8.1",
"psr/log": "^1 || ^2 || ^3",
"symfony/polyfill-mbstring": "^1.0"
},
@@ -37,9 +37,9 @@
"ext-mbstring": "Use C/C++ extension instead of polyfill will be more efficient"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.64",
"phpstan/phpstan": "^1.12",
"phpunit/phpunit": "^9.0",
"friendsofphp/php-cs-fixer": "^3.2",
"phpstan/phpstan": "^1.1",
"phpunit/phpunit": "^8.5 || ^9.0",
"roave/security-advisories": "dev-latest",
"brainmaestro/composer-git-hooks": "^2.8"
},

View File

@@ -10,14 +10,14 @@ use Psr\Log\LogLevel;
class ConsoleLogger extends AbstractLogger
{
public const VERSION = '1.1.9';
public const VERSION = '1.1.0';
/**
* 日志输出格式
*
* @var string
*/
public static $format = '[%date%] [%level%] %body%';
public static $format = '[%date%] [%level%] %caller% %body%';
/**
* 日志输出日期格式
@@ -86,13 +86,6 @@ class ConsoleLogger extends AbstractLogger
*/
protected $stream;
/**
* 递归保护标志 — 防止日志写入失败时错误处理器再次调用日志导致死循环 CPU 100%
*
* @var bool
*/
protected static $in_log = false;
/**
* 是否带颜色
*
@@ -100,15 +93,13 @@ class ConsoleLogger extends AbstractLogger
*/
protected $decorated;
protected $use_stderr = false;
/**
* 创建一个 ConsoleLogger 实例
*
* @param string $level 日志等级
* @param null|resource $stream
*/
public function __construct(string $level = LogLevel::INFO, $stream = null, bool $decorated = true, bool $use_stderr = false)
public function __construct(string $level = LogLevel::INFO, $stream = null, bool $decorated = true)
{
$this->decorated = $decorated;
self::$log_level = $this->castLogLevel($level);
@@ -122,18 +113,12 @@ class ConsoleLogger extends AbstractLogger
if (($stat['mode'] & 0170000) === 0100000) { // whether is regular file
$this->decorated = false;
} else {
$this->decorated
= PHP_OS_FAMILY !== 'Windows' // linux or unix
$this->decorated =
PHP_OS_FAMILY !== 'Windows' // linux or unix
&& function_exists('posix_isatty')
&& posix_isatty($stream); // whether is interactive terminal
}
$this->stream = $stream;
$this->use_stderr = $use_stderr;
}
public function setLevel(string $level): void
{
self::$log_level = $this->castLogLevel($level);
}
/**
@@ -154,6 +139,27 @@ class ConsoleLogger extends AbstractLogger
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;
}
/**
* 添加静态上下文
*/
@@ -177,7 +183,7 @@ class ConsoleLogger extends AbstractLogger
{
$log = 'Stack trace:' . PHP_EOL;
$trace = debug_backtrace();
// array_shift($trace);
//array_shift($trace);
foreach ($trace as $i => $t) {
if (!isset($t['file'])) {
$t['file'] = 'unknown';
@@ -186,7 +192,6 @@ class ConsoleLogger extends AbstractLogger
$t['line'] = 0;
}
$log .= "#{$i} {$t['file']}({$t['line']}): ";
/* @phpstan-ignore-next-line */
if (isset($t['object']) && is_object($t['object'])) {
$log .= get_class($t['object']) . '->';
}
@@ -219,75 +224,60 @@ class ConsoleLogger extends AbstractLogger
return ConsoleColor::apply($styles, $string)->__toString();
}
/**
* {@inheritDoc}
*/
public function log($level, $message, array $context = []): void
{
$level = $this->castLogLevel($level);
$log_replace = [
'%date%' => date(self::$date_format),
'%level_long%' => strtoupper(self::$levels[$level]),
'%level%' => strtoupper(substr(self::$levels[$level], 0, 4)),
'%body%' => $message,
'%level_short%' => strtoupper(substr(self::$levels[$level], 0, 1)),
];
$output = str_replace(array_keys($log_replace), array_values($log_replace), self::$format);
$output = $this->interpolate($output, array_merge($this->static_context, $context));
foreach ($this->log_callbacks as $callback) {
if ($callback($level, $output, $message, $context, $this->shouldLog($level)) === false) {
return;
}
}
if (!$this->shouldLog($level)) {
return;
}
$output = str_replace(
['%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));
foreach ($this->log_callbacks as $callback) {
if ($callback($level, $output, $message, $context) === false) {
return;
}
}
if ($this->decorated) {
$output = $this->colorize($output, $level) . PHP_EOL;
} else {
$output = $output . PHP_EOL;
$output .= PHP_EOL;
}
// 递归保护:如果上一次日志写入触发了错误(如终端关闭后 STDOUT/STDERR 破损),
// 跳过本次输出,防止错误处理器递归调用导致 CPU 100% 空耗
if (self::$in_log) {
return;
// use stream
if ($this->stream) {
fwrite($this->stream, $output);
fflush($this->stream);
} else {
// use plain text output
echo $output;
}
self::$in_log = true;
try {
// use stream
if ($this->stream) {
fwrite($this->stream, $output);
fflush($this->stream);
} else {
if ($level <= 4 && $this->use_stderr) {
fwrite(STDERR, $output);
} else {
// use plain text output
echo $output;
}
}
} finally {
self::$in_log = false;
}
}
public function setDecorated(bool $decorated): void
{
$this->decorated = $decorated;
}
/**
* 转换日志等级
*/
protected function castLogLevel(string $level): int
private function castLogLevel(string $level): int
{
if (in_array($level, self::$levels, true)) {
return array_flip(self::$levels)[$level];
}
throw new InvalidArgumentException('Invalid log level: ' . $level);
throw new InvalidArgumentException('无效的日志等级');
}
/**
@@ -295,7 +285,7 @@ class ConsoleLogger extends AbstractLogger
*
* @param mixed $item 日志内容
*/
protected function stringify($item): string
private function stringify($item): string
{
switch (true) {
case is_callable($item):
@@ -329,7 +319,7 @@ class ConsoleLogger extends AbstractLogger
/**
* 判断是否应该记录该等级日志
*/
protected function shouldLog(int $level): bool
private function shouldLog(int $level): bool
{
return $level <= self::$log_level;
}
@@ -340,7 +330,7 @@ class ConsoleLogger extends AbstractLogger
* @param string $message 日志内容
* @param array $context 变量列表
*/
protected function interpolate(string $message, array $context = []): string
private function interpolate(string $message, array $context = []): string
{
$replace = [];
foreach ($context as $key => $value) {
@@ -349,4 +339,23 @@ class ConsoleLogger extends AbstractLogger
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);
}
}

View File

@@ -137,8 +137,8 @@ class TablePrinter
$line_data[$current_line] = [
'used' => $valid_width - $k_len - 2 - $partial_v_len,
'can_put_second' => false,
'lines' => $k . ': '
. ConsoleColor::apply([$this->value_color], $partial_v . str_pad('', $valid_width - $k_len - 2 - $partial_v_len, '.')),
'lines' => $k . ': ' .
ConsoleColor::apply([$this->value_color], $partial_v . str_pad('', $valid_width - $k_len - 2 - $partial_v_len, '.')),
];
++$current_line;
// 下一个参数
@@ -237,12 +237,7 @@ class TablePrinter
}
} else {
$size = exec('stty size 2>/dev/null');
// in case stty is not available
if (empty($size)) {
$size = 0;
} else {
$size = (int) explode(' ', trim($size))[1];
}
$size = (int) explode(' ', trim($size))[1];
}
if (empty($size)) {
return $this->terminal_size = 79;