|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SlackLogEngine\Log\Engine; |
|
4
|
|
|
|
|
5
|
|
|
use Cake\Core\Configure; |
|
6
|
|
|
use Cake\Log\Engine\BaseLog; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Maknz\Slack\Client as SlackClient; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Log engine to post to slack with hook url. |
|
12
|
|
|
* |
|
13
|
|
|
* Licensed under The MIT License |
|
14
|
|
|
* For full copyright and license information, please see the LICENSE file |
|
15
|
|
|
*/ |
|
16
|
|
|
class SlackLogEngine extends BaseLog |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Instance of SlackClient class |
|
21
|
|
|
* |
|
22
|
|
|
* @var \Maknz\Slack\Client |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $_SlackClient = null; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The default config used unless overridden by runtime configuration |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $_defaultConfig = [ |
|
32
|
|
|
'types' => null, |
|
33
|
|
|
'levels' => [], |
|
34
|
|
|
'scopes' => [], |
|
35
|
|
|
'clientClass' => '\Maknz\Slack\Client' |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Configuration is valid or not |
|
40
|
|
|
* |
|
41
|
|
|
* @var boolean |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $_valid = true; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Sets protected properties based on config provided |
|
47
|
|
|
* Either `client` or `hookUrl` is required |
|
48
|
|
|
* |
|
49
|
|
|
* - `hookUrl` [string] Slack hook url. |
|
50
|
|
|
* - `client` [\Maknz\Slack\Client] Slack client instance for custom. |
|
51
|
|
|
* - `clientClass` [string(optional)] slack client class. This option is used only with `hookUrl` option. |
|
52
|
|
|
* |
|
53
|
|
|
* Other available settings can be seen at https://github.com/maknz/slack#settings |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $config Configuration array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct(array $config = []) |
|
58
|
|
|
{ |
|
59
|
|
|
parent::__construct($config); |
|
60
|
|
|
$config = $this->_config; |
|
61
|
|
|
|
|
62
|
|
|
if (isset($config['client'])) { |
|
63
|
|
|
$this->_SlackClient = $config['client']; |
|
64
|
|
|
unset($config['client']); |
|
65
|
|
|
} elseif (isset($config['hookUrl'])) { |
|
66
|
|
|
$hookUrl = $config['hookUrl']; |
|
67
|
|
|
$className = $config['clientClass']; |
|
68
|
|
|
unset($config['hookUrl'], $config['clientClass']); |
|
69
|
|
|
$this->_SlackClient = new $className($hookUrl, $config); |
|
70
|
|
|
} else { |
|
71
|
|
|
$this->_valid = false; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get slack client |
|
77
|
|
|
* |
|
78
|
|
|
* @return \Maknz\Slack\Client slack client |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getClient() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->_SlackClient; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Set slack client |
|
87
|
|
|
* |
|
88
|
|
|
* @param \Maknz\Slack\Client $client slack client |
|
89
|
|
|
* @return \Maknz\Slack\Client slack client |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setClient(SlackClient $client) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->_SlackClient = $client; |
|
94
|
|
|
$this->_valid = true; |
|
95
|
|
|
|
|
96
|
|
|
return $client; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Implements post a log to slack. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $level The severity level of the message being written. |
|
103
|
|
|
* See Cake\Log\Log::$_levels for list of possible levels. |
|
104
|
|
|
* @param string $message The message you want to log. |
|
105
|
|
|
* @param array $context Additional information about the logged message |
|
106
|
|
|
* @return bool success |
|
107
|
|
|
*/ |
|
108
|
|
|
public function log($level, $message, array $context = []) |
|
109
|
|
|
{ |
|
110
|
|
|
if (!$this->_valid) { |
|
111
|
|
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$message = $this->_format($message, $context); |
|
115
|
|
|
$output = date('Y-m-d H:i:s') . ' ' . ucfirst($level) . ': ' . $message; |
|
116
|
|
|
try { |
|
117
|
|
|
$this->_SlackClient->send($output); |
|
118
|
|
|
} catch (Exception $e) { |
|
119
|
|
|
$this->_valid = false; |
|
120
|
|
|
// Rethrow exception when debug mode |
|
121
|
|
|
if (Configure::read('debug')) { |
|
122
|
|
|
throw $e; |
|
123
|
|
|
} |
|
124
|
|
|
// otherwise DO nothing |
|
125
|
|
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return true; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|