1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Automation tool mixed with code generator for easier continuous development |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hidev |
6
|
|
|
* @package hidev |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2018, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hidev\log; |
12
|
|
|
|
13
|
|
|
use Psr\Log\LogLevel; |
14
|
|
|
use yii\helpers\Console; |
15
|
|
|
use yii\helpers\VarDumper; |
16
|
|
|
|
17
|
|
|
class ConsoleTarget extends \yii\log\Target |
18
|
|
|
{ |
19
|
|
|
public $exportInterval = 1; |
20
|
|
|
|
21
|
|
|
public $exportContext = [ |
22
|
|
|
LogLevel::EMERGENCY => false, |
23
|
|
|
LogLevel::ERROR => false, |
24
|
|
|
LogLevel::ALERT => false, |
25
|
|
|
LogLevel::CRITICAL => false, |
26
|
|
|
LogLevel::WARNING => false, |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
public static $styles = [ |
30
|
|
|
LogLevel::EMERGENCY => [Console::BOLD, Console::BG_RED], |
31
|
|
|
LogLevel::ERROR => [Console::FG_RED, Console::BOLD], |
32
|
|
|
LogLevel::ALERT => [Console::FG_RED], |
33
|
|
|
LogLevel::CRITICAL => [Console::FG_RED], |
34
|
|
|
LogLevel::WARNING => [Console::FG_YELLOW], |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
public function export() |
38
|
|
|
{ |
39
|
|
|
foreach ($this->messages as $message) { |
40
|
|
|
$this->out($message[0], $message[1]); |
41
|
|
|
$this->outContext($message[0], $message[2]); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function outContext($level, $context) |
46
|
|
|
{ |
47
|
|
|
if ($this->exportContext[$level] ?? false) { |
48
|
|
|
$export = VarDumper::export($context); |
49
|
|
|
Console::stdout($export . "\n"); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function out($level, $message) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
/* XXX add minimum log severity to show |
56
|
|
|
* if ($level > $this->getLevel()) { |
57
|
|
|
return; |
58
|
|
|
}*/ |
59
|
|
|
$style = self::$styles[$level]; |
60
|
|
|
if ($style) { |
61
|
|
|
$message = Console::ansiFormat($message, $style); |
62
|
|
|
} else { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
Console::stdout($message . "\n"); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function getContextMessage() |
69
|
|
|
{ |
70
|
|
|
return ''; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.