4 Commits

Author SHA1 Message Date
crazywhalecc
1d7427abd8 Add short level format 2025-04-24 10:02:28 +08:00
sunxyw
1b7e343493 Merge pull request #7 from zhamao-robot/fix-no-terminal-size-without-io
修復无交互环境下无法获取终端尺寸
2023-03-09 23:41:10 +08:00
sunxyw
97ee152121 update version 2023-03-09 23:40:30 +08:00
sunxyw
11156c65a2 fix no terminal size without io 2023-03-09 22:41:30 +08:00
2 changed files with 16 additions and 54 deletions

View File

@@ -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);
}
}

View File

@@ -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;