SysLogger::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/*
3
 * This file is part of Yolk - Gamer Network's PHP Framework.
4
 *
5
 * Copyright (c) 2014 Gamer Network Ltd.
6
 * 
7
 * Distributed under the MIT License, a copy of which is available in the
8
 * LICENSE file that was bundled with this package, or online at:
9
 * https://github.com/gamernetwork/yolk-logger
10
 */
11
12
namespace yolk\log\adapter;
13
14
use yolk\log\BaseLogger;
15
use yolk\log\LogLevel;
16
17
/**
18
 * Provides logging to syslog.
19
 */
20
class SysLogger extends BaseLogger {
21
	
22
	/**
23
	 * Value prefixed to all messages.
24
	 * @var string
25
	 */
26
	protected $prefix;
27
	
28
	/**
29
	 * Create a syslogger.
30
	 * @param string $prefix   value prefixed to all message.
31
	 */
32
	public function __construct( $prefix = '' ) {
33
		parent::__construct();
34
		$prefix = trim($prefix);
35
		$this->prefix = $prefix ? $prefix. ': ' : '';
36
	}
37
	
38
	protected function output( $level, $msg ) {
39
40
		// map logging levels to syslog priority levels
41
		$priorities = array(
42
			LogLevel::EMERGENCY => LOG_EMERG,
43
			LogLevel::ALERT     => LOG_ALERT,
44
			LogLevel::CRITICAL  => LOG_CRIT,
45
			LogLevel::ERROR     => LOG_ERR,
46
			LogLevel::WARNING   => LOG_WARNING,
47
			LogLevel::NOTICE    => LOG_NOTICE,
48
			LogLevel::INFO      => LOG_INFO,
49
			LogLevel::DEBUG     => LOG_DEBUG,
50
		);
51
52
		$priority = isset($priorities[$level]) ? $priorities[$level] : LOG_INFO;
53
54
		syslog($priority, $this->prefix. $msg);
55
56
	}
57
   
58
}
59
60
// EOF