1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dafiti\Silex; |
4
|
|
|
|
5
|
|
|
use Dafiti\Silex\Log\Factory; |
6
|
|
|
use Dafiti\Silex\Log\Logger; |
7
|
|
|
use Monolog\Handler\StreamHandler; |
8
|
|
|
use Silex\Application; |
9
|
|
|
use Silex\ServiceProviderInterface; |
10
|
|
|
|
11
|
|
|
class LoggerServiceProvider implements ServiceProviderInterface |
12
|
|
|
{ |
13
|
|
|
public function register(Application $app) |
14
|
|
|
{ |
15
|
|
|
$app['logger.class'] = '\Dafiti\Silex\Log\Logger'; |
16
|
|
|
|
17
|
|
|
$app['logger.create'] = $app->protect( |
18
|
|
|
function ($name, $level = 'debug', array $handlers = [], array $processors = []) use ($app) { |
19
|
|
|
$logger = new $app['logger.class']($name); |
20
|
|
|
$level = $logger->translateLevel($level); |
21
|
|
|
|
22
|
|
|
if (empty($handlers)) { |
23
|
|
|
$stream = sprintf('%s/%s.log', $app['logger.log_folder'], $name); |
24
|
|
|
$handlers = [ |
25
|
|
|
new StreamHandler($stream, $level, $app['logger.bubble'], $app['logger.permission']), |
26
|
|
|
]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
foreach ($handlers as $handler) { |
30
|
|
|
$logger->pushHandler($handler); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
foreach ($processors as $processor) { |
34
|
|
|
$logger->pushProcessor($processor); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$app['logger.manager']->add($logger); |
38
|
|
|
|
39
|
|
|
return $logger; |
40
|
|
|
} |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
$app['logger.handler'] = $app->protect(new Factory\Handler()); |
44
|
|
|
|
45
|
|
|
$app['logger.processor'] = $app->protect(new Factory\Processor()); |
46
|
|
|
|
47
|
|
|
$app['logger.factory'] = $app->protect( |
48
|
|
|
function (array $loggers) use ($app) { |
49
|
|
|
if (empty($loggers)) { |
50
|
|
|
throw new \InvalidArgumentException('Empty value is not allowed for loggers'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
foreach ($loggers as $name => $values) { |
54
|
|
|
$level = 'debug'; |
55
|
|
|
$handlers = []; |
56
|
|
|
$processors = []; |
57
|
|
|
|
58
|
|
|
if (isset($values['level'])) { |
59
|
|
|
$level = $values['level']; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!isset($values['handlers'])) { |
63
|
|
|
$values['handlers'] = []; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!isset($values['processors'])) { |
67
|
|
|
$values['processors'] = []; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
foreach ($values['handlers'] as $handler) { |
71
|
|
|
if (!isset($handler['level'])) { |
72
|
|
|
$handler['level'] = $level; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$handlers[] = $app['logger.handler']($handler); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
foreach ($values['processors'] as $processor) { |
79
|
|
|
$processors[] = $app['logger.processor']($processor); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$app['logger.create']($name, $level, $handlers, $processors); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$app['logger.manager'] = $app->share( |
88
|
|
|
function () { |
89
|
|
|
return new Log\Collection(); |
90
|
|
|
} |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$app['logger.bubble'] = true; |
94
|
|
|
$app['logger.permission'] = null; |
95
|
|
|
$app['logger.log_folder'] = null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @codeCoverageIgnore |
100
|
|
|
*/ |
101
|
|
|
public function boot(Application $app) |
102
|
|
|
{ |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|