1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the lxpgw/logger. |
5
|
|
|
* |
6
|
|
|
* (c) lichunqiang <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace lxpgw\logger; |
13
|
|
|
|
14
|
|
|
use Yii; |
15
|
|
|
use yii\log\Logger; |
16
|
|
|
use yii\log\Target; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ~~~ |
20
|
|
|
* 'log' => [ |
21
|
|
|
* 'traceLevel' => 3, |
22
|
|
|
* 'targets' => [ |
23
|
|
|
* [ |
24
|
|
|
* 'class' => LogTarget::class, |
25
|
|
|
* 'categories' => ['category1'], |
26
|
|
|
* 'exportInterval' => 1, //send every message |
27
|
|
|
* 'logVars' => [], |
28
|
|
|
* ] |
29
|
|
|
* ] |
30
|
|
|
* ] |
31
|
|
|
* ~~~. |
32
|
|
|
* |
33
|
|
|
* @version 0.0.0 |
34
|
|
|
* |
35
|
|
|
* @author lichunqaing <[email protected]> |
36
|
|
|
*/ |
37
|
|
|
class LogTarget extends Target |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var string The channel of the message send to. |
41
|
|
|
*/ |
42
|
|
|
public $channel; |
43
|
|
|
/** |
44
|
|
|
* @var string The logger title. |
45
|
|
|
*/ |
46
|
|
|
public $title; |
47
|
|
|
/** |
48
|
|
|
* @var Pubu |
49
|
|
|
*/ |
50
|
|
|
public $robot; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
1 |
|
public function init() |
56
|
|
|
{ |
57
|
1 |
|
parent::init(); |
58
|
|
|
|
59
|
1 |
|
$this->robot = Yii::createObject([ |
60
|
1 |
|
'class' => Pubu::class, |
61
|
1 |
|
'remote' => $this->channel, |
62
|
1 |
|
]); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Exports log [[messages]] to a specific destination. |
67
|
|
|
*/ |
68
|
1 |
|
public function export() |
69
|
|
|
{ |
70
|
1 |
|
$this->robot->send($this->title ?: 'Logger', $this->getAttachments()); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* ~~~ |
75
|
|
|
* [ |
76
|
|
|
* 'title' => 'this is title', |
77
|
|
|
* 'description' => 'this is description', |
78
|
|
|
* 'url' => 'http://www.baidu.com', |
79
|
|
|
* 'color' => 'success' |
80
|
|
|
* ] |
81
|
|
|
* ~~~. |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
1 |
|
private function getAttachments() |
86
|
|
|
{ |
87
|
1 |
|
$attachments = []; |
88
|
1 |
|
foreach ($this->messages as $i => $message) { |
89
|
|
|
$attachment = [ |
90
|
1 |
|
'title' => $message[2], //category as title |
91
|
1 |
|
'description' => htmlspecialchars($this->formatMessage($message)), |
92
|
1 |
|
'color' => $this->getLevelColor($message[1]), |
93
|
1 |
|
]; |
94
|
1 |
|
$attachments[] = $attachment; |
95
|
1 |
|
} |
96
|
|
|
|
97
|
1 |
|
return $attachments; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param int $level |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
1 |
|
private function getLevelColor($level) |
106
|
|
|
{ |
107
|
|
|
$colors = [ |
108
|
1 |
|
Logger::LEVEL_ERROR => 'error', |
109
|
1 |
|
Logger::LEVEL_WARNING => 'warning', |
110
|
1 |
|
Logger::LEVEL_INFO => 'info', |
111
|
1 |
|
Logger::LEVEL_PROFILE => 'primary', |
112
|
1 |
|
Logger::LEVEL_TRACE => 'muted', |
113
|
1 |
|
]; |
114
|
1 |
|
if (!isset($colors[$level])) { |
115
|
|
|
return 'success'; |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
return $colors[$level]; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|