|
1
|
|
|
<?php |
|
2
|
|
|
namespace Germania\Logger; |
|
3
|
|
|
|
|
4
|
|
|
use Pimple\Container; |
|
5
|
|
|
use Pimple\ServiceProviderInterface; |
|
6
|
|
|
|
|
7
|
|
|
use Monolog\Logger; |
|
8
|
|
|
use Monolog\Handler\SlackHandler; |
|
9
|
|
|
|
|
10
|
|
|
class SlackLoggerServiceProvider implements ServiceProviderInterface |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
public $token; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
public $channel; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
public $username; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
public $loglevel = Logger::CRITICAL; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $token Slack token |
|
36
|
|
|
* @param string $channel Slack channel |
|
37
|
|
|
* @param string $username Slack username |
|
38
|
|
|
* @param int|null $loglevel Monolog Loglevel constant |
|
39
|
|
|
*/ |
|
40
|
6 |
|
public function __construct(string $token, string $channel, string $username, int $loglevel = null) |
|
41
|
|
|
{ |
|
42
|
6 |
|
$this->token = $token; |
|
43
|
6 |
|
$this->channel = $channel; |
|
44
|
6 |
|
$this->username = $username; |
|
45
|
|
|
|
|
46
|
6 |
|
if (!is_null($loglevel)) { |
|
47
|
6 |
|
$this->loglevel = $loglevel; |
|
48
|
|
|
} |
|
49
|
6 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param Container $dic [description] |
|
54
|
|
|
* @return void |
|
55
|
|
|
*/ |
|
56
|
4 |
|
public function register(Container $dic) |
|
57
|
|
|
{ |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
// Make sure there's a 'Monolog.Handlers' service |
|
61
|
4 |
|
if (!$dic->offsetExists('Monolog.Handlers')) : |
|
62
|
4 |
|
$dic['Monolog.Handlers'] = function ($dic) { |
|
|
|
|
|
|
63
|
2 |
|
return array(); |
|
64
|
|
|
}; |
|
65
|
|
|
endif; |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
4 |
|
$dic->extend('Monolog.Handlers', function (array $handlers, $dic) { |
|
72
|
2 |
|
$handlers[] = $dic['Monolog.Handlers.SlackHandler']; |
|
73
|
2 |
|
return $handlers; |
|
74
|
4 |
|
}); |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Send log messages to Slack. |
|
80
|
|
|
* |
|
81
|
|
|
* - https://blog.tschelhas.de/symfony/slack-als-logger-fuer-symfony-nutzen/ |
|
82
|
|
|
* - https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/SlackHandler.php |
|
83
|
|
|
* |
|
84
|
|
|
* @return SlackHandler |
|
85
|
|
|
*/ |
|
86
|
4 |
|
$dic['Monolog.Handlers.SlackHandler'] = function ($dic) { |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
// As hardcoded in SlackHandler class |
|
89
|
4 |
|
$useAttachment = true; // Whether the message should be added to Slack as attachment (plain text otherwise) |
|
90
|
4 |
|
$iconEmoji = null; // The emoji name to use (or null) |
|
91
|
4 |
|
$loglevel = Logger::CRITICAL; // The minimum logging level at which this handler will be triggered |
|
|
|
|
|
|
92
|
4 |
|
$bubble = true; // Whether the messages that are handled can bubble up the stack or not |
|
93
|
4 |
|
$useShortAttachment = false; // Whether the the context/extra messages added to Slack as attachments are in a short style |
|
|
|
|
|
|
94
|
4 |
|
$includeContextAndExtra = false; // Whether the attachment should include context and extra data |
|
|
|
|
|
|
95
|
4 |
|
$excludeFields = array(); // Dot separated list of fields to exclude from slack message. E.g. ['context.field1', 'extra.field2'] |
|
96
|
|
|
|
|
97
|
|
|
// Override with custom values |
|
98
|
4 |
|
$loglevel = $this->loglevel; // Override for custom loglevel |
|
99
|
4 |
|
$includeContextAndExtra = true; // Show details in Slack |
|
100
|
4 |
|
$useShortAttachment = true; // Show details in Slack as compact JSON code boxes |
|
101
|
|
|
|
|
102
|
|
|
// Slack credentials |
|
103
|
4 |
|
$token = $this->token; |
|
104
|
4 |
|
$channel = $this->channel; |
|
105
|
4 |
|
$username = $this->username; |
|
106
|
|
|
|
|
107
|
4 |
|
return new SlackHandler($token, $channel, $username, $useAttachment, $iconEmoji, $loglevel, $bubble, $useShortAttachment, $includeContextAndExtra, $excludeFields); |
|
108
|
|
|
}; |
|
109
|
4 |
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.