mirror of
https://github.com/zhamao-robot/zhamao-logger.git
synced 2026-07-02 14:25:40 +08:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
30bee20a0a | ||
|
|
d13c8fb85f | ||
|
|
c21ddda192 | ||
|
|
d79edcef53 | ||
|
|
ac5fa59428 | ||
|
|
26dc5fa8c3 | ||
|
|
e75fa01ca5 |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "zhamao/logger",
|
||||
"description": "Another Console Logger for CLI Applications",
|
||||
"description": "Another Colorful Console Logger for CLI Applications",
|
||||
"type": "library",
|
||||
"license": "Apache-2.0",
|
||||
"autoload": {
|
||||
@@ -16,7 +16,7 @@
|
||||
"authors": [
|
||||
{
|
||||
"name": "jerry",
|
||||
"email": "admin@zhamao.me"
|
||||
"email": "github@cwcc.me"
|
||||
},
|
||||
{
|
||||
"name": "sunxyw",
|
||||
@@ -25,7 +25,7 @@
|
||||
],
|
||||
"minimum-stability": "stable",
|
||||
"require": {
|
||||
"php": "^7.2 || ^7.3 || ^7.4 || ^8.0 || ^8.1",
|
||||
"php": "^7.2 || ^7.3 || ^7.4 || ^8.0 || ^8.1 || ^8.2 || ^8.3 || ^8.4 || ^8.5",
|
||||
"psr/log": "^1 || ^2 || ^3",
|
||||
"symfony/polyfill-mbstring": "^1.0"
|
||||
},
|
||||
@@ -37,9 +37,9 @@
|
||||
"ext-mbstring": "Use C/C++ extension instead of polyfill will be more efficient"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "^3.2",
|
||||
"phpstan/phpstan": "^1.1",
|
||||
"phpunit/phpunit": "^8.5 || ^9.0",
|
||||
"friendsofphp/php-cs-fixer": "^3.64",
|
||||
"phpstan/phpstan": "^1.12",
|
||||
"phpunit/phpunit": "^9.0",
|
||||
"roave/security-advisories": "dev-latest",
|
||||
"brainmaestro/composer-git-hooks": "^2.8"
|
||||
},
|
||||
|
||||
@@ -10,7 +10,7 @@ use Psr\Log\LogLevel;
|
||||
|
||||
class ConsoleLogger extends AbstractLogger
|
||||
{
|
||||
public const VERSION = '1.1.4';
|
||||
public const VERSION = '1.1.9';
|
||||
|
||||
/**
|
||||
* 日志输出格式
|
||||
@@ -86,6 +86,13 @@ class ConsoleLogger extends AbstractLogger
|
||||
*/
|
||||
protected $stream;
|
||||
|
||||
/**
|
||||
* 递归保护标志 — 防止日志写入失败时错误处理器再次调用日志导致死循环 CPU 100%
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected static $in_log = false;
|
||||
|
||||
/**
|
||||
* 是否带颜色
|
||||
*
|
||||
@@ -93,13 +100,15 @@ class ConsoleLogger extends AbstractLogger
|
||||
*/
|
||||
protected $decorated;
|
||||
|
||||
protected $use_stderr = false;
|
||||
|
||||
/**
|
||||
* 创建一个 ConsoleLogger 实例
|
||||
*
|
||||
* @param string $level 日志等级
|
||||
* @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;
|
||||
self::$log_level = $this->castLogLevel($level);
|
||||
@@ -119,6 +128,7 @@ class ConsoleLogger extends AbstractLogger
|
||||
&& posix_isatty($stream); // whether is interactive terminal
|
||||
}
|
||||
$this->stream = $stream;
|
||||
$this->use_stderr = $use_stderr;
|
||||
}
|
||||
|
||||
public function setLevel(string $level): void
|
||||
@@ -215,6 +225,7 @@ class ConsoleLogger extends AbstractLogger
|
||||
|
||||
$log_replace = [
|
||||
'%date%' => date(self::$date_format),
|
||||
'%level_long%' => strtoupper(self::$levels[$level]),
|
||||
'%level%' => strtoupper(substr(self::$levels[$level], 0, 4)),
|
||||
'%body%' => $message,
|
||||
'%level_short%' => strtoupper(substr(self::$levels[$level], 0, 1)),
|
||||
@@ -238,14 +249,33 @@ class ConsoleLogger extends AbstractLogger
|
||||
} else {
|
||||
$output = $output . PHP_EOL;
|
||||
}
|
||||
// use stream
|
||||
if ($this->stream) {
|
||||
fwrite($this->stream, $output);
|
||||
fflush($this->stream);
|
||||
} else {
|
||||
// use plain text output
|
||||
echo $output;
|
||||
// 递归保护:如果上一次日志写入触发了错误(如终端关闭后 STDOUT/STDERR 破损),
|
||||
// 跳过本次输出,防止错误处理器递归调用导致 CPU 100% 空耗
|
||||
if (self::$in_log) {
|
||||
return;
|
||||
}
|
||||
self::$in_log = true;
|
||||
try {
|
||||
// use stream
|
||||
if ($this->stream) {
|
||||
fwrite($this->stream, $output);
|
||||
fflush($this->stream);
|
||||
} else {
|
||||
if ($level <= 4 && $this->use_stderr) {
|
||||
fwrite(STDERR, $output);
|
||||
} else {
|
||||
// use plain text output
|
||||
echo $output;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
self::$in_log = false;
|
||||
}
|
||||
}
|
||||
|
||||
public function setDecorated(bool $decorated): void
|
||||
{
|
||||
$this->decorated = $decorated;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -319,9 +349,4 @@ class ConsoleLogger extends AbstractLogger
|
||||
|
||||
return strtr($message, $replace);
|
||||
}
|
||||
|
||||
public function setDecorated(bool $decorated): void
|
||||
{
|
||||
$this->decorated = $decorated;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user