This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
MonologServiceProvider.php
148 lines (118 loc) · 4.7 KB
/
MonologServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Provider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Logger;
use Monolog\Handler;
use Monolog\ErrorHandler;
use Silex\Application;
use Silex\Api\BootableProviderInterface;
use Silex\Api\EventListenerProviderInterface;
use Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategy;
use Symfony\Bridge\Monolog\Processor\DebugProcessor;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Silex\EventListener\LogListener;
/**
* Monolog Provider.
*
* @author Fabien Potencier <[email protected]>
*/
class MonologServiceProvider implements ServiceProviderInterface, BootableProviderInterface, EventListenerProviderInterface
{
public function register(Container $app)
{
$app['logger'] = function () use ($app) {
return $app['monolog'];
};
if ($bridge = class_exists('Symfony\Bridge\Monolog\Logger')) {
if (isset($app['request_stack'])) {
$app['monolog.not_found_activation_strategy'] = function () use ($app) {
$level = MonologServiceProvider::translateLevel($app['monolog.level']);
return new NotFoundActivationStrategy($app['request_stack'], ['^/'], $level);
};
}
}
$app['monolog.logger.class'] = $bridge ? 'Symfony\Bridge\Monolog\Logger' : 'Monolog\Logger';
$app['monolog'] = function ($app) use ($bridge) {
$log = new $app['monolog.logger.class']($app['monolog.name']);
$handler = new Handler\GroupHandler($app['monolog.handlers']);
if (isset($app['monolog.not_found_activation_strategy'])) {
$handler = new Handler\FingersCrossedHandler($handler, $app['monolog.not_found_activation_strategy']);
}
$log->pushHandler($handler);
if ($app['debug'] && $bridge) {
$log->pushProcessor(new DebugProcessor());
}
return $log;
};
$app['monolog.formatter'] = function () {
return new LineFormatter();
};
$app['monolog.handler'] = $defaultHandler = function () use ($app) {
$level = MonologServiceProvider::translateLevel($app['monolog.level']);
$handler = new Handler\StreamHandler($app['monolog.logfile'], $level, $app['monolog.bubble'], $app['monolog.permission']);
$handler->setFormatter($app['monolog.formatter']);
return $handler;
};
$app['monolog.handlers'] = function () use ($app, $defaultHandler) {
$handlers = [];
// enables the default handler if a logfile was set or the monolog.handler service was redefined
if ($app['monolog.logfile'] || $defaultHandler !== $app->raw('monolog.handler')) {
$handlers[] = $app['monolog.handler'];
}
return $handlers;
};
$app['monolog.level'] = function () {
return Logger::DEBUG;
};
$app['monolog.listener'] = function () use ($app) {
return new LogListener($app['logger'], $app['monolog.exception.logger_filter']);
};
$app['monolog.name'] = 'app';
$app['monolog.bubble'] = true;
$app['monolog.permission'] = null;
$app['monolog.exception.logger_filter'] = null;
$app['monolog.logfile'] = null;
$app['monolog.use_error_handler'] = function ($app) {
return !$app['debug'];
};
}
public function boot(Application $app)
{
if ($app['monolog.use_error_handler']) {
ErrorHandler::register($app['monolog']);
}
}
public function subscribe(Container $app, EventDispatcherInterface $dispatcher)
{
if (isset($app['monolog.listener'])) {
$dispatcher->addSubscriber($app['monolog.listener']);
}
}
public static function translateLevel($name)
{
// level is already translated to logger constant, return as-is
if (is_int($name)) {
return $name;
}
$psrLevel = Logger::toMonologLevel($name);
if (is_int($psrLevel)) {
return $psrLevel;
}
$levels = Logger::getLevels();
$upper = strtoupper($name);
if (!isset($levels[$upper])) {
throw new \InvalidArgumentException("Provided logging level '$name' does not exist. Must be a valid monolog logging level.");
}
return $levels[$upper];
}
}