Skip to content

Commit

Permalink
add stream output support and "decorated" setting
Browse files Browse the repository at this point in the history
  • Loading branch information
crazywhalecc committed Nov 5, 2022
1 parent 2afe08d commit 92d2812
Showing 1 changed file with 63 additions and 9 deletions.
72 changes: 63 additions & 9 deletions src/ZM/Logger/ConsoleLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class ConsoleLogger extends AbstractLogger
{
public const VERSION = '1.0.0-alpha';
public const VERSION = '1.1.0';

/**
* 日志输出格式
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -128,7 +160,7 @@ public function addLogCallback(callable $callback): void
*/
public function trace(): void
{
$log = "Stack trace:\n";
$log = 'Stack trace:' . PHP_EOL;
$trace = debug_backtrace();
//array_shift($trace);
foreach ($trace as $i => $t) {
Expand All @@ -142,10 +174,20 @@ public function trace(): void
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;
}

/**
Expand Down Expand Up @@ -185,7 +227,19 @@ public function log($level, $message, array $context = []): void
}
}

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;
}
}

/**
Expand Down Expand Up @@ -219,7 +273,7 @@ private function stringify($item): string
case is_string($item):
return $item;
case is_array($item):
return 'array' . json_encode($item, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_LINE_TERMINATORS);
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):
Expand Down

0 comments on commit 92d2812

Please sign in to comment.