This repository has been archived by the owner on May 14, 2019. It is now read-only.
forked from bermi/akelos
-
Notifications
You must be signed in to change notification settings - Fork 10
/
logger.php
448 lines (404 loc) · 15.2 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
# This file is part of the Akelos Framework
# (Copyright) 2004-2010 Bermi Ferrer bermi a t bermilabs com
# See LICENSE and CREDITS for details
/**
* Loggin in your Akelos applications
*
* Akelos provides a flexible loggin system which includes five loggin levels
* and four log handlers.
*
* In order to log events you'll have to define in your configuration file:
*
* define('AK_LOG_EVENTS', true);
*
* Then you can get a singleton copy of a logger prepared for current
* environment by calling:
*
* Ak::getLogger();
*
* which will log to ./log/development.log if the environment id development.
*
* You can get loggers under a different namespace by calling:
*
* Ak::getLogger('my_app');
*
* and the output for that logger will be saved in:
*
* ./log/my_app.log
*
* Once you have an instance of your logger you can emit these events:
*
* * debug => file
* * info => file
* * warn => file, display,
* * error => file, display, mail,
* * fatal => file, display, mail, fatal
*
* All error levels have the same interface
*
* Ak::getLogger()->debug('Debuggin foo', optional_array)
*
* where +optional_array+ will be converted into json and added to the log
*
*
* You can customize handlers for each level by setting the
* +handlers_for_levels+ option in your configuration like:
*
* AkConfig::setOption('handlers_for_levels',= array(
* 'debug' => array('file'),
* 'info' => array('file'),
* 'warn' => array('file', 'display'),
* 'error' => array('file', 'display', 'mail'),
* 'fatal' => array('file', 'display', 'mail', 'fatal'),
* ));
*
* ## Log handlers:
*
* ### file
*
* Files are logged by default to ./log (AK_LOG_DIR) and upon the
* environment, so while you're on developing your app you can find it at:
*
* ./log/development.log
*
* No file will be written into the log directory if the user does not have
* the right set of permissions.
*
* ### display
*
* Messages will be rendered as html and will show the file and line where the
* log was requested.
*
* The display handler is disabled on production and testing modes.
*
* ### mail
*
* You can get a mail of critical issues when running on production mode.
*
* You'll have to define an address where events will be received:
*
* define('AK_LOGER_DEFAULT_MAIL_DESTINATION', 'dev@example.com');
*
* ## fatal
*
* will run exit(0);
*
*
* # Customizing the way the logger work
*
* You can customize init options for each environment or namespace by
* setting:
*
* AkConfig::setOption('production_logger_options', array(
* 'mail_destination' => 'warnings@example.com',
* ));
*
* If you want to write your own handler, you can use set it like:
*
* AkConfig::setOption('production_logger', 'MyCustomLogger');
*
* or
*
* AkConfig::setOption('production_logger', $LoggerInstance);
*
* If you want to use your custom logger for all environments, use:
*
* AkConfig::setOption('logger', 'MyCustomLogger');
*
* Your custom loggers need to implement AkLoggerInterface
*
*
*
* ## Rotating your logs
*
* Akelos does not provide a mechanism to rotate file logs.
*
* Anyhow, you can rotate logs by adding to your +logrotate+ configutation
*
* /etc/logrotate.conf
*
* /path/to/your/akelos/applicaton/log/*.log {
* daily
* missingok
* rotate 7
* compress
* delaycompress
* notifempty
* copytruncate
* }
*/
defined('AK_LOG_DIR') || define('AK_LOG_DIR', (defined('MAKELOS_BASE_DIR') ? MAKELOS_BASE_DIR : AK_BASE_DIR).DS.'log');
defined('AK_LOGER_DEFAULT_LOG_FILE') || define('AK_LOGER_DEFAULT_LOG_FILE', AK_LOG_DIR.DS.AK_ENVIRONMENT.'.log');
defined('AK_LOG_LEVEL') || define('AK_LOG_LEVEL', AK_PRODUCTION_MODE ? 'info,warn,error,fatal' : 'debug,info,warn,error,fatal');
defined('AK_LOG_HANDLERS') || define('AK_LOG_HANDLERS', 'file,display,mail,fatal');
// Default mail logger settings
defined('AK_LOGER_DEFAULT_MAIL_DESTINATION') || define('AK_LOGER_DEFAULT_MAIL_DESTINATION', false);
defined('AK_LOGER_DEFAULT_MAIL_SENDER') || define('AK_LOGER_DEFAULT_MAIL_SENDER', AK_HOST);
defined('AK_LOGER_DEFAULT_MAIL_SUBJECT') || define('AK_LOGER_DEFAULT_MAIL_SUBJECT', '[%error_level %namespace] '.AK_APP_NAME.' logger');
defined('AK_LOG_ENABLE_COLORING') || define('AK_LOG_ENABLE_COLORING', true);
interface AkLoggerInterface
{
public function debug($message, $parameters = array());
public function info($message, $parameters = array());
public function warn($message, $parameters = array());
public function error($message, $parameters = array());
public function fatal($message, $parameters = array());
}
class AkLogger implements AkLoggerInterface
{
public $options = array(
'namespace' => AK_ENVIRONMENT,
'log_level' => AK_LOG_LEVEL,
'log_handlers' => AK_LOG_HANDLERS,
'print' => AK_DEV_MODE,
// By default no mails are sent unless AK_LOGER_DEFAULT_MAIL_DESTINATION
// is defined
'mail_destination' => AK_LOGER_DEFAULT_MAIL_DESTINATION,
'mail_sender' => AK_LOGER_DEFAULT_MAIL_SENDER,
'mail_subject' => AK_LOGER_DEFAULT_MAIL_SUBJECT,
);
// Log levels
protected $_log_levels = array(
'debug' => 1,
'info' => 2,
'warn' => 4,
'error' => 8,
'fatal' => 16
);
// Log handlers
protected $_log_handlers = array(
'file' => 1,
'display' => 2,
'mail' => 4,
'fatal' => 8
);
protected $_handlers_for_levels = array(
'debug' => array('file'),
'info' => array('file'),
'warn' => array('file', 'display'),
'error' => array('file', 'display', 'mail'),
'fatal' => array('file', 'display', 'mail', 'fatal'),
);
public $default_mail_destination = AK_LOGER_DEFAULT_MAIL_DESTINATION;
public $default_mail_sender = AK_LOGER_DEFAULT_MAIL_SENDER;
public $default_mail_subject = AK_LOGER_DEFAULT_MAIL_SUBJECT;
public $error_file = AK_LOGER_DEFAULT_LOG_FILE;
public $log_type;
private $_log_level;
private $_handling_mode;
private $_level_handlers = array();
public function __construct($options = array()) {
$this->init($options);
}
public function init($options = array()){
$this->setOptions($options);
$this->setLogLevels($this->options['log_level']);
$this->setLogHandlers($this->options['log_handlers']);
$this->setNamespace($this->options['namespace']);
$this->setupHandlersForLevels($this->options['handlers_for_levels']);
}
public function setOptions($options = array()){
$default_options = array(
'namespace' => AK_ENVIRONMENT,
'log_level' => Ak::toArray(AkConfig::getOption('log_level', AK_LOG_LEVEL)),
'log_handlers' => Ak::toArray(AkConfig::getOption('log_handlers', AK_LOG_HANDLERS)),
'handlers_for_levels' => Ak::toArray(AkConfig::getOption('handlers_for_levels', $this->_handlers_for_levels)),
'log_handling_methods' => AkConfig::getOption('log_handling_methods', $this->getLogHandlingMethods($this->_log_handlers)),
'print' => !AK_PRODUCTION_MODE && !AK_TEST_MODE,
'send_mails' => !AK_TEST_MODE,
'mail_destination' => AK_LOGER_DEFAULT_MAIL_DESTINATION,
'mail_sender' => AK_LOGER_DEFAULT_MAIL_SENDER,
'mail_subject' => AK_LOGER_DEFAULT_MAIL_SUBJECT,
);
$this->options = array_merge($default_options, $options);
}
public function debug($message, $parameters = array()) {
$this->_log(__FUNCTION__, $message, $parameters);
}
public function info($message, $parameters = array()) {
$this->_log(__FUNCTION__, $message, $parameters);
}
public function warn($message, $parameters = array()) {
$this->_log(__FUNCTION__, $message, $parameters);
}
public function error($message, $parameters = array()) {
$this->_log(__FUNCTION__, $message, $parameters);
}
public function fatal($message, $parameters = array()) {
$this->_log(__FUNCTION__, $message, $parameters);
}
public function _log($error_level, $error_message, $parameters = array()) {
if(isset($this->_log_levels[$error_level]) && !($this->_log_levels[$error_level] & $this->_log_level)){
return;
}
foreach ($this->_log_handlers as $handler => $mode){
if($this->_handling_mode & $mode){
if(($this->_level_handlers[$error_level] & $mode) && isset($this->options['log_handling_methods'][$handler])){
$this->{$this->options['log_handling_methods'][$handler]}($error_level, $error_message, $parameters);
}
}
}
}
public function setNamespace($namespace){
$this->options['namespace'] = $namespace;
$this->setErrorFileForNamespace($namespace);
}
public function setErrorFileForNamespace($namespace){
$log_dir = AkConfig::getDir('log');
$file_name = $log_dir.DS.$namespace.'.log';
$this->error_file = $file_name;
if(!is_file($file_name)){
if(is_writable($log_dir)){
AkFileSystem::file_put_contents($file_name, '', array('base_path' => AkConfig::getDir('log')));
}else{
$this->error_file = false;
}
}
}
public function setLogLevels($levels){
$log_level = 0;
foreach ($levels as $level){
if(isset($this->_log_levels[$level])){
$log_level += $this->_log_levels[$level];
}
}
$this->options['log_level'] = $log_level;
$this->_log_level = $log_level;
}
public function setLogHandlers($handlers){
$handling_mode = 0;
foreach ($handlers as $handler){
if(isset($this->_log_handlers[$handler])){
$handling_mode += $this->_log_handlers[$handler];
}
}
$this->options['handling_mode'] = $handling_mode;
$this->_handling_mode = $handling_mode;
}
public function setupHandlersForLevels($options){
$options = array_merge($this->_handlers_for_levels, $options);
foreach ($options as $level => $handlers){
$this->_level_handlers[$level] = 0;
$handlers = Ak::toArray($handlers);
foreach ($handlers as $handler){
$this->_level_handlers[$level] += $this->_log_handlers[$handler];
}
}
}
public function getLogHandlingMethods($log_handlers = array()){
$methods = array();
foreach ($log_handlers as $handler => $mode){
$method_name = 'handle'.AkInflector::camelize($handler).'Message';
if(method_exists($this, $method_name)){
$methods[$handler] = $method_name;
}
}
return $methods;
}
public function handleFileMessage($error_level, $message, $parameters = array()){
if($this->error_file == false){
return;
}
$filename = $this->error_file;
$message = $this->getMessageFormatedAsString($error_level, $message, $parameters);
if(!$fp = fopen($filename, 'a')) {
trigger_error('Cannot start logging to file '.$filename, E_USER_NOTICE);
}
flock($fp, LOCK_EX);
if (fwrite($fp, $message) === FALSE) {
flock ($fp, LOCK_UN);
trigger_error('Error writing file: '.$filename, E_USER_NOTICE);
}
flock ($fp, LOCK_UN);
fclose($fp);
}
public function handleDisplayMessage($error_level, $message, $parameters = array()){
if(AkConfig::getOption('logger.display_message', true)){
if(!empty($this->options['print'])){
list($file,$line,$method) = AkDebug::getLastFileAndLineAndMethod(false, 3);
AkDebug::trace("<strong>[$error_level]</strong> - ".AkTextHelper::html_escape($message), $line, $file, $method, false);
if(!empty($parameters)) {
AkDebug::trace($parameters, $line, $file, $method);
}
}
}
}
public function handleMailMessage($error_level, $message, $parameters = array()){
if(AkConfig::getOption('logger.send_mails', true)){
if(!empty($this->options['send_mails']) && !empty($this->options['mail_destination']) && AK_PRODUCTION_MODE){
$message = $this->getMessageFormatedAsString($error_level, $message, $parameters, true);
$subject = str_replace(array('%error_level','%namespace'), array($error_level, $this->options['namespace']), $this->options['mail_subject']);
Ak::mail($this->options['mail_sender'], $this->options['mail_destination'], $subject, $message);
}
}
}
public function handleFatalMessage($error_level, $message, $parameters = array()){
if(AkConfig::getOption('logger.exit_on_fatal', true)){
exit(0);
}
}
public function getMessageFormatedAsString($error_level, $error_message, $parameters = array(), $prevent_color = false) {
$flag = $prevent_color ? $error_level : $this->getFlagForError($error_level);
$message = date('r')."\t[$flag]\t$error_message";
if(!empty($parameters)){
$message .= "\n". json_encode($parameters)."\n";
}
return $message."\n";
}
public function formatText($text, $style = 'normal') {
if(!AK_LOG_ENABLE_COLORING){
return $text;
}
$colors = array(
'light_red ' => '[1;31m',
'light_green' => '[1;32m',
'yellow' => '[1;33m',
'light_blue' => '[1;34m',
'magenta' => '[1;35m',
'light_cyan' => '[1;36m',
'white' => '[1;37m',
'normal' => '[0m',
'black' => '[0;30m',
'red' => '[0;31m',
'green' => '[0;32m',
'brown' => '[0;33m',
'blue' => '[0;34m',
'cyan' => '[0;36m',
'bold' => '[1m',
'underscore' => '[4m',
'reverse' => '[7m'
);
return "\033".(isset($colors[$style]) ? $colors[$style] : '[0m').$text."\033[0m";
}
public function getFlagForError($error_type){
$colors = array(
'debug' => 'magenta',
'info' => 'cyan',
'warn' => 'blue',
'error' => 'red',
'fatal' => 'red',
);
return $this->formatText($error_type, isset($colors[$error_type]) ? $colors[$error_type] : 'normal');
}
/**
* @deprecated Alias for info
*/
public function message($message, $parameters = array()) {
$this->info($message, $parameters);
}
/**
* @deprecated Alias for info
*/
public function notice($message, $parameters = array()) {
Ak::deprecateMethod(__METHOD__, __CLASS__.'->info()');
$this->info($message, $parameters);
}
/**
* @deprecated Alias for info
*/
public function log($log_level, $message, $parameters = array()) {
Ak::deprecateMethod(__METHOD__, __CLASS__.'->info()');
$this->_log($log_level, $message, $parameters);
}
}