Enhance output handling in ConsoleLogger to use STDERR for specific log levels

This commit is contained in:
crazywhalecc 2025-12-16 10:56:19 +08:00
parent e75fa01ca5
commit 26dc5fa8c3
No known key found for this signature in database
GPG Key ID: 1F4BDD59391F2680

View File

@ -10,7 +10,7 @@ use Psr\Log\LogLevel;
class ConsoleLogger extends AbstractLogger class ConsoleLogger extends AbstractLogger
{ {
public const VERSION = '1.1.4'; public const VERSION = '1.1.5';
/** /**
* 日志输出格式 * 日志输出格式
@ -93,13 +93,15 @@ class ConsoleLogger extends AbstractLogger
*/ */
protected $decorated; protected $decorated;
protected $use_stderr = false;
/** /**
* 创建一个 ConsoleLogger 实例 * 创建一个 ConsoleLogger 实例
* *
* @param string $level 日志等级 * @param string $level 日志等级
* @param null|resource $stream * @param null|resource $stream
*/ */
public function __construct(string $level = LogLevel::INFO, $stream = null, bool $decorated = true) public function __construct(string $level = LogLevel::INFO, $stream = null, bool $decorated = true, bool $use_stderr = false)
{ {
$this->decorated = $decorated; $this->decorated = $decorated;
self::$log_level = $this->castLogLevel($level); self::$log_level = $this->castLogLevel($level);
@ -119,6 +121,7 @@ class ConsoleLogger extends AbstractLogger
&& posix_isatty($stream); // whether is interactive terminal && posix_isatty($stream); // whether is interactive terminal
} }
$this->stream = $stream; $this->stream = $stream;
$this->use_stderr = $use_stderr;
} }
public function setLevel(string $level): void public function setLevel(string $level): void
@ -252,6 +255,11 @@ class ConsoleLogger extends AbstractLogger
} }
} }
public function setDecorated(bool $decorated): void
{
$this->decorated = $decorated;
}
/** /**
* 转换日志等级 * 转换日志等级
*/ */
@ -323,9 +331,4 @@ class ConsoleLogger extends AbstractLogger
return strtr($message, $replace); return strtr($message, $replace);
} }
public function setDecorated(bool $decorated): void
{
$this->decorated = $decorated;
}
} }