Test Failed
Pull Request — master (#56)
by
unknown
02:50
created

Logger::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resque;
4
5
use \Psr\Log\AbstractLogger;
6
use \Psr\Log\LogLevel;
7
8
/**
9
 * Resque default logger PSR-3 compliant
10
 *
11
 * @package		Resque/Stat
12
 * @author		Chris Boulton <[email protected]>
13
 * @license		http://www.opensource.org/licenses/mit-license.php
14
 */
15
class Logger extends AbstractLogger
16
{
17
	public $verbose;
18
19
	public function __construct($verbose = false)
20
	{
21
		$this->verbose = $verbose;
22
	}
23
24
	/**
25
	 * Logs with an arbitrary level.
26
	 *
27
	 * @param mixed   $level    PSR-3 log level constant, or equivalent string
28
	 * @param string  $message  Message to log, may contain a { placeholder }
29
	 * @param array   $context  Variables to replace { placeholder }
30
	 * @return null
31
	 */
32
	public function log($level, $message, array $context = array())
33
	{
34
		if ($this->verbose) {
35
			fwrite(
36
				STDOUT,
37
				'[' . $level . '] [' . strftime('%T %Y-%m-%d') . '] ' . $this->interpolate($message, $context) . PHP_EOL
38
			);
39
			return;
40
		}
41
42
		if (!($level === LogLevel::INFO || $level === LogLevel::DEBUG)) {
43
			fwrite(
44
				STDOUT,
45
				'[' . $level . '] ' . $this->interpolate($message, $context) . PHP_EOL
46
			);
47
		}
48
	}
49
50
	/**
51
	 * Fill placeholders with the provided context
52
	 * @author Jordi Boggiano [email protected]
53
	 *
54
	 * @param  string  $message  Message to be logged
55
	 * @param  array   $context  Array of variables to use in message
56
	 * @return string
57
	 */
58
	public function interpolate($message, array $context = array())
59
	{
60
		// build a replacement array with braces around the context keys
61
		$replace = array();
62
		foreach ($context as $key => $val) {
63
			$replace['{' . $key . '}'] = $val;
64
		}
65
66
		// interpolate replacement values into the message and return
67
		return strtr($message, $replace);
68
	}
69
}
70