3 Commits
1.1.2 ... 1.1.4

Author SHA1 Message Date
crazywhalecc
b28c0c26d5 Add setLevel function 2025-11-08 23:38:01 +08:00
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
2 changed files with 27 additions and 18 deletions

View File

@@ -10,7 +10,7 @@ use Psr\Log\LogLevel;
class ConsoleLogger extends AbstractLogger
{
public const VERSION = '1.1.2';
public const VERSION = '1.1.4';
/**
* 日志输出格式
@@ -113,14 +113,19 @@ 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;
}
public function setLevel(string $level): void
{
self::$log_level = $this->castLogLevel($level);
}
/**
* 获取当前样式表
*
@@ -162,7 +167,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';
@@ -171,6 +176,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']) . '->';
}
@@ -203,16 +209,10 @@ class ConsoleLogger extends AbstractLogger
return ConsoleColor::apply($styles, $string)->__toString();
}
/**
* {@inheritDoc}
*/
public function log($level, $message, array $context = []): void
{
$level = $this->castLogLevel($level);
if (!$this->shouldLog($level)) {
return;
}
$log_replace = [
'%date%' => date(self::$date_format),
'%level%' => strtoupper(substr(self::$levels[$level], 0, 4)),
@@ -224,11 +224,15 @@ class ConsoleLogger extends AbstractLogger
$output = $this->interpolate($output, array_merge($this->static_context, $context));
foreach ($this->log_callbacks as $callback) {
if ($callback($level, $output, $message, $context) === false) {
if ($callback($level, $output, $message, $context, $this->shouldLog($level)) === false) {
return;
}
}
if (!$this->shouldLog($level)) {
return;
}
if ($this->decorated) {
$output = $this->colorize($output, $level) . PHP_EOL;
} else {
@@ -247,13 +251,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);
}
/**
@@ -261,7 +265,7 @@ class ConsoleLogger extends AbstractLogger
*
* @param mixed $item 日志内容
*/
private function stringify($item): string
protected function stringify($item): string
{
switch (true) {
case is_callable($item):
@@ -295,7 +299,7 @@ class ConsoleLogger extends AbstractLogger
/**
* 判断是否应该记录该等级日志
*/
private function shouldLog(int $level): bool
protected function shouldLog(int $level): bool
{
return $level <= self::$log_level;
}
@@ -306,7 +310,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) {
@@ -315,4 +319,9 @@ class ConsoleLogger extends AbstractLogger
return strtr($message, $replace);
}
public function setDecorated(bool $decorated): void
{
$this->decorated = $decorated;
}
}

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;
// 下一个参数