1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stats\Providers; |
4
|
|
|
|
5
|
|
|
use Joomla\DI\Container; |
6
|
|
|
use Joomla\DI\ServiceProviderInterface; |
7
|
|
|
use Monolog\Handler\StreamHandler; |
8
|
|
|
use Monolog\Logger; |
9
|
|
|
use Monolog\Processor\PsrLogMessageProcessor; |
10
|
|
|
use Monolog\Processor\WebProcessor; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Monolog service provider |
14
|
|
|
* |
15
|
|
|
* @since 1.0 |
16
|
|
|
*/ |
17
|
|
|
class MonologServiceProvider implements ServiceProviderInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Registers the service provider with a DI container. |
21
|
|
|
* |
22
|
|
|
* @param Container $container The DI container. |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
* |
26
|
|
|
* @since 1.0 |
27
|
|
|
*/ |
28
|
1 |
|
public function register(Container $container) |
29
|
|
|
{ |
30
|
|
|
// Register the PSR-3 processor |
31
|
1 |
|
$container->share('monolog.processor.psr3', [$this, 'getMonologProcessorPsr3Service'], true); |
32
|
|
|
|
33
|
|
|
// Register the web processor |
34
|
1 |
|
$container->share('monolog.processor.web', [$this, 'getMonologProcessorWebService'], true); |
35
|
|
|
|
36
|
|
|
// Register the web application handler |
37
|
1 |
|
$container->share('monolog.handler.application', [$this, 'getMonologHandlerApplicationService'], true); |
38
|
|
|
|
39
|
|
|
// Register the database handler |
40
|
1 |
|
$container->share('monolog.handler.database', [$this, 'getMonologHandlerDatabaseService'], true); |
41
|
|
|
|
42
|
|
|
// Register the web application Logger |
43
|
1 |
|
$container->share('monolog.logger.application', [$this, 'getMonologLoggerApplicationService'], true); |
44
|
|
|
|
45
|
|
|
// Register the CLI application Logger |
46
|
1 |
|
$container->share('monolog.logger.cli', [$this, 'getMonologLoggerCliService'], true); |
47
|
|
|
|
48
|
|
|
// Register the database Logger |
49
|
1 |
|
$container->share('monolog.logger.database', [$this, 'getMonologLoggerDatabaseService'], true); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the `monolog.processor.psr3` service |
54
|
|
|
* |
55
|
|
|
* @param Container $container The DI container. |
56
|
|
|
* |
57
|
|
|
* @return PsrLogMessageProcessor |
58
|
|
|
* |
59
|
|
|
* @since 1.0 |
60
|
|
|
*/ |
61
|
1 |
|
public function getMonologProcessorPsr3Service(Container $container) |
|
|
|
|
62
|
|
|
{ |
63
|
1 |
|
return new PsrLogMessageProcessor; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the `monolog.processor.web` service |
68
|
|
|
* |
69
|
|
|
* @param Container $container The DI container. |
70
|
|
|
* |
71
|
|
|
* @return WebProcessor |
72
|
|
|
* |
73
|
|
|
* @since 1.0 |
74
|
|
|
*/ |
75
|
1 |
|
public function getMonologProcessorWebService(Container $container) |
|
|
|
|
76
|
|
|
{ |
77
|
1 |
|
return new WebProcessor; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the `monolog.handler.application` service |
82
|
|
|
* |
83
|
|
|
* @param Container $container The DI container. |
84
|
|
|
* |
85
|
|
|
* @return StreamHandler |
86
|
|
|
* |
87
|
|
|
* @since 1.0 |
88
|
|
|
*/ |
89
|
1 |
View Code Duplication |
public function getMonologHandlerApplicationService(Container $container) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
/** @var \Joomla\Registry\Registry $config */ |
92
|
1 |
|
$config = $container->get('config'); |
93
|
|
|
|
94
|
1 |
|
$level = strtoupper($config->get('log.application', $config->get('log.level', 'error'))); |
95
|
|
|
|
96
|
1 |
|
return new StreamHandler(APPROOT . '/logs/stats.log', constant('\\Monolog\\Logger::' . $level)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the `monolog.handler.database` service |
101
|
|
|
* |
102
|
|
|
* @param Container $container The DI container. |
103
|
|
|
* |
104
|
|
|
* @return StreamHandler |
105
|
|
|
* |
106
|
|
|
* @since 1.0 |
107
|
|
|
*/ |
108
|
1 |
View Code Duplication |
public function getMonologHandlerDatabaseService(Container $container) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
/** @var \Joomla\Registry\Registry $config */ |
111
|
1 |
|
$config = $container->get('config'); |
112
|
|
|
|
113
|
|
|
// If database debugging is enabled then force the logger's error level to DEBUG, otherwise use the level defined in the app config |
114
|
1 |
|
$level = $config->get('database.debug', false) ? 'DEBUG' : strtoupper($config->get('log.database', $config->get('log.level', 'error'))); |
115
|
|
|
|
116
|
1 |
|
return new StreamHandler(APPROOT . '/logs/stats.log', constant('\\Monolog\\Logger::' . $level)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the `monolog.logger.application` service |
121
|
|
|
* |
122
|
|
|
* @param Container $container The DI container. |
123
|
|
|
* |
124
|
|
|
* @return Logger |
125
|
|
|
* |
126
|
|
|
* @since 1.0 |
127
|
|
|
*/ |
128
|
1 |
|
public function getMonologLoggerApplicationService(Container $container) |
129
|
|
|
{ |
130
|
1 |
|
return new Logger( |
131
|
1 |
|
'Application', |
132
|
|
|
[ |
133
|
1 |
|
$container->get('monolog.handler.application') |
134
|
1 |
|
], |
135
|
|
|
[ |
|
|
|
|
136
|
1 |
|
$container->get('monolog.processor.web') |
137
|
1 |
|
] |
138
|
1 |
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the `monolog.logger.cli` service |
143
|
|
|
* |
144
|
|
|
* @param Container $container The DI container. |
145
|
|
|
* |
146
|
|
|
* @return Logger |
147
|
|
|
* |
148
|
|
|
* @since 1.0 |
149
|
|
|
*/ |
150
|
1 |
|
public function getMonologLoggerCliService(Container $container) |
151
|
|
|
{ |
152
|
1 |
|
return new Logger( |
153
|
1 |
|
'Application', |
154
|
|
|
[ |
155
|
1 |
|
$container->get('monolog.handler.application') |
156
|
1 |
|
] |
157
|
1 |
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the `monolog.logger.database` service |
162
|
|
|
* |
163
|
|
|
* @param Container $container The DI container. |
164
|
|
|
* |
165
|
|
|
* @return Logger |
166
|
|
|
* |
167
|
|
|
* @since 1.0 |
168
|
|
|
*/ |
169
|
1 |
|
public function getMonologLoggerDatabaseService(Container $container) |
170
|
|
|
{ |
171
|
1 |
|
return new Logger( |
172
|
1 |
|
'Application', |
173
|
|
|
[ |
174
|
1 |
|
$container->get('monolog.handler.database') |
175
|
1 |
|
], |
176
|
|
|
[ |
|
|
|
|
177
|
1 |
|
$container->get('monolog.processor.psr3'), |
178
|
1 |
|
$container->get('monolog.processor.web') |
179
|
1 |
|
] |
180
|
1 |
|
); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.