1
|
|
|
<?php |
2
|
|
|
namespace Germania\Logger; |
3
|
|
|
|
4
|
|
|
use Pimple\Container; |
5
|
|
|
use Pimple\ServiceProviderInterface; |
6
|
|
|
|
7
|
|
|
use Monolog\Logger; |
8
|
|
|
use Monolog\Formatter\HtmlFormatter; |
9
|
|
|
use CMDISP\MonologMicrosoftTeams\TeamsLogHandler; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class TeamsLoggerServiceProvider implements ServiceProviderInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
public $incoming_webook_url; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
public $loglevel = Logger::INFO; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $incoming_webook_url Incoming Webhook URL, leave empty to disable |
28
|
6 |
|
* @param int|null $loglevel Monolog loglevel number |
29
|
|
|
*/ |
30
|
6 |
|
public function __construct(string $incoming_webook_url = null, int $loglevel = null) |
31
|
6 |
|
{ |
32
|
6 |
|
$this->incoming_webook_url = $incoming_webook_url; |
33
|
|
|
$this->loglevel = $loglevel ?: Logger::INFO; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Container $dic [description] |
39
|
4 |
|
* @return void |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function register(Container $dic) |
|
|
|
|
42
|
|
|
{ |
43
|
4 |
|
|
44
|
|
|
// Do nothing when no incoming_webook_url is set |
45
|
|
|
if (empty($this->incoming_webook_url)) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
4 |
|
|
50
|
4 |
|
// Make sure there's a 'Monolog.Handlers' service |
51
|
2 |
|
if (!$dic->offsetExists('Monolog.Handlers')) : |
52
|
|
|
$dic['Monolog.Handlers'] = function ($dic) { |
|
|
|
|
53
|
|
|
return array(); |
54
|
|
|
}; |
55
|
|
|
endif; |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
4 |
|
* @return array |
60
|
2 |
|
*/ |
61
|
2 |
|
$dic->extend('Monolog.Handlers', function (array $handlers, $dic) { |
62
|
4 |
|
$handlers[] = $dic['Monolog.Handlers.TeamsHandler']; |
63
|
|
|
return $handlers; |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Send log messages to Microsoft Teams. |
70
|
|
|
* |
71
|
4 |
|
* @return SlackHandler |
72
|
4 |
|
*/ |
73
|
4 |
|
$dic['Monolog.Handlers.TeamsHandler'] = function ($dic) { |
|
|
|
|
74
|
4 |
|
$th = new TeamsLogHandler($this->incoming_webook_url, $this->loglevel); |
75
|
|
|
$th->setFormatter(new HtmlFormatter); |
76
|
4 |
|
return $th; |
77
|
|
|
}; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
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.