1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Buttress\Concrete\Log; |
4
|
|
|
|
5
|
|
|
use League\CLImate\CLImate; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use Psr\Log\LoggerTrait; |
8
|
|
|
use Psr\Log\LogLevel; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A PSR3 logger that pushes entries out to CLImate |
12
|
|
|
*/ |
13
|
|
|
class Logger implements LoggerInterface |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
use LoggerTrait; |
17
|
|
|
|
18
|
|
|
/** @var bool Debug mode */ |
19
|
|
|
public $debug = false; |
20
|
|
|
|
21
|
|
|
/** @var \League\CLImate\CLImate */ |
22
|
|
|
protected $cli; |
23
|
|
|
|
24
|
9 |
|
public function __construct(CLImate $climate) |
25
|
|
|
{ |
26
|
9 |
|
$this->cli = $climate; |
27
|
9 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Handle logging different log levels |
31
|
|
|
* @param mixed $level |
32
|
|
|
* @param string $message |
33
|
|
|
* @param array $context |
34
|
|
|
*/ |
35
|
9 |
|
public function log($level, $message, array $context = array()) |
36
|
|
|
{ |
37
|
|
|
// Handle PSR-3 interpolation |
38
|
9 |
|
$message = $this->interpolate($message, $context); |
39
|
|
|
|
40
|
|
|
switch ($level) { |
41
|
9 |
|
case LogLevel::NOTICE: |
42
|
9 |
|
case LogLevel::INFO: |
43
|
8 |
|
case LogLevel::WARNING: |
44
|
8 |
|
case LogLevel::ALERT: |
45
|
6 |
|
return $this->handleInfo($level, $message, $context); |
46
|
6 |
|
case LogLevel::DEBUG: |
47
|
6 |
|
return $this->handleDebug($level, $message, $context); |
48
|
2 |
|
default: |
49
|
3 |
|
return $this->handleError($level, $message, $context); |
50
|
2 |
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Handle info types |
55
|
|
|
* |
56
|
|
|
* @param $level |
57
|
|
|
* @param $message |
58
|
|
|
* @param array $context |
59
|
|
|
*/ |
60
|
6 |
|
private function handleInfo($level, $message, array $context) |
|
|
|
|
61
|
|
|
{ |
62
|
6 |
|
$this->cli->output(sprintf(' - [%s] %s ', strtoupper($level), $message)); |
|
|
|
|
63
|
6 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Handle debub types |
67
|
|
|
* |
68
|
|
|
* @param $level |
69
|
|
|
* @param $message |
70
|
|
|
* @param array $context |
71
|
|
|
*/ |
72
|
6 |
|
private function handleDebug($level, $message, array $context) |
|
|
|
|
73
|
|
|
{ |
74
|
6 |
|
if ($this->debug) { |
75
|
3 |
|
$this->cli->output(sprintf(' ~ [%s] %s ', strtoupper($level), $message)); |
|
|
|
|
76
|
2 |
|
} |
77
|
6 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Handle Errors |
81
|
|
|
* |
82
|
|
|
* @param $level |
83
|
|
|
* @param $message |
84
|
|
|
* @param array $context |
85
|
|
|
*/ |
86
|
3 |
|
private function handleError($level, $message, array $context) |
|
|
|
|
87
|
|
|
{ |
88
|
3 |
|
$this->cli->error(sprintf(' ! [%s] %s ', strtoupper($level), $message)); |
89
|
3 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Interpolate context into a message. |
93
|
|
|
* This is copied directly from PSR-3 documentation |
94
|
|
|
* |
95
|
|
|
* @param $message |
96
|
|
|
* @param array $context |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
9 |
|
public function interpolate($message, array $context = array()) |
100
|
|
|
{ |
101
|
|
|
// build a replacement array with braces around the context keys |
102
|
9 |
|
$replace = array(); |
103
|
9 |
|
foreach ($context as $key => $val) { |
104
|
|
|
// check that the value can be casted to string |
105
|
3 |
|
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { |
106
|
3 |
|
$replace['{' . $key . '}'] = $val; |
107
|
2 |
|
} |
108
|
6 |
|
} |
109
|
|
|
|
110
|
|
|
// interpolate replacement values into the message and return |
111
|
9 |
|
return strtr($message, $replace); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.