1 | <?php |
||
43 | class Logger extends Component |
||
44 | { |
||
45 | /** |
||
46 | * Error message level. An error message is one that indicates the abnormal termination of the |
||
47 | * application and may require developer's handling. |
||
48 | */ |
||
49 | const LEVEL_ERROR = 0x01; |
||
50 | /** |
||
51 | * Warning message level. A warning message is one that indicates some abnormal happens but |
||
52 | * the application is able to continue to run. Developers should pay attention to this message. |
||
53 | */ |
||
54 | const LEVEL_WARNING = 0x02; |
||
55 | /** |
||
56 | * Informational message level. An informational message is one that includes certain information |
||
57 | * for developers to review. |
||
58 | */ |
||
59 | const LEVEL_INFO = 0x04; |
||
60 | /** |
||
61 | * Tracing message level. An tracing message is one that reveals the code execution flow. |
||
62 | */ |
||
63 | const LEVEL_TRACE = 0x08; |
||
64 | /** |
||
65 | * Profiling message level. This indicates the message is for profiling purpose. |
||
66 | */ |
||
67 | const LEVEL_PROFILE = 0x40; |
||
68 | /** |
||
69 | * Profiling message level. This indicates the message is for profiling purpose. It marks the |
||
70 | * beginning of a profiling block. |
||
71 | */ |
||
72 | const LEVEL_PROFILE_BEGIN = 0x50; |
||
73 | /** |
||
74 | * Profiling message level. This indicates the message is for profiling purpose. It marks the |
||
75 | * end of a profiling block. |
||
76 | */ |
||
77 | const LEVEL_PROFILE_END = 0x60; |
||
78 | |||
79 | /** |
||
80 | * @var array logged messages. This property is managed by [[log()]] and [[flush()]]. |
||
81 | * Each log message is of the following structure: |
||
82 | * |
||
83 | * ``` |
||
84 | * [ |
||
85 | * [0] => message (mixed, can be a string or some complex data, such as an exception object) |
||
86 | * [1] => level (integer) |
||
87 | * [2] => category (string) |
||
88 | * [3] => timestamp (float, obtained by microtime(true)) |
||
89 | * [4] => traces (array, debug backtrace, contains the application code call stacks) |
||
90 | * [5] => memory usage in bytes (int, obtained by memory_get_usage()), available since version 2.0.11. |
||
91 | * ] |
||
92 | * ``` |
||
93 | */ |
||
94 | public $messages = []; |
||
95 | /** |
||
96 | * @var int how many messages should be logged before they are flushed from memory and sent to targets. |
||
97 | * Defaults to 1000, meaning the [[flush]] method will be invoked once every 1000 messages logged. |
||
98 | * Set this property to be 0 if you don't want to flush messages until the application terminates. |
||
99 | * This property mainly affects how much memory will be taken by the logged messages. |
||
100 | * A smaller value means less memory, but will increase the execution time due to the overhead of [[flush()]]. |
||
101 | */ |
||
102 | public $flushInterval = 1000; |
||
103 | /** |
||
104 | * @var int how much call stack information (file name and line number) should be logged for each message. |
||
105 | * If it is greater than 0, at most that number of call stacks will be logged. Note that only application |
||
106 | * call stacks are counted. |
||
107 | */ |
||
108 | public $traceLevel = 0; |
||
109 | /** |
||
110 | * @var Dispatcher the message dispatcher |
||
111 | */ |
||
112 | public $dispatcher; |
||
113 | |||
114 | |||
115 | /** |
||
116 | * Initializes the logger by registering [[flush()]] as a shutdown function. |
||
117 | 22 | */ |
|
118 | public function init() |
||
129 | |||
130 | /** |
||
131 | * Logs a message with the given type and category. |
||
132 | * If [[traceLevel]] is greater than 0, additional call stack information about |
||
133 | * the application code will be logged as well. |
||
134 | * @param string|array $message the message to be logged. This can be a simple string or a more |
||
135 | * complex data structure that will be handled by a [[Target|log target]]. |
||
136 | * @param int $level the level of the message. This must be one of the following: |
||
137 | * `Logger::LEVEL_ERROR`, `Logger::LEVEL_WARNING`, `Logger::LEVEL_INFO`, `Logger::LEVEL_TRACE`, |
||
138 | * `Logger::LEVEL_PROFILE_BEGIN`, `Logger::LEVEL_PROFILE_END`. |
||
139 | * @param string $category the category of the message. |
||
140 | 963 | */ |
|
141 | public function log($message, $level, $category = 'application') |
||
164 | |||
165 | /** |
||
166 | * Flushes log messages from memory to targets. |
||
167 | * @param bool $final whether this is a final call during a request. |
||
168 | 56 | */ |
|
169 | public function flush($final = false) |
||
179 | |||
180 | /** |
||
181 | * Returns the total elapsed time since the start of the current request. |
||
182 | * This method calculates the difference between now and the timestamp |
||
183 | * defined by constant `YII_BEGIN_TIME` which is evaluated at the beginning |
||
184 | * of [[\yii\BaseYii]] class file. |
||
185 | * @return float the total elapsed time in seconds for current request. |
||
186 | 1 | */ |
|
187 | public function getElapsedTime() |
||
191 | |||
192 | /** |
||
193 | * Returns the profiling results. |
||
194 | * |
||
195 | * By default, all profiling results will be returned. You may provide |
||
196 | * `$categories` and `$excludeCategories` as parameters to retrieve the |
||
197 | * results that you are interested in. |
||
198 | * |
||
199 | * @param array $categories list of categories that you are interested in. |
||
200 | * You can use an asterisk at the end of a category to do a prefix match. |
||
201 | * For example, 'yii\db\*' will match categories starting with 'yii\db\', |
||
202 | * such as 'yii\db\Connection'. |
||
203 | * @param array $excludeCategories list of categories that you want to exclude |
||
204 | * @return array the profiling results. Each element is an array consisting of these elements: |
||
205 | * `info`, `category`, `timestamp`, `trace`, `level`, `duration`, `memory`, `memoryDiff`. |
||
206 | 4 | * The `memory` and `memoryDiff` values are available since version 2.0.11. |
|
207 | */ |
||
208 | 4 | public function getProfiling($categories = [], $excludeCategories = []) |
|
244 | |||
245 | /** |
||
246 | * Returns the statistical results of DB queries. |
||
247 | * The results returned include the number of SQL statements executed and |
||
248 | * the total time spent. |
||
249 | * @return array the first element indicates the number of SQL statements executed, |
||
250 | 1 | * and the second element the total time spent in SQL execution. |
|
251 | */ |
||
252 | 1 | public function getDbProfiling() |
|
263 | |||
264 | /** |
||
265 | * Calculates the elapsed time for the given log messages. |
||
266 | * @param array $messages the log messages obtained from profiling |
||
267 | * @return array timings. Each element is an array consisting of these elements: |
||
268 | 4 | * `info`, `category`, `timestamp`, `trace`, `level`, `duration`, `memory`, `memoryDiff`. |
|
269 | * The `memory` and `memoryDiff` values are available since version 2.0.11. |
||
270 | 4 | */ |
|
271 | 4 | public function calculateTimings($messages) |
|
302 | |||
303 | 3 | ||
304 | /** |
||
305 | * Returns the text display of the specified level. |
||
306 | * @param int $level the message level, e.g. [[LEVEL_ERROR]], [[LEVEL_WARNING]]. |
||
307 | * @return string the text display of the level |
||
308 | */ |
||
309 | public static function getLevelName($level) |
||
323 | } |
||
324 |
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.