mirror of
https://github.com/zhamao-robot/zhamao-logger.git
synced 2026-07-02 14:25:40 +08:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1d7427abd8 | ||
|
|
1b7e343493 | ||
|
|
97ee152121 | ||
|
|
11156c65a2 | ||
|
|
5158a0ee2a | ||
|
|
92d281262c |
@@ -10,7 +10,7 @@ use Psr\Log\LogLevel;
|
||||
|
||||
class ConsoleLogger extends AbstractLogger
|
||||
{
|
||||
public const VERSION = '1.0.2';
|
||||
public const VERSION = '1.1.2';
|
||||
|
||||
/**
|
||||
* 日志输出格式
|
||||
@@ -79,14 +79,46 @@ class ConsoleLogger extends AbstractLogger
|
||||
*/
|
||||
protected $log_callbacks = [];
|
||||
|
||||
/**
|
||||
* Stream 写入
|
||||
*
|
||||
* @var null|int|resource
|
||||
*/
|
||||
protected $stream;
|
||||
|
||||
/**
|
||||
* 是否带颜色
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $decorated;
|
||||
|
||||
/**
|
||||
* 创建一个 ConsoleLogger 实例
|
||||
*
|
||||
* @param string $level 日志等级
|
||||
* @param string $level 日志等级
|
||||
* @param null|resource $stream
|
||||
*/
|
||||
public function __construct(string $level = LogLevel::INFO)
|
||||
public function __construct(string $level = LogLevel::INFO, $stream = null, bool $decorated = true)
|
||||
{
|
||||
$this->decorated = $decorated;
|
||||
self::$log_level = $this->castLogLevel($level);
|
||||
if (!$stream || !is_resource($stream) || get_resource_type($stream) !== 'stream') {
|
||||
return;
|
||||
}
|
||||
$stat = fstat($stream);
|
||||
if (!$stat) {
|
||||
return;
|
||||
}
|
||||
if (($stat['mode'] & 0170000) === 0100000) { // whether is regular file
|
||||
$this->decorated = false;
|
||||
} else {
|
||||
$this->decorated =
|
||||
PHP_OS_FAMILY !== 'Windows' // linux or unix
|
||||
&& function_exists('posix_isatty')
|
||||
&& posix_isatty($stream); // whether is interactive terminal
|
||||
}
|
||||
$this->stream = $stream;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,7 +160,7 @@ class ConsoleLogger extends AbstractLogger
|
||||
*/
|
||||
public function trace(): void
|
||||
{
|
||||
$log = "Stack trace:\n";
|
||||
$log = 'Stack trace:' . PHP_EOL;
|
||||
$trace = debug_backtrace();
|
||||
//array_shift($trace);
|
||||
foreach ($trace as $i => $t) {
|
||||
@@ -142,10 +174,20 @@ class ConsoleLogger extends AbstractLogger
|
||||
if (isset($t['object']) && is_object($t['object'])) {
|
||||
$log .= get_class($t['object']) . '->';
|
||||
}
|
||||
$log .= "{$t['function']}()\n";
|
||||
$log .= "{$t['function']}()" . PHP_EOL;
|
||||
}
|
||||
if ($this->decorated) {
|
||||
$log = $this->colorize($log, $this->castLogLevel(LogLevel::DEBUG));
|
||||
}
|
||||
|
||||
// use stream
|
||||
if ($this->stream) {
|
||||
fwrite($this->stream, $log);
|
||||
fflush($this->stream);
|
||||
} else {
|
||||
// use plain text output
|
||||
echo $log;
|
||||
}
|
||||
$log = $this->colorize($log, $this->castLogLevel(LogLevel::DEBUG));
|
||||
echo $log;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -171,12 +213,14 @@ class ConsoleLogger extends AbstractLogger
|
||||
if (!$this->shouldLog($level)) {
|
||||
return;
|
||||
}
|
||||
$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(
|
||||
['%date%', '%level%', '%body%'],
|
||||
[date(self::$date_format), strtoupper(substr(self::$levels[$level], 0, 4)), $message],
|
||||
self::$format
|
||||
);
|
||||
$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) {
|
||||
@@ -185,7 +229,19 @@ class ConsoleLogger extends AbstractLogger
|
||||
}
|
||||
}
|
||||
|
||||
echo $this->colorize($output, $level) . "\n";
|
||||
if ($this->decorated) {
|
||||
$output = $this->colorize($output, $level) . PHP_EOL;
|
||||
} else {
|
||||
$output = $output . PHP_EOL;
|
||||
}
|
||||
// use stream
|
||||
if ($this->stream) {
|
||||
fwrite($this->stream, $output);
|
||||
fflush($this->stream);
|
||||
} else {
|
||||
// use plain text output
|
||||
echo $output;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -219,10 +275,7 @@ class ConsoleLogger extends AbstractLogger
|
||||
case is_string($item):
|
||||
return $item;
|
||||
case is_array($item):
|
||||
if (extension_loaded('json')) {
|
||||
return 'array' . json_encode($item, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_LINE_TERMINATORS);
|
||||
}
|
||||
return var_export($item, true);
|
||||
return 'array' . (extension_loaded('json') ? json_encode($item, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_LINE_TERMINATORS) : '');
|
||||
case is_object($item):
|
||||
return get_class($item);
|
||||
case is_resource($item):
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user