mirror of
https://github.com/zhamao-robot/zhamao-logger.git
synced 2026-07-02 14:25:40 +08:00
Update to 1.1.3
This commit is contained in:
@@ -10,7 +10,7 @@ use Psr\Log\LogLevel;
|
|||||||
|
|
||||||
class ConsoleLogger extends AbstractLogger
|
class ConsoleLogger extends AbstractLogger
|
||||||
{
|
{
|
||||||
public const VERSION = '1.1.2';
|
public const VERSION = '1.1.3';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 日志输出格式
|
* 日志输出格式
|
||||||
@@ -113,8 +113,8 @@ class ConsoleLogger extends AbstractLogger
|
|||||||
if (($stat['mode'] & 0170000) === 0100000) { // whether is regular file
|
if (($stat['mode'] & 0170000) === 0100000) { // whether is regular file
|
||||||
$this->decorated = false;
|
$this->decorated = false;
|
||||||
} else {
|
} else {
|
||||||
$this->decorated =
|
$this->decorated
|
||||||
PHP_OS_FAMILY !== 'Windows' // linux or unix
|
= PHP_OS_FAMILY !== 'Windows' // linux or unix
|
||||||
&& function_exists('posix_isatty')
|
&& function_exists('posix_isatty')
|
||||||
&& posix_isatty($stream); // whether is interactive terminal
|
&& posix_isatty($stream); // whether is interactive terminal
|
||||||
}
|
}
|
||||||
@@ -162,7 +162,7 @@ class ConsoleLogger extends AbstractLogger
|
|||||||
{
|
{
|
||||||
$log = 'Stack trace:' . PHP_EOL;
|
$log = 'Stack trace:' . PHP_EOL;
|
||||||
$trace = debug_backtrace();
|
$trace = debug_backtrace();
|
||||||
//array_shift($trace);
|
// array_shift($trace);
|
||||||
foreach ($trace as $i => $t) {
|
foreach ($trace as $i => $t) {
|
||||||
if (!isset($t['file'])) {
|
if (!isset($t['file'])) {
|
||||||
$t['file'] = 'unknown';
|
$t['file'] = 'unknown';
|
||||||
@@ -203,16 +203,10 @@ class ConsoleLogger extends AbstractLogger
|
|||||||
return ConsoleColor::apply($styles, $string)->__toString();
|
return ConsoleColor::apply($styles, $string)->__toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
public function log($level, $message, array $context = []): void
|
public function log($level, $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$level = $this->castLogLevel($level);
|
$level = $this->castLogLevel($level);
|
||||||
|
|
||||||
if (!$this->shouldLog($level)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$log_replace = [
|
$log_replace = [
|
||||||
'%date%' => date(self::$date_format),
|
'%date%' => date(self::$date_format),
|
||||||
'%level%' => strtoupper(substr(self::$levels[$level], 0, 4)),
|
'%level%' => strtoupper(substr(self::$levels[$level], 0, 4)),
|
||||||
@@ -224,11 +218,15 @@ class ConsoleLogger extends AbstractLogger
|
|||||||
$output = $this->interpolate($output, array_merge($this->static_context, $context));
|
$output = $this->interpolate($output, array_merge($this->static_context, $context));
|
||||||
|
|
||||||
foreach ($this->log_callbacks as $callback) {
|
foreach ($this->log_callbacks as $callback) {
|
||||||
if ($callback($level, $output, $message, $context) === false) {
|
if ($callback($level, $output, $message, $context, $this->shouldLog($level)) === false) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!$this->shouldLog($level)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->decorated) {
|
if ($this->decorated) {
|
||||||
$output = $this->colorize($output, $level) . PHP_EOL;
|
$output = $this->colorize($output, $level) . PHP_EOL;
|
||||||
} else {
|
} else {
|
||||||
@@ -247,13 +245,13 @@ class ConsoleLogger extends AbstractLogger
|
|||||||
/**
|
/**
|
||||||
* 转换日志等级
|
* 转换日志等级
|
||||||
*/
|
*/
|
||||||
private function castLogLevel(string $level): int
|
protected function castLogLevel(string $level): int
|
||||||
{
|
{
|
||||||
if (in_array($level, self::$levels, true)) {
|
if (in_array($level, self::$levels, true)) {
|
||||||
return array_flip(self::$levels)[$level];
|
return array_flip(self::$levels)[$level];
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new InvalidArgumentException('无效的日志等级');
|
throw new InvalidArgumentException('Invalid log level: ' . $level);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -261,7 +259,7 @@ class ConsoleLogger extends AbstractLogger
|
|||||||
*
|
*
|
||||||
* @param mixed $item 日志内容
|
* @param mixed $item 日志内容
|
||||||
*/
|
*/
|
||||||
private function stringify($item): string
|
protected function stringify($item): string
|
||||||
{
|
{
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case is_callable($item):
|
case is_callable($item):
|
||||||
@@ -295,7 +293,7 @@ class ConsoleLogger extends AbstractLogger
|
|||||||
/**
|
/**
|
||||||
* 判断是否应该记录该等级日志
|
* 判断是否应该记录该等级日志
|
||||||
*/
|
*/
|
||||||
private function shouldLog(int $level): bool
|
protected function shouldLog(int $level): bool
|
||||||
{
|
{
|
||||||
return $level <= self::$log_level;
|
return $level <= self::$log_level;
|
||||||
}
|
}
|
||||||
@@ -306,7 +304,7 @@ class ConsoleLogger extends AbstractLogger
|
|||||||
* @param string $message 日志内容
|
* @param string $message 日志内容
|
||||||
* @param array $context 变量列表
|
* @param array $context 变量列表
|
||||||
*/
|
*/
|
||||||
private function interpolate(string $message, array $context = []): string
|
protected function interpolate(string $message, array $context = []): string
|
||||||
{
|
{
|
||||||
$replace = [];
|
$replace = [];
|
||||||
foreach ($context as $key => $value) {
|
foreach ($context as $key => $value) {
|
||||||
|
|||||||
@@ -137,8 +137,8 @@ class TablePrinter
|
|||||||
$line_data[$current_line] = [
|
$line_data[$current_line] = [
|
||||||
'used' => $valid_width - $k_len - 2 - $partial_v_len,
|
'used' => $valid_width - $k_len - 2 - $partial_v_len,
|
||||||
'can_put_second' => false,
|
'can_put_second' => false,
|
||||||
'lines' => $k . ': ' .
|
'lines' => $k . ': '
|
||||||
ConsoleColor::apply([$this->value_color], $partial_v . str_pad('', $valid_width - $k_len - 2 - $partial_v_len, '.')),
|
. ConsoleColor::apply([$this->value_color], $partial_v . str_pad('', $valid_width - $k_len - 2 - $partial_v_len, '.')),
|
||||||
];
|
];
|
||||||
++$current_line;
|
++$current_line;
|
||||||
// 下一个参数
|
// 下一个参数
|
||||||
|
|||||||
Reference in New Issue
Block a user