1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Balloon |
7
|
|
|
* |
8
|
|
|
* @author Raffael Sahli <[email protected]> |
9
|
|
|
* @copyright Copryright (c) 2012-2017 gyselroth GmbH (https://gyselroth.com) |
10
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Micro\Log\Adapter; |
14
|
|
|
|
15
|
|
|
use Psr\Log\LogLevel; |
16
|
|
|
|
17
|
|
|
class Stdout extends AbstractAdapter |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Bash fg colors. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
public $foreground_colors = [ |
25
|
|
|
'black' => '0;30', |
26
|
|
|
'dark_gray' => '1;30', |
27
|
|
|
'blue' => '0;34', |
28
|
|
|
'light_blue' => '1;34', |
29
|
|
|
'green' => '0;32', |
30
|
|
|
'light_green' => '1;32', |
31
|
|
|
'cyan' => '0;36', |
32
|
|
|
'light_cyan' => '1;36', |
33
|
|
|
'red' => '0;31', |
34
|
|
|
'light_red' => '1;31', |
35
|
|
|
'purple' => '0;35', |
36
|
|
|
'light_purple' => '1;35', |
37
|
|
|
'brown' => '0;33', |
38
|
|
|
'yellow' => '1;33', |
39
|
|
|
'light_gray' => '0;37', |
40
|
|
|
'white' => '1;37', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Bash bg colors. |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
public $background_colors = [ |
49
|
|
|
'black' => '40', |
50
|
|
|
'red' => '41', |
51
|
|
|
'green' => '42', |
52
|
|
|
'yellow' => '43', |
53
|
|
|
'blue' => '44', |
54
|
|
|
'magenta' => '45', |
55
|
|
|
'cyan' => '46', |
56
|
|
|
'light_gray' => '47', |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Convert to bash color. |
61
|
|
|
* |
62
|
|
|
* @param string $string |
63
|
|
|
* @param string $foreground_color |
64
|
|
|
* @param string $background_color |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getColoredString(string $string, string $foreground_color = null, string $background_color = null): string |
69
|
|
|
{ |
70
|
|
|
$colored_string = ''; |
71
|
|
|
|
72
|
|
|
if (isset($this->foreground_colors[$foreground_color])) { |
73
|
|
|
$colored_string .= "\033[".$this->foreground_colors[$foreground_color].'m'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (isset($this->background_colors[$background_color])) { |
77
|
|
|
$colored_string .= "\033[".$this->background_colors[$background_color].'m'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$colored_string .= $string."\033[0m"; |
81
|
|
|
|
82
|
|
|
return $colored_string; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Log. |
87
|
|
|
* |
88
|
|
|
* @param string $level |
89
|
|
|
* @param string $message |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function log(string $level, string $message): bool |
94
|
|
|
{ |
95
|
|
|
switch ($level) { |
96
|
|
|
case LogLevel::EMERGENCY: |
97
|
|
|
$message = '['.$this->getColoredString('EMERG', 'red').']'."\t".$message; |
98
|
|
|
|
99
|
|
|
break; |
100
|
|
|
case LogLevel::ALERT: |
101
|
|
|
$message = '['.$this->getColoredString('ALERT', 'red').']'."\t".$message; |
102
|
|
|
|
103
|
|
|
break; |
104
|
|
|
case LogLevel::CRITICAL: |
105
|
|
|
$message = '['.$this->getColoredString('CRIT', 'red').']'."\t".$message; |
106
|
|
|
|
107
|
|
|
break; |
108
|
|
|
case LogLevel::ERROR: |
109
|
|
|
$message = '['.$this->getColoredString('ERR', 'light_red').']'."\t".$message; |
110
|
|
|
|
111
|
|
|
break; |
112
|
|
|
case LogLevel::WARNING: |
113
|
|
|
$message = '['.$this->getColoredString('WARN', 'yellow').']'."\t".$message; |
114
|
|
|
|
115
|
|
|
break; |
116
|
|
|
case LogLevel::INFO: |
117
|
|
|
$message = '['.$this->getColoredString('INFO', 'cyan').']'."\t".$message; |
118
|
|
|
|
119
|
|
|
break; |
120
|
|
|
case LogLevel::NOTICE: |
121
|
|
|
$message = '['.$this->getColoredString('NOTICE', 'green').']'."\t".$message; |
122
|
|
|
|
123
|
|
|
break; |
124
|
|
|
case LogLevel::DEBUG: |
125
|
|
|
$message = '['.$this->getColoredString('DEBUG', 'blue').']'."\t".$message; |
126
|
|
|
|
127
|
|
|
break; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
echo $message."\n"; |
131
|
|
|
|
132
|
|
|
return true; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|