-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathConsoleTarget.php
138 lines (118 loc) · 3.13 KB
/
ConsoleTarget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* @link http://www.yiiframework.com/
* @license http://www.yiiframework.com/license/
*/
namespace pahanini\log;
use Throwable;
use yii\helpers\BaseConsole;
use yii\helpers\Console;
use yii\log\Logger;
use yii\log\Target;
use yii\helpers\VarDumper;
use const STDOUT;
/**
* ConsoleTarget writes log to console (useful for debugging console applications)
*
* @author pahanini <pahanini@gmail.com>
*/
class ConsoleTarget extends Target
{
/**
* @var bool If true context message will be added to the end of output
*/
public $enableContextMassage = false;
public $displayCategory = false;
public $displayDate = false;
public $dateFormat = 'Y-m-d H:i:s';
public $padSize = 30;
/**
* @var array color scheme for message labels
*/
public $color = [
'error' => BaseConsole::BG_RED
];
/**
* @inheritdoc
* @return string
*/
protected function getContextMessage(): string
{
return $this->enableContextMassage ? parent::getContextMessage() : '';
}
/**
* @inheritdoc
*/
public function export(): void
{
foreach ($this->messages as $message) {
if ($message[1] == Logger::LEVEL_ERROR) {
Console::error($this->formatMessage($message));
} else {
Console::output($this->formatMessage($message));
}
}
}
/**
* @param array $message
* 0 - massage
* 1 - level
* 2 - category
* 3 - timestamp
* 4 - ???
*
* @return string
*/
public function formatMessage($message): string
{
$label = $this->generateLabel($message);
$text = $this->generateText($message);
return str_pad($label, $this->padSize, ' ') . ' '.$text;
}
/**
* @param $message
*
* @return string
*/
private function generateLabel($message): string
{
$label = '';
//Add date to log
if (true === $this->displayDate) {
$label.= '['.date($this->dateFormat, (int)$message[3]).']';
}
//Add category to label
if (true === $this->displayCategory) {
$label.= "[".$message[2]."]";
}
$level = Logger::getLevelName($message[1]);
$tmpLevel= "[$level]";
if (Console::streamSupportsAnsiColors(STDOUT)) {
if (isset($this->color[$level])) {
$tmpLevel = Console::ansiFormat($tmpLevel, [$this->color[$level]]);
} else {
$tmpLevel = Console::ansiFormat($tmpLevel, [BaseConsole::BOLD]);
}
}
$label.= $tmpLevel;
return $label;
}
/**
* @param $message
*
* @return string
*/
private function generateText($message): string
{
$text = $message[0];
if (!is_string($text)) {
// exceptions may not be serializable if in the call stack somewhere is a Closure
if ($text instanceof Throwable) {
$text = (string) $text;
} else {
$text = VarDumper::export($text);
}
}
return $text;
}
}