1
|
|
|
<?php |
2
|
|
|
namespace Germania\Logger; |
3
|
|
|
|
4
|
|
|
use Pimple\Container; |
5
|
|
|
use Pimple\ServiceProviderInterface; |
6
|
|
|
|
7
|
|
|
use Monolog\Logger; |
8
|
|
|
use Monolog\Handler\RotatingFileHandler; |
9
|
|
|
|
10
|
|
|
class FileLoggerServiceProvider implements ServiceProviderInterface |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
public $logfile = "php://stderr"; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
public $max_files = 30; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
public $loglevel = Logger::DEBUG; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string|null $logfile Logfile path |
31
|
|
|
* @param int|null $max_files Maximum number of logfiles |
32
|
|
|
* @param int|null $loglevel Monolog Loglevel constant |
33
|
|
|
*/ |
34
|
6 |
|
public function __construct(string $logfile = null, int $max_files = null, int $loglevel = null) |
35
|
|
|
{ |
36
|
6 |
|
$this->logfile = $logfile; |
37
|
|
|
|
38
|
6 |
|
if (!is_null($max_files)) { |
39
|
6 |
|
$this->max_files = $max_files; |
40
|
|
|
} |
41
|
|
|
|
42
|
6 |
|
if (!is_null($loglevel)) { |
43
|
6 |
|
$this->loglevel = $loglevel; |
44
|
|
|
} |
45
|
6 |
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Container $dic [description] |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
4 |
|
public function register(Container $dic) |
53
|
|
|
{ |
54
|
|
|
// Do nothing when no logfile is set |
55
|
4 |
|
if (empty($this->logfile)) { |
56
|
|
|
return; |
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.RotatingFileHandler']; |
73
|
2 |
|
return $handlers; |
74
|
4 |
|
}); |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return RotatingFileHandler |
79
|
|
|
*/ |
80
|
4 |
|
$dic['Monolog.Handlers.RotatingFileHandler'] = function ($dic) { |
|
|
|
|
81
|
4 |
|
$logfile = $this->logfile; |
82
|
4 |
|
$max_files = $this->max_files; |
83
|
4 |
|
$loglevel = $this->loglevel; |
84
|
|
|
|
85
|
4 |
|
return new RotatingFileHandler($logfile, $max_files, $loglevel); |
86
|
|
|
}; |
87
|
4 |
|
} |
88
|
|
|
} |
89
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.