1 | <?php |
||
33 | abstract class Target extends Component |
||
34 | { |
||
35 | /** |
||
36 | * @var bool whether to enable this log target. Defaults to true. |
||
37 | */ |
||
38 | public $enabled = true; |
||
39 | /** |
||
40 | * @var array list of message categories that this target is interested in. Defaults to empty, meaning all categories. |
||
41 | * You can use an asterisk at the end of a category so that the category may be used to |
||
42 | * match those categories sharing the same common prefix. For example, 'yii\db\*' will match |
||
43 | * categories starting with 'yii\db\', such as `yii\db\Connection`. |
||
44 | */ |
||
45 | public $categories = []; |
||
46 | /** |
||
47 | * @var array list of message categories that this target is NOT interested in. Defaults to empty, meaning no uninteresting messages. |
||
48 | * If this property is not empty, then any category listed here will be excluded from [[categories]]. |
||
49 | * You can use an asterisk at the end of a category so that the category can be used to |
||
50 | * match those categories sharing the same common prefix. For example, 'yii\db\*' will match |
||
51 | * categories starting with 'yii\db\', such as `yii\db\Connection`. |
||
52 | * @see categories |
||
53 | */ |
||
54 | public $except = []; |
||
55 | /** |
||
56 | * @var array the message levels that this target is interested in. |
||
57 | * |
||
58 | * The parameter should be an array of interested level names. See [[LogLevel]] constants for valid level names. |
||
59 | * |
||
60 | * For example: |
||
61 | * |
||
62 | * ```php |
||
63 | * ['error', 'warning'], |
||
64 | * // or |
||
65 | * [LogLevel::ERROR, LogLevel::WARNING] |
||
66 | * ``` |
||
67 | * |
||
68 | * Defaults is empty array, meaning all available levels. |
||
69 | */ |
||
70 | public $levels = []; |
||
71 | /** |
||
72 | * @var array list of the PHP predefined variables that should be logged in a message. |
||
73 | * Note that a variable must be accessible via `$GLOBALS`. Otherwise it won't be logged. |
||
74 | * |
||
75 | * Defaults to `['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER']`. |
||
76 | * |
||
77 | * Since version 2.0.9 additional syntax can be used: |
||
78 | * Each element could be specified as one of the following: |
||
79 | * |
||
80 | * - `var` - `var` will be logged. |
||
81 | * - `var.key` - only `var[key]` key will be logged. |
||
82 | * - `!var.key` - `var[key]` key will be excluded. |
||
83 | * |
||
84 | * @see \yii\helpers\ArrayHelper::filter() |
||
85 | */ |
||
86 | public $logVars = ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER']; |
||
87 | /** |
||
88 | * @var callable a PHP callable that returns a string to be prefixed to every exported message. |
||
89 | * |
||
90 | * If not set, [[getMessagePrefix()]] will be used, which prefixes the message with context information |
||
91 | * such as user IP, user ID and session ID. |
||
92 | * |
||
93 | * The signature of the callable should be `function ($message)`. |
||
94 | */ |
||
95 | public $prefix; |
||
96 | /** |
||
97 | * @var int how many messages should be accumulated before they are exported. |
||
98 | * Defaults to 1000. Note that messages will always be exported when the application terminates. |
||
99 | * Set this property to be 0 if you don't want to export messages until the application terminates. |
||
100 | */ |
||
101 | public $exportInterval = 1000; |
||
102 | /** |
||
103 | * @var array the messages that are retrieved from the logger so far by this log target. |
||
104 | * Please refer to [[Logger::messages]] for the details about the message structure. |
||
105 | */ |
||
106 | public $messages = []; |
||
107 | |||
108 | |||
109 | /** |
||
110 | * Exports log [[messages]] to a specific destination. |
||
111 | * Child classes must implement this method. |
||
112 | */ |
||
113 | abstract public function export(); |
||
114 | |||
115 | /** |
||
116 | * Processes the given log messages. |
||
117 | * This method will filter the given messages with [[levels]] and [[categories]]. |
||
118 | * And if requested, it will also export the filtering result to specific medium (e.g. email). |
||
119 | * @param array $messages log messages to be processed. See [[Logger::messages]] for the structure |
||
120 | * of each message. |
||
121 | * @param bool $final whether this method is called at the end of the current application |
||
122 | */ |
||
123 | 31 | public function collect($messages, $final) |
|
140 | |||
141 | /** |
||
142 | * Generates the context information to be logged. |
||
143 | * The default implementation will dump user information, system variables, etc. |
||
144 | * @return string the context information. If an empty string, it means no context information. |
||
145 | */ |
||
146 | 26 | protected function getContextMessage() |
|
155 | |||
156 | /** |
||
157 | * Filters the given messages according to their categories and levels. |
||
158 | * @param array $messages messages to be filtered. |
||
159 | * The message structure follows that in [[Logger::messages]]. |
||
160 | * @param array $levels the message levels to filter by. Empty value means allowing all levels. |
||
161 | * @param array $categories the message categories to filter by. If empty, it means all categories are allowed. |
||
162 | * @param array $except the message categories to exclude. If empty, it means all categories are allowed. |
||
163 | * @return array the filtered messages. |
||
164 | */ |
||
165 | 31 | public static function filterMessages($messages, $levels = [], $categories = [], $except = []) |
|
197 | |||
198 | /** |
||
199 | * Formats a log message for display as a string. |
||
200 | * @param array $message the log message to be formatted. |
||
201 | * The message structure follows that in [[Logger::messages]]. |
||
202 | * @return string the formatted message |
||
203 | */ |
||
204 | 2 | public function formatMessage($message) |
|
221 | |||
222 | /** |
||
223 | * Returns a string to be prefixed to the given message. |
||
224 | * If [[prefix]] is configured it will return the result of the callback. |
||
225 | * The default implementation will return user IP, user ID and session ID as a prefix. |
||
226 | * @param array $message the message being exported. |
||
227 | * The message structure follows that in [[Logger::messages]]. |
||
228 | * @return string the prefix string |
||
229 | */ |
||
230 | 8 | public function getMessagePrefix($message) |
|
257 | } |
||
258 |
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.