|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Subreality\Dilmun\Nabu; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\AbstractLogger; |
|
6
|
|
|
use Psr\Log\InvalidArgumentException; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use Subreality\Dilmun\Nabu\LoggerHandler\HandlerInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* PSR-3 logger implementation for logging RFC-5424 log-level messages |
|
12
|
|
|
* |
|
13
|
|
|
* 0 Emergency: system is unusable |
|
14
|
|
|
* 1 Alert: action must be taken immediately |
|
15
|
|
|
* 2 Critical: critical conditions |
|
16
|
|
|
* 3 Error: error conditions |
|
17
|
|
|
* 4 Warning: warning conditions |
|
18
|
|
|
* 5 Notice: normal but significant condition |
|
19
|
|
|
* 6 Informational: informational messages |
|
20
|
|
|
* 7 Debug: debug-level messages |
|
21
|
|
|
* |
|
22
|
|
|
* @see http://www.php-fig.org/psr/psr-3/ |
|
23
|
|
|
* @see http://tools.ietf.org/html/rfc5424 |
|
24
|
|
|
*/ |
|
25
|
|
|
class Logger extends AbstractLogger implements LoggerInterface |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
private $handler; |
|
29
|
|
|
private static $valid_levels = array( |
|
30
|
|
|
"EMERGENCY", |
|
31
|
|
|
"ALERT", |
|
32
|
|
|
"CRITICAL", |
|
33
|
|
|
"ERROR", |
|
34
|
|
|
"WARNING", |
|
35
|
|
|
"NOTICE", |
|
36
|
|
|
"INFO", |
|
37
|
|
|
"DEBUG" |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param HandlerInterface $handler Sets the handler for logging (e.g. File handler). |
|
42
|
|
|
*/ |
|
43
|
3 |
|
public function __construct(HandlerInterface $handler) |
|
44
|
|
|
{ |
|
45
|
3 |
|
$this->handler = $handler; |
|
46
|
3 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return HandlerInterface |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function getHandler() |
|
52
|
|
|
{ |
|
53
|
1 |
|
return $this->handler; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Logs with an arbitrary level. |
|
58
|
|
|
* |
|
59
|
|
|
* @see http://www.php-fig.org/psr/psr-3/ |
|
60
|
|
|
* |
|
61
|
|
|
* @param mixed $level |
|
62
|
|
|
* @param string $message |
|
63
|
|
|
* @param array $context |
|
64
|
|
|
* |
|
65
|
|
|
* @return string Returns the text of the log entry. |
|
66
|
|
|
*/ |
|
67
|
16 |
|
public function log($level, $message, array $context = array()) |
|
68
|
|
|
{ |
|
69
|
16 |
|
StaticLogger::setHandler($this->handler); |
|
70
|
|
|
|
|
71
|
16 |
|
$u_level = strtoupper($level); |
|
72
|
|
|
|
|
73
|
16 |
|
if (!in_array($u_level, self::$valid_levels)) { |
|
74
|
1 |
|
throw new InvalidArgumentException("Unknown log level"); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
15 |
|
$log_entry = StaticLogger::log($level, $message, $context); |
|
78
|
|
|
|
|
79
|
15 |
|
return $log_entry; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|