Skip to content

Commit af97af6

Browse files
mwithhelddiosmosis
andauthored
Add log handlers syslog and errorlog (matomo-org#17764)
* Add log handlers syslog and errorlog Issue#9400 * Add log handlers syslog and errorlog * syslog/errorlog: fix namespace; add log.syslog.ident * syslog/errorlog: Document options * ErrorLogHandler constructor: Avoid null Avoid null in ErrorLogHandler constructor call: Use level param; default others * missing comma * fix two di definitions Co-authored-by: diosmosis <[email protected]>
1 parent 46b2655 commit af97af6

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

config/global.ini.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
enable_logging = 0
9393

9494
[log]
95-
; possible values for log: screen, database, file
95+
; possible values for log: screen, database, file, errorlog, syslog
9696
log_writers[] = screen
9797

9898
; log level, everything logged w/ this level or one of greater severity
@@ -105,10 +105,16 @@
105105
; this allows you to log more information to one backend vs another.
106106
; log_level_screen =
107107
; log_level_file =
108+
; log_level_errorlog =
109+
; log_level_syslog =
108110

109111
; if configured to log in a file, log entries will be made to this file
110112
logger_file_path = tmp/logs/matomo.log
111113

114+
; if configured to log to syslog, mark them with this identifier string.
115+
; This acts as an easy-to-find tag in the syslog.
116+
logger_syslog_ident = 'matomo'
117+
112118
[Cache]
113119
; available backends are 'file', 'array', 'null', 'redis', 'chained'
114120
; 'array' will cache data only during one request

core/Log.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@
3838
* The following configuration options can be set:
3939
*
4040
* - `log_writers[]`: This is an array of log writer IDs. The three log writers provided
41-
* by Piwik core are **file**, **screen** and **database**. You can
42-
* get more by installing plugins. The default value is **screen**.
41+
* by Piwik core are **file**, **screen**, **database**, **errorlog**,
42+
* and **syslog**. You can get more by installing plugins. The default
43+
* value is **screen**.
4344
* - `log_level`: The current log level. Can be **ERROR**, **WARN**, **INFO**, **DEBUG**,
4445
* or **VERBOSE**. Log entries made with a log level that is as or more
4546
* severe than the current log level will be outputted. Others will be
@@ -48,6 +49,9 @@
4849
* to log to or a path to a directory to store logs in. If a
4950
* directory, the file name is piwik.log. Can be relative to
5051
* Piwik's root dir or an absolute path. Defaults to **tmp/logs**.
52+
* - `logger_syslog_ident`: If configured to log to syslog, mark them with this
53+
* identifier string. This acts as an easy-to-find tag in
54+
* the syslog.
5155
*
5256
*
5357
* @deprecated Inject and use Psr\Log\LoggerInterface instead of this class.

plugins/Monolog/config/config.php

+38
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
'file' => 'Piwik\Plugins\Monolog\Handler\FileHandler',
1818
'screen' => 'Piwik\Plugins\Monolog\Handler\WebNotificationHandler',
1919
'database' => 'Piwik\Plugins\Monolog\Handler\DatabaseHandler',
20+
'errorlog' => '\Monolog\Handler\ErrorLogHandler',
21+
'syslog' => '\Monolog\Handler\SyslogHandler',
2022
),
2123
'log.handlers' => DI\factory(function (\DI\Container $c) {
2224
if ($c->has('ini.log.log_writers')) {
@@ -97,6 +99,14 @@
9799
'Piwik\Plugins\Monolog\Handler\FileHandler' => DI\create()
98100
->constructor(DI\get('log.file.filename'), DI\get('log.level.file'))
99101
->method('setFormatter', DI\get('log.lineMessageFormatter.file')),
102+
103+
'\Monolog\Handler\ErrorLogHandler' => DI\autowire()
104+
->constructorParameter('level', DI\get('log.level.errorlog'))
105+
->method('setFormatter', DI\get('log.lineMessageFormatter.file')),
106+
107+
'\Monolog\Handler\SyslogHandler' => DI\create()
108+
->constructor(DI\get('log.syslog.ident'), 'syslog', DI\get('log.level.syslog'))
109+
->method('setFormatter', DI\get('log.lineMessageFormatter.file')),
100110

101111
'Piwik\Plugins\Monolog\Handler\DatabaseHandler' => DI\create()
102112
->constructor(DI\get('log.level.database'))
@@ -147,6 +157,26 @@
147157
return $c->get('log.level');
148158
}),
149159

160+
'log.level.syslog' => DI\factory(function (ContainerInterface $c) {
161+
if ($c->has('ini.log.log_level_syslog')) {
162+
$level = Log::getMonologLevelIfValid($c->get('ini.log.log_level_syslog'));
163+
if ($level !== null) {
164+
return $level;
165+
}
166+
}
167+
return $c->get('log.level');
168+
}),
169+
170+
'log.level.errorlog' => DI\factory(function (ContainerInterface $c) {
171+
if ($c->has('ini.log.log_level_errorlog')) {
172+
$level = Log::getMonologLevelIfValid($c->get('ini.log.log_level_errorlog'));
173+
if ($level !== null) {
174+
return $level;
175+
}
176+
}
177+
return $c->get('log.level');
178+
}),
179+
150180
'log.file.filename' => DI\factory(function (ContainerInterface $c) {
151181
$logPath = $c->get('ini.log.logger_file_path');
152182

@@ -172,6 +202,14 @@
172202

173203
return $logPath;
174204
}),
205+
206+
'log.syslog.ident' => DI\factory(function (ContainerInterface $c) {
207+
$ident = $c->get('ini.log.logger_syslog_ident');
208+
if (empty($ident)) {
209+
$ident = 'matomo';
210+
}
211+
return $ident;
212+
}),
175213

176214
'Piwik\Plugins\Monolog\Formatter\LineMessageFormatter' => DI\create('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter')
177215
->constructor(DI\get('log.short.format')),

0 commit comments

Comments
 (0)