mirror of
https://github.com/zhamao-robot/zhamao-logger.git
synced 2026-07-10 18:25:35 +08:00
Compare commits
4 Commits
add-caller
...
1.1.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1d7427abd8 | ||
|
|
1b7e343493 | ||
|
|
97ee152121 | ||
|
|
11156c65a2 |
@@ -10,14 +10,14 @@ use Psr\Log\LogLevel;
|
||||
|
||||
class ConsoleLogger extends AbstractLogger
|
||||
{
|
||||
public const VERSION = '1.1.0';
|
||||
public const VERSION = '1.1.2';
|
||||
|
||||
/**
|
||||
* 日志输出格式
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $format = '[%date%] [%level%] %caller% %body%';
|
||||
public static $format = '[%date%] [%level%] %body%';
|
||||
|
||||
/**
|
||||
* 日志输出日期格式
|
||||
@@ -139,27 +139,6 @@ 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加静态上下文
|
||||
*/
|
||||
@@ -234,17 +213,14 @@ class ConsoleLogger extends AbstractLogger
|
||||
if (!$this->shouldLog($level)) {
|
||||
return;
|
||||
}
|
||||
$log_replace = [
|
||||
'%date%' => date(self::$date_format),
|
||||
'%level%' => strtoupper(substr(self::$levels[$level], 0, 4)),
|
||||
'%body%' => $message,
|
||||
'%level_short%' => strtoupper(substr(self::$levels[$level], 0, 1)),
|
||||
];
|
||||
|
||||
$output = str_replace(
|
||||
['%date%', '%level%', '%body%', '%caller%'],
|
||||
[
|
||||
date(self::$date_format),
|
||||
strtoupper(substr(self::$levels[$level], 0, 4)),
|
||||
$message,
|
||||
$this->getCaller(),
|
||||
],
|
||||
self::$format
|
||||
);
|
||||
$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) {
|
||||
@@ -256,7 +232,7 @@ class ConsoleLogger extends AbstractLogger
|
||||
if ($this->decorated) {
|
||||
$output = $this->colorize($output, $level) . PHP_EOL;
|
||||
} else {
|
||||
$output .= PHP_EOL;
|
||||
$output = $output . PHP_EOL;
|
||||
}
|
||||
// use stream
|
||||
if ($this->stream) {
|
||||
@@ -339,23 +315,4 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,7 +237,12 @@ class TablePrinter
|
||||
}
|
||||
} else {
|
||||
$size = exec('stty size 2>/dev/null');
|
||||
$size = (int) explode(' ', trim($size))[1];
|
||||
// in case stty is not available
|
||||
if (empty($size)) {
|
||||
$size = 0;
|
||||
} else {
|
||||
$size = (int) explode(' ', trim($size))[1];
|
||||
}
|
||||
}
|
||||
if (empty($size)) {
|
||||
return $this->terminal_size = 79;
|
||||
|
||||
Reference in New Issue
Block a user