|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Phoole (PHP7.2+) |
|
5
|
|
|
* |
|
6
|
|
|
* @category Library |
|
7
|
|
|
* @package Phoole\Logger |
|
8
|
|
|
* @copyright Copyright (c) 2019 Hong Zhang |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Phoole\Logger\Formatter; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Log\LogLevel; |
|
15
|
|
|
use Phoole\Logger\Entry\LogEntryInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* ANSI Color formatter |
|
19
|
|
|
* |
|
20
|
|
|
* @package Phoole\Logger |
|
21
|
|
|
*/ |
|
22
|
|
|
class AnsiFormatter extends DefaultFormatter |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* foreground color |
|
26
|
|
|
* |
|
27
|
|
|
* @const |
|
28
|
|
|
*/ |
|
29
|
|
|
const FGCOLOR_BLACK = "\033[0;30m"; |
|
30
|
|
|
const FGCOLOR_RED = "\033[0;31m"; |
|
31
|
|
|
const FGCOLOR_GREEN = "\033[0;32m"; |
|
32
|
|
|
const FGCOLOR_YELLOW = "\033[0;33m"; |
|
33
|
|
|
const FGCOLOR_BLUE = "\033[0;34m"; |
|
34
|
|
|
const FGCOLOR_MAGENTA = "\033[0;35m"; |
|
35
|
|
|
const FGCOLOR_CYAN = "\033[0;36m"; |
|
36
|
|
|
const FGCOLOR_GRAY = "\033[0;37m"; |
|
37
|
|
|
const FGCOLOR_DARK_GRAY = "\033[1;30m"; |
|
38
|
|
|
const FGCOLOR_BRIGHT_RED = "\033[1;31m"; |
|
39
|
|
|
const FGCOLOR_BRIGHT_GREEN = "\033[1;32m"; |
|
40
|
|
|
const FGCOLOR_BRIGHT_YELLOW = "\033[1;33m"; |
|
41
|
|
|
const FGCOLOR_BRIGHT_BLUE = "\033[1;34m"; |
|
42
|
|
|
const FGCOLOR_BRIGHT_MAGENTA = "\033[1;35m"; |
|
43
|
|
|
const FGCOLOR_BRIGHT_CYAN = "\033[1;36m"; |
|
44
|
|
|
const FGCOLOR_WHITE = "\033[1;37m"; |
|
45
|
|
|
/** |
|
46
|
|
|
* background color |
|
47
|
|
|
* |
|
48
|
|
|
* @const |
|
49
|
|
|
*/ |
|
50
|
|
|
const BGCOLOR_BLACK = "\033[40m"; |
|
51
|
|
|
const BGCOLOR_RED = "\033[41m"; |
|
52
|
|
|
const BGCOLOR_GREEN = "\033[42m"; |
|
53
|
|
|
const BGCOLOR_YELLOW = "\033[43m"; |
|
54
|
|
|
const BGCOLOR_BLUE = "\033[44m"; |
|
55
|
|
|
const BGCOLOR_MAGENTA = "\033[45m"; |
|
56
|
|
|
const BGCOLOR_CYAN = "\033[46m"; |
|
57
|
|
|
const BGCOLOR_WHITE = "\033[47m"; |
|
58
|
|
|
const DECO_BOLD = "\033[1m"; |
|
59
|
|
|
const DECO_UNDERLINE = "\033[4m"; |
|
60
|
|
|
const DECO_BLINK = "\033[5m"; |
|
61
|
|
|
const DECO_REVERSE = "\033[7m"; |
|
62
|
|
|
const DECO_CROSS = "\033[9m"; |
|
63
|
|
|
const DECO_END = "\033[0m"; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Color definitions for different log levels |
|
67
|
|
|
* format [ fgColor, bgColor, textDeco ] |
|
68
|
|
|
* |
|
69
|
|
|
* @var array |
|
70
|
|
|
* @access protected |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $colors = array( |
|
73
|
|
|
LogLevel::DEBUG => [self::FGCOLOR_GRAY, '', ''], |
|
74
|
|
|
LogLevel::INFO => ['', '', ''], |
|
75
|
|
|
LogLevel::NOTICE => [self::FGCOLOR_BRIGHT_GREEN, '', ''], |
|
76
|
|
|
LogLevel::WARNING => [self::FGCOLOR_BRIGHT_YELLOW, '', ''], |
|
77
|
|
|
LogLevel::ERROR => [self::FGCOLOR_BRIGHT_RED, '', ''], |
|
78
|
|
|
LogLevel::CRITICAL => [self::FGCOLOR_BRIGHT_RED, '', self::DECO_UNDERLINE], |
|
79
|
|
|
LogLevel::ALERT => [self::FGCOLOR_BRIGHT_RED, self::BGCOLOR_WHITE, ''], |
|
80
|
|
|
LogLevel::EMERGENCY => [self::FGCOLOR_BRIGHT_RED, self::BGCOLOR_WHITE, self::DECO_BLINK], |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritDoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function format(LogEntryInterface $entry): string |
|
87
|
|
|
{ |
|
88
|
|
|
$text = parent::format($entry); |
|
89
|
|
|
return $this->addColor($entry->getLevel(), $text); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* add ansi color to text |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $level |
|
96
|
|
|
* @param string $text |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function addColor(string $level, string $text): string |
|
100
|
|
|
{ |
|
101
|
|
|
list($fgColor, $bgColor, $deColor) = $this->colors[$level]; |
|
102
|
|
|
$prefix = $fgColor . $bgColor . $deColor; |
|
103
|
|
|
$suffix = $prefix ? self::DECO_END : ''; |
|
104
|
|
|
return $prefix . $text . $suffix; |
|
105
|
|
|
} |
|
106
|
|
|
} |