-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.php
158 lines (138 loc) · 3.52 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
<?php
/**
* @package SugiPHP
* @subpackage Logger
* @author Plamen Popov <tzappa@gmail.com>
* @license http://opensource.org/licenses/mit-license.php (MIT License)
*/
namespace SugiPHP\Logger;
use Monolog\Logger as Monolog;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\HandlerInterface;
class Logger extends Monolog
{
const MONOLOG_LEVEL = 2147483647;
/**
* Filters for every handler
*/
protected $filters = array();
/**
* Overriding Monolog constructor.
*/
public function __construct()
{
parent::__construct("");
}
/**
* Adds a handler to the stack.
*
* @param HandlerInterface $handler
* @param string $filter
*/
public function addHandler(HandlerInterface $handler, $filter = "all")
{
$this->handlers[] = $handler;
$this->filters[] = $filter;
}
/**
* Pushes a handler on to the stack.
*
* @param HandlerInterface $handler
* @param string $filter
*/
public function pushHandler(HandlerInterface $handler, $filter = "all")
{
array_unshift($this->filters, $filter);
parent::pushHandler($handler);
}
/**
* Pops a handler from the stack
*
* @return HandlerInterface
*/
public function popHandler()
{
$handler = parent::popHandler();
array_shift($this->filters);
return $handler;
}
/**
* Adds a log record.
*
* @param mixed $level The logging level
* @param string $message The log message
* @param array $context The log context
* @return boolean Whether the record has been processed
*/
public function addRecord($level, $message, array $context = array())
{
if (!$this->handlers) {
return false;
}
if (!static::$timezone) {
static::$timezone = new \DateTimeZone(date_default_timezone_get() ?: "UTC");
}
try {
$level_name = static::getLevelName($level);
} catch (\Exception $e) {
$level_name = $level;
}
$level_name = strtolower($level_name);
$record = array(
"message" => (string) $message,
"context" => $context,
"level_name" => $level_name,
"datetime" => \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), static::$timezone)->setTimezone(static::$timezone),
"extra" => array(),
// these are not used by SugiPHP, but are required by Monolog
"level" => static::MONOLOG_LEVEL,
"channel" => $this->name,
);
// check if any handler will handle this message
$handlerKey = null;
foreach ($this->handlers as $key => $handler) {
if (static::isHandlingByFilter($level_name, $this->filters[$key])) {
$handlerKey = $key;
break;
}
}
// none found
if ($handlerKey === null) {
return false;
}
// found at least one, process message and dispatch it
foreach ($this->processors as $processor) {
$record = call_user_func($processor, $record);
}
while (isset($this->handlers[$handlerKey])) {
if (static::isHandlingByFilter($level_name, $this->filters[$handlerKey])) {
try {
$this->handlers[$handlerKey]->handle($record);
} catch (\Exception $e) {
// can't do anything!
}
}
$handlerKey++;
}
return true;
}
/**
* Checks the level is within allowed levels
*
* @param string $level_name
* @param string $filter
* @return boolean
*/
protected static function isHandlingByFilter($level_name, $filter = null)
{
if (is_null($filter)) {
return true;
}
$filter = strtolower($filter) . " ";
$level_name = strtolower($level_name);
if (strpos($filter, "none") === 0) {
return (strpos($filter, "+$level_name ") > 0);
}
return (strpos($filter, "-$level_name ") === false);
}
}