Completed
Pull Request — master (#7)
by Mateusz
02:42
created

MonologSysLogWriter::factory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 5.

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.

Loading history...
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');
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Monolog\Logger('application') of type object<Monolog\Logger> is incompatible with the declared type object<SilverStripe\Auditor\Monolog\Logger> of property $monolog.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
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