Skip to content

Commit

Permalink
colorize support for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
desperado committed Dec 30, 2019
1 parent 080d8c5 commit d92b857
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 20 deletions.
91 changes: 91 additions & 0 deletions src/Infrastructure/Logger/Handlers/StdOut/StdOutFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* PHP Service Bus (publish-subscribe pattern implementation).
*
* @author Maksim Masiukevich <dev@async-php.com>
* @license MIT
* @license https://opensource.org/licenses/MIT
*/

declare(strict_types = 1);

namespace ServiceBus\Infrastructure\Logger\Handlers\StdOut;

use Monolog\Formatter\LineFormatter;
use Psr\Log\LogLevel;
use function Amp\Log\hasColorSupport;
use function ServiceBus\Common\jsonEncode;

/**
* @codeCoverageIgnore
*/
final class StdOutFormatter extends LineFormatter
{
/** @var bool */
private $colorize;

public function __construct()
{
parent::__construct("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\r\n");

$this->colorize = hasColorSupport();
}

/**
* @inheritDoc
*/
public function format(array $record): string
{
if ($this->colorize === true)
{
$record['level_name'] = $this->ansifyLevel($record['level_name']);
$record['channel'] = "\033[1m{$record['channel']}\033[0m";
}

return parent::format($record);
}

/**
* @inheritDoc
*/
protected function toJson($data, bool $ignoreErrors = false): string
{
if (\is_array($data) === true)
{
return jsonEncode($data);
}

return '';
}

private function ansifyLevel(string $level): string
{
$level = \strtolower($level);

switch ($level)
{
case LogLevel::EMERGENCY:
case LogLevel::ALERT:
case LogLevel::CRITICAL:
case LogLevel::ERROR:
/** bold + red */
return "\033[1;31m{$level}\033[0m";
case LogLevel::WARNING:
/** bold + yellow */
return "\033[1;33m{$level}\033[0m";
case LogLevel::NOTICE:
/** bold + green */
return "\033[1;32m{$level}\033[0m";
case LogLevel::INFO:
/** bold + magenta */
return "\033[1;35m{$level}\033[0m";
case LogLevel::DEBUG:
/** bold + cyan */
return "\033[1;36m{$level}\033[0m";
default:
/** bold */
return "\033[1m{$level}\033[0m";
}
}
}
21 changes: 1 addition & 20 deletions src/Infrastructure/Logger/Handlers/StdOut/StdOutHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@

use Amp\ByteStream\ResourceOutputStream;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use function ServiceBus\Common\jsonEncode;

/**
* Console output handler.
Expand All @@ -33,24 +31,7 @@ public function __construct(int $level = Logger::DEBUG, bool $bubble = true, ?Fo
{
parent::__construct($level, $bubble);

$this->formatter = $formatter
?: new class() extends LineFormatter
{
public function __construct()
{
parent::__construct("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\r\n");
}

protected function toJson($data, bool $ignoreErrors = false): string
{
if (\is_array($data) === true)
{
return jsonEncode($data);
}

return '';
}
};
$this->formatter = $formatter ?? new StdOutFormatter();

$this->streamWriter = new ResourceOutputStream(\STDOUT, 50000);
}
Expand Down

0 comments on commit d92b857

Please sign in to comment.