|
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-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hidev\base; |
|
12
|
|
|
|
|
13
|
|
|
use yii\helpers\Console; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* XXX TODO REDO to: |
|
17
|
|
|
* ConsoleTarget sends selected log messages to the console stdout. |
|
18
|
|
|
* |
|
19
|
|
|
* ```php |
|
20
|
|
|
* 'components' => [ |
|
21
|
|
|
* 'log' => [ |
|
22
|
|
|
* 'targets' => [ |
|
23
|
|
|
* [ |
|
24
|
|
|
* 'class' => 'yii\log\ConsoleTarget', |
|
25
|
|
|
* 'levels' => ['error', 'warning'], |
|
26
|
|
|
* ], |
|
27
|
|
|
* ], |
|
28
|
|
|
* ], |
|
29
|
|
|
* ], |
|
30
|
|
|
* ``` |
|
31
|
|
|
*/ |
|
32
|
|
|
class Logger extends \yii\log\Logger |
|
33
|
|
|
{ |
|
34
|
|
|
public static $styles = [ |
|
35
|
|
|
self::LEVEL_WARNING => [Console::FG_YELLOW], |
|
36
|
|
|
self::LEVEL_ERROR => [Console::FG_RED], |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
public static $levels = [ |
|
40
|
|
|
'warning' => self::LEVEL_WARNING, |
|
41
|
|
|
'error' => self::LEVEL_ERROR, |
|
42
|
|
|
'quiet' => 0, |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var integer level to send log messages to stdout |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $_spamLevel; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Logs a message to console and then to yii\log\Logger. |
|
52
|
|
|
*/ |
|
53
|
3 |
|
public function log($message, $level, $category = 'application') |
|
54
|
|
|
{ |
|
55
|
3 |
|
if ($level <= $this->getSpamLevel()) { |
|
56
|
|
|
$style = self::$styles[$level]; |
|
57
|
|
|
if ($style) { |
|
58
|
|
|
$message = Console::ansiFormat($message, $style); |
|
59
|
|
|
} |
|
60
|
|
|
Console::stdout($message . "\n"); |
|
61
|
|
|
} |
|
62
|
3 |
|
parent::log($message, $level, $category); |
|
63
|
3 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Spam level getter. Default `warning` level. |
|
67
|
|
|
* @return integer |
|
68
|
|
|
*/ |
|
69
|
3 |
|
public function getSpamLevel() |
|
70
|
|
|
{ |
|
71
|
3 |
|
if ($this->_spamLevel === null) { |
|
72
|
|
|
$this->setSpamLevel('warning'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
return $this->_spamLevel; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Spam level setter. |
|
80
|
|
|
* @param integer $value |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function setSpamLevel($value) |
|
83
|
|
|
{ |
|
84
|
3 |
|
if (isset(static::$levels[$value])) { |
|
85
|
3 |
|
$value = static::$levels[$value]; |
|
86
|
|
|
} |
|
87
|
3 |
|
$this->_spamLevel = $value; |
|
88
|
3 |
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|