1 | <?php |
||
41 | class Logger extends Component |
||
42 | { |
||
43 | /** |
||
44 | * Error message level. An error message is one that indicates the abnormal termination of the |
||
45 | * application and may require developer's handling. |
||
46 | */ |
||
47 | const LEVEL_ERROR = 0x01; |
||
48 | /** |
||
49 | * Warning message level. A warning message is one that indicates some abnormal happens but |
||
50 | * the application is able to continue to run. Developers should pay attention to this message. |
||
51 | */ |
||
52 | const LEVEL_WARNING = 0x02; |
||
53 | /** |
||
54 | * Informational message level. An informational message is one that includes certain information |
||
55 | * for developers to review. |
||
56 | */ |
||
57 | const LEVEL_INFO = 0x04; |
||
58 | /** |
||
59 | * Tracing message level. An tracing message is one that reveals the code execution flow. |
||
60 | */ |
||
61 | const LEVEL_TRACE = 0x08; |
||
62 | /** |
||
63 | * Profiling message level. This indicates the message is for profiling purpose. |
||
64 | */ |
||
65 | const LEVEL_PROFILE = 0x40; |
||
66 | /** |
||
67 | * Profiling message level. This indicates the message is for profiling purpose. It marks the |
||
68 | * beginning of a profiling block. |
||
69 | */ |
||
70 | const LEVEL_PROFILE_BEGIN = 0x50; |
||
71 | /** |
||
72 | * Profiling message level. This indicates the message is for profiling purpose. It marks the |
||
73 | * end of a profiling block. |
||
74 | */ |
||
75 | const LEVEL_PROFILE_END = 0x60; |
||
76 | |||
77 | /** |
||
78 | * @var array logged messages. This property is managed by [[log()]] and [[flush()]]. |
||
79 | * Each log message is of the following structure: |
||
80 | * |
||
81 | * ``` |
||
82 | * [ |
||
83 | * [0] => message (mixed, can be a string or some complex data, such as an exception object) |
||
84 | * [1] => level (integer) |
||
85 | * [2] => category (string) |
||
86 | * [3] => timestamp (float, obtained by microtime(true)) |
||
87 | * [4] => traces (array, debug backtrace, contains the application code call stacks) |
||
88 | * ] |
||
89 | * ``` |
||
90 | */ |
||
91 | public $messages = []; |
||
92 | /** |
||
93 | * @var integer how many messages should be logged before they are flushed from memory and sent to targets. |
||
94 | * Defaults to 1000, meaning the [[flush]] method will be invoked once every 1000 messages logged. |
||
95 | * Set this property to be 0 if you don't want to flush messages until the application terminates. |
||
96 | * This property mainly affects how much memory will be taken by the logged messages. |
||
97 | * A smaller value means less memory, but will increase the execution time due to the overhead of [[flush()]]. |
||
98 | */ |
||
99 | public $flushInterval = 1000; |
||
100 | /** |
||
101 | * @var integer how much call stack information (file name and line number) should be logged for each message. |
||
102 | * If it is greater than 0, at most that number of call stacks will be logged. Note that only application |
||
103 | * call stacks are counted. |
||
104 | */ |
||
105 | public $traceLevel = 0; |
||
106 | /** |
||
107 | * @var Dispatcher the message dispatcher |
||
108 | */ |
||
109 | public $dispatcher; |
||
110 | |||
111 | |||
112 | /** |
||
113 | * Initializes the logger by registering [[flush()]] as a shutdown function. |
||
114 | */ |
||
115 | 24 | public function init() |
|
116 | { |
||
117 | 24 | parent::init(); |
|
118 | 24 | register_shutdown_function(function () { |
|
119 | // make regular flush before other shutdown functions, which allows session data collection and so on |
||
120 | $this->flush(); |
||
121 | // make sure log entries written by shutdown functions are also flushed |
||
122 | // ensure "flush()" is called last when there are multiple shutdown functions |
||
123 | register_shutdown_function([$this, 'flush'], true); |
||
124 | 24 | }); |
|
125 | 24 | } |
|
126 | |||
127 | /** |
||
128 | * Logs a message with the given type and category. |
||
129 | * If [[traceLevel]] is greater than 0, additional call stack information about |
||
130 | * the application code will be logged as well. |
||
131 | * @param string|array $message the message to be logged. This can be a simple string or a more |
||
132 | * complex data structure that will be handled by a [[Target|log target]]. |
||
133 | * @param integer $level the level of the message. This must be one of the following: |
||
134 | * `Logger::LEVEL_ERROR`, `Logger::LEVEL_WARNING`, `Logger::LEVEL_INFO`, `Logger::LEVEL_TRACE`, |
||
135 | * `Logger::LEVEL_PROFILE_BEGIN`, `Logger::LEVEL_PROFILE_END`. |
||
136 | * @param string $category the category of the message. |
||
137 | */ |
||
138 | 753 | public function log($message, $level, $category = 'application') |
|
139 | { |
||
140 | 753 | $time = microtime(true); |
|
141 | 753 | $traces = []; |
|
142 | 753 | if ($this->traceLevel > 0) { |
|
143 | $count = 0; |
||
144 | $ts = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
||
145 | array_pop($ts); // remove the last trace since it would be the entry script, not very useful |
||
146 | foreach ($ts as $trace) { |
||
147 | if (isset($trace['file'], $trace['line']) && strpos($trace['file'], YII2_PATH) !== 0) { |
||
148 | unset($trace['object'], $trace['args']); |
||
149 | $traces[] = $trace; |
||
150 | if (++$count >= $this->traceLevel) { |
||
151 | break; |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | 753 | $this->messages[] = [$message, $level, $category, $time, $traces]; |
|
157 | 753 | if ($this->flushInterval > 0 && count($this->messages) >= $this->flushInterval) { |
|
158 | 45 | $this->flush(); |
|
159 | 45 | } |
|
160 | 753 | } |
|
161 | |||
162 | /** |
||
163 | * Flushes log messages from memory to targets. |
||
164 | * @param boolean $final whether this is a final call during a request. |
||
165 | */ |
||
166 | 48 | public function flush($final = false) |
|
167 | { |
||
168 | 48 | $messages = $this->messages; |
|
169 | // https://github.com/yiisoft/yii2/issues/5619 |
||
170 | // new messages could be logged while the existing ones are being handled by targets |
||
171 | 48 | $this->messages = []; |
|
172 | 48 | if ($this->dispatcher instanceof Dispatcher) { |
|
173 | 34 | $this->dispatcher->dispatch($messages, $final); |
|
174 | 34 | } |
|
175 | 48 | } |
|
176 | |||
177 | /** |
||
178 | * Returns the total elapsed time since the start of the current request. |
||
179 | * This method calculates the difference between now and the timestamp |
||
180 | * defined by constant `YII_BEGIN_TIME` which is evaluated at the beginning |
||
181 | * of [[\yii\BaseYii]] class file. |
||
182 | * @return float the total elapsed time in seconds for current request. |
||
183 | */ |
||
184 | public function getElapsedTime() |
||
188 | |||
189 | /** |
||
190 | * Returns the profiling results. |
||
191 | * |
||
192 | * By default, all profiling results will be returned. You may provide |
||
193 | * `$categories` and `$excludeCategories` as parameters to retrieve the |
||
194 | * results that you are interested in. |
||
195 | * |
||
196 | * @param array $categories list of categories that you are interested in. |
||
197 | * You can use an asterisk at the end of a category to do a prefix match. |
||
198 | * For example, 'yii\db\*' will match categories starting with 'yii\db\', |
||
199 | * such as `yii\db\Connection`. |
||
200 | * @param array $excludeCategories list of categories that you want to exclude |
||
201 | * @return array the profiling results. Each element is an array consisting of these elements: |
||
202 | * `info`, `category`, `timestamp`, `trace`, `level`, `duration`. |
||
203 | */ |
||
204 | public function getProfiling($categories = [], $excludeCategories = []) |
||
240 | |||
241 | /** |
||
242 | * Returns the statistical results of DB queries. |
||
243 | * The results returned include the number of SQL statements executed and |
||
244 | * the total time spent. |
||
245 | * @return array the first element indicates the number of SQL statements executed, |
||
246 | * and the second element the total time spent in SQL execution. |
||
247 | */ |
||
248 | public function getDbProfiling() |
||
262 | |||
263 | /** |
||
264 | * Calculates the elapsed time for the given log messages. |
||
265 | * @param array $messages the log messages obtained from profiling |
||
266 | * @return array timings. Each element is an array consisting of these elements: |
||
267 | * `info`, `category`, `timestamp`, `trace`, `level`, `duration`. |
||
268 | */ |
||
269 | public function calculateTimings($messages) |
||
297 | |||
298 | |||
299 | /** |
||
300 | * Returns the text display of the specified level. |
||
301 | * @param integer $level the message level, e.g. [[LEVEL_ERROR]], [[LEVEL_WARNING]]. |
||
302 | * @return string the text display of the level |
||
303 | */ |
||
304 | 2 | public static function getLevelName($level) |
|
317 | } |
||
318 |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.