1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Auditor; |
4
|
|
|
|
5
|
|
|
require_once BASE_PATH . '/framework/thirdparty/Zend/Log/Writer/Abstract.php'; |
6
|
|
|
require_once BASE_PATH . '/vendor/autoload.php'; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Alternative monolog writer for SS_Log, for use when auditor module is in operation. |
10
|
|
|
* |
11
|
|
|
* Supplied, because there cannot be more than one SS_SysLogWriter has problems writing |
12
|
|
|
* to more than one facility at the same time. See this module's readme for more information. |
13
|
|
|
*/ |
14
|
|
|
class MonologSysLogWriter extends \Zend_Log_Writer_Abstract { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array Convert Zend_Log level to Monolog level. |
18
|
|
|
*/ |
19
|
|
|
protected $priorities = array( |
20
|
|
|
\Zend_Log::EMERG => \Monolog\Logger::EMERGENCY, |
21
|
|
|
\Zend_Log::ALERT => \Monolog\Logger::ALERT, |
22
|
|
|
\Zend_Log::CRIT => \Monolog\Logger::CRITICAL, |
23
|
|
|
\Zend_Log::ERR => \Monolog\Logger::ERROR, |
24
|
|
|
\Zend_Log::WARN => \Monolog\Logger::WARNING, |
25
|
|
|
\Zend_Log::NOTICE => \Monolog\Logger::NOTICE, |
26
|
|
|
\Zend_Log::INFO => \Monolog\Logger::INFO, |
27
|
|
|
\Zend_Log::DEBUG => \Monolog\Logger::DEBUG, |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Monolog\Logger |
32
|
|
|
*/ |
33
|
|
|
protected $monolog; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $ident String identifying the application within the syslog stream. |
37
|
|
|
* @param $facility Syslog facility. |
38
|
|
|
*/ |
39
|
|
|
public function __construct($ident = 'SilverStripe_log', $facility = LOG_USER) { |
40
|
|
|
$this->monolog = new \Monolog\Logger('application'); |
|
|
|
|
41
|
|
|
$syslog = new \Monolog\Handler\SyslogHandler($ident, $facility, \Monolog\Logger::DEBUG); |
42
|
|
|
$formatter = new \Monolog\Formatter\LineFormatter("%level_name%: %message% %context%"); |
43
|
|
|
$syslog->setFormatter($formatter); |
44
|
|
|
$this->monolog->pushHandler($syslog); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
static public function factory($config) { |
|
|
|
|
48
|
|
|
return new MonologSysLogWriter(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function _write($event) { |
52
|
|
|
if (!empty($this->priorities[$event['priority']])) { |
53
|
|
|
$level = $this->priorities[$event['priority']]; |
54
|
|
|
} else { |
55
|
|
|
$level = \Monolog\Logger::INFO; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$message = sprintf( |
59
|
|
|
'%s (line %s in %s)', |
60
|
|
|
$event['message']['errstr'], |
61
|
|
|
$event['message']['errline'], |
62
|
|
|
$event['message']['errfile'] |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$context = []; |
66
|
|
|
if (!empty($event['info'])) { |
67
|
|
|
$context = $event['info']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->monolog->log($level, $message, $context); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.