6 Commits

Author SHA1 Message Date
crazywhalecc
2551b3e1db phpstan 2025-08-02 21:44:27 +08:00
crazywhalecc
81ab0b98f6 Update to 1.1.3 2025-08-02 21:43:12 +08:00
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 36 additions and 75 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.3';
/**
* 日志输出格式
*
* @var string
*/
public static $format = '[%date%] [%level%] %caller% %body%';
public static $format = '[%date%] [%level%] %body%';
/**
* 日志输出日期格式
@@ -113,8 +113,8 @@ 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
}
@@ -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;
}
/**
* 添加静态上下文
*/
@@ -183,7 +162,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';
@@ -192,6 +171,7 @@ 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']) . '->';
}
@@ -224,39 +204,34 @@ 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%' => 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 .= PHP_EOL;
$output = $output . PHP_EOL;
}
// use stream
if ($this->stream) {
@@ -271,13 +246,13 @@ class ConsoleLogger extends AbstractLogger
/**
* 转换日志等级
*/
private function castLogLevel(string $level): int
protected function castLogLevel(string $level): int
{
if (in_array($level, self::$levels, true)) {
return array_flip(self::$levels)[$level];
}
throw new InvalidArgumentException('无效的日志等级');
throw new InvalidArgumentException('Invalid log level: ' . $level);
}
/**
@@ -285,7 +260,7 @@ class ConsoleLogger extends AbstractLogger
*
* @param mixed $item 日志内容
*/
private function stringify($item): string
protected function stringify($item): string
{
switch (true) {
case is_callable($item):
@@ -319,7 +294,7 @@ class ConsoleLogger extends AbstractLogger
/**
* 判断是否应该记录该等级日志
*/
private function shouldLog(int $level): bool
protected function shouldLog(int $level): bool
{
return $level <= self::$log_level;
}
@@ -330,7 +305,7 @@ class ConsoleLogger extends AbstractLogger
* @param string $message 日志内容
* @param array $context 变量列表
*/
private function interpolate(string $message, array $context = []): string
protected function interpolate(string $message, array $context = []): string
{
$replace = [];
foreach ($context as $key => $value) {
@@ -339,23 +314,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

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