1 | <?php |
||
44 | class Logger extends Component implements LoggerInterface |
||
45 | { |
||
46 | use LoggerTrait; |
||
47 | |||
48 | /** |
||
49 | * @var array logged messages. This property is managed by [[log()]] and [[flush()]]. |
||
50 | * Each log message is of the following structure: |
||
51 | * |
||
52 | * ``` |
||
53 | * [ |
||
54 | * [0] => level (string) |
||
55 | * [1] => message (mixed, can be a string or some complex data, such as an exception object) |
||
56 | * [2] => context (array) |
||
57 | * ] |
||
58 | * ``` |
||
59 | * |
||
60 | * Message context has a following keys: |
||
61 | * |
||
62 | * - category: string, message category. |
||
63 | * - time: float, message timestamp obtained by microtime(true). |
||
64 | * - trace: array, debug backtrace, contains the application code call stacks. |
||
65 | * - memory: int, memory usage in bytes, obtained by `memory_get_usage()`, available since version 2.0.11. |
||
66 | */ |
||
67 | public $messages = []; |
||
68 | /** |
||
69 | * @var int how many messages should be logged before they are flushed from memory and sent to targets. |
||
70 | * Defaults to 1000, meaning the [[flush]] method will be invoked once every 1000 messages logged. |
||
71 | * Set this property to be 0 if you don't want to flush messages until the application terminates. |
||
72 | * This property mainly affects how much memory will be taken by the logged messages. |
||
73 | * A smaller value means less memory, but will increase the execution time due to the overhead of [[flush()]]. |
||
74 | */ |
||
75 | public $flushInterval = 1000; |
||
76 | /** |
||
77 | * @var int how much call stack information (file name and line number) should be logged for each message. |
||
78 | * If it is greater than 0, at most that number of call stacks will be logged. Note that only application |
||
79 | * call stacks are counted. |
||
80 | */ |
||
81 | public $traceLevel = 0; |
||
82 | |||
83 | /** |
||
84 | * @var array|Target[] the log targets. Each array element represents a single [[Target|log target]] instance |
||
85 | * or the configuration for creating the log target instance. |
||
86 | * @since 2.1 |
||
87 | */ |
||
88 | private $_targets = []; |
||
89 | /** |
||
90 | * @var bool whether [[targets]] have been initialized, e.g. ensured to be objects. |
||
91 | * @since 2.1 |
||
92 | */ |
||
93 | private $_isTargetsInitialized = false; |
||
94 | |||
95 | |||
96 | /** |
||
97 | * @return Target[] the log targets. Each array element represents a single [[Target|log target]] instance. |
||
98 | * @since 2.1 |
||
99 | */ |
||
100 | 35 | public function getTargets() |
|
112 | |||
113 | /** |
||
114 | * @param array|Target[] $targets the log targets. Each array element represents a single [[Target|log target]] instance |
||
115 | * or the configuration for creating the log target instance. |
||
116 | * @since 2.1 |
||
117 | */ |
||
118 | 27 | public function setTargets($targets) |
|
123 | |||
124 | /** |
||
125 | * Adds extra target to [[targets]]. |
||
126 | * @param Target|array $target the log target instance or its DI compatible configuration. |
||
127 | * @param string|null $name array key to be used to store target, if `null` is given target will be append |
||
128 | * to the end of the array by natural integer key. |
||
129 | */ |
||
130 | 1 | public function addTarget($target, $name = null) |
|
141 | |||
142 | /** |
||
143 | * Initializes the logger by registering [[flush()]] as a shutdown function. |
||
144 | */ |
||
145 | 21 | public function init() |
|
146 | { |
||
147 | 21 | parent::init(); |
|
148 | 21 | register_shutdown_function(function () { |
|
149 | // make regular flush before other shutdown functions, which allows session data collection and so on |
||
150 | $this->flush(); |
||
151 | // make sure log entries written by shutdown functions are also flushed |
||
152 | // ensure "flush()" is called last when there are multiple shutdown functions |
||
153 | register_shutdown_function([$this, 'flush'], true); |
||
154 | 21 | }); |
|
155 | 21 | } |
|
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | 1536 | public function log($level, $message, array $context = array()) |
|
219 | |||
220 | /** |
||
221 | * Flushes log messages from memory to targets. |
||
222 | * @param bool $final whether this is a final call during a request. |
||
223 | */ |
||
224 | 36 | public function flush($final = false) |
|
233 | |||
234 | /** |
||
235 | * Dispatches the logged messages to [[targets]]. |
||
236 | * @param array $messages the logged messages |
||
237 | * @param bool $final whether this method is called at the end of the current application |
||
238 | * @since 2.1 |
||
239 | */ |
||
240 | 37 | protected function dispatch($messages, $final) |
|
264 | |||
265 | /** |
||
266 | * Parses log message resolving placeholders in the form: '{foo}', where foo |
||
267 | * will be replaced by the context data in key "foo". |
||
268 | * @param string $message log message. |
||
269 | * @param array $context message context. |
||
270 | * @return string parsed message. |
||
271 | * @since 2.1 |
||
272 | */ |
||
273 | protected function parseMessage($message, array $context) |
||
283 | |||
284 | /** |
||
285 | * Returns the total elapsed time since the start of the current request. |
||
286 | * This method calculates the difference between now and the timestamp |
||
287 | * defined by constant `YII_BEGIN_TIME` which is evaluated at the beginning |
||
288 | * of [[\yii\BaseYii]] class file. |
||
289 | * @return float the total elapsed time in seconds for current request. |
||
290 | */ |
||
291 | 1 | public function getElapsedTime() |
|
295 | |||
296 | /** |
||
297 | * Returns the text display of the specified level. |
||
298 | * @param mixed $level the message level, e.g. [[LogLevel::ERROR]], [[LogLevel::WARNING]]. |
||
299 | * @return string the text display of the level |
||
300 | */ |
||
301 | 3 | public static function getLevelName($level) |
|
308 | } |
||
309 |