-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.php
169 lines (131 loc) · 4.47 KB
/
Logger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace FS\Log4PhpBundle;
use FS\Log4PhpBundle\Wrapper\LoggerMDC;
use FS\Log4PhpBundle\Wrapper\LoggerNDC;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use FS\Log4PhpBundle\Wrapper\Logger as Log4phpLogger;
/**
*
* @author Florian Semm
*/
class Logger implements ApplicationLoggerInterface {
/**
*
* @var LoggerRoot
*/
private $logger = null;
/**
*
* @var array
*/
private $messages = array();
private $container;
public function __construct($container) {
$this->container = $container;
}
public function configureLogger(array $config) {
Log4phpLogger::configure($config);
$this->logger = Log4phpLogger::getRootLogger();
}
/**
*
* @param array $context
* @return \Logger
*/
private function getLogger(array $context = array()) {
$logger = $this->logger;
if (array_key_exists('logger', $context)) {
$logger = Log4phpLogger::getLogger($context['logger']);
}
return $logger;
}
/**
* @return LoggerMDC
*/
public function getLoggerMDC() {
return new LoggerMDC();
}
/**
* @return LoggerNDC
*/
public function getLoggerNDC() {
return new LoggerNDC();
}
/**
*
* @param string $appenderName
* @param string $logger
*/
public function addAppender($appenderId, $logger = '') {
if (!$this->logger instanceof \Logger) {
return;
}
$context = array();
if ($logger != '') {
$context['app'] = $logger;
}
$logger = $this->getLogger($context);
$logger->addAppender($this->container->get($appenderId));
}
private function addMessage($message, $priority, $loggerName = 'root') {
$message = array(
'timestamp' => time(),
'message' => $message,
'priority' => $priority,
'priorityName' => $priority,
'context' => $loggerName,
);
if (false == array_key_exists($priority, $this->messages)) {
$this->messages[$priority] = array();
}
$messagesOfPrio = $this->messages[$priority];
$messagesOfPrio[] = $message;
$this->messages[$priority] = $messagesOfPrio;
ksort($this->messages);
}
public function alert($message, array $context = array()) {
$logger = $this->getLogger($context);
$this->addMessage($message, 'alert', $logger->getName());
$logger->error($message);
}
public function crit($message, array $context = array()) {
$logger = $this->getLogger($context);
$this->addMessage($message, 'critical', $logger->getName());
$logger->fatal($message);
}
public function debug($message, array $context = array()) {
$logger = $this->getLogger($context);
$this->addMessage($message, 'debug', $logger->getName());
$logger->debug($message);
}
public function emerg($message, array $context = array()) {
$logger = $this->getLogger($context);
$this->addMessage($message, 'emerg', $logger->getName());
$logger->fatal($message);
}
public function err($message, array $context = array()) {
$logger = $this->getLogger($context);
$this->addMessage($message, 'error', $logger->getName());
$logger->error($message);
}
public function info($message, array $context = array()) {
$logger = $this->getLogger($context);
$this->addMessage($message, 'info', $logger->getName());
$logger->info($message);
}
public function notice($message, array $context = array()) {
$logger = $this->getLogger($context);
$this->addMessage($message, 'notice', $logger->getName());
$logger->info($message);
}
public function warn($message, array $context = array()) {
$logger = $this->getLogger($context);
$this->addMessage($message, 'warning', $logger->getName());
$logger->warn($message);
}
public function getLogs() {
return $this->messages;
}
}
?>