mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-22 17:48:37 +08:00
Merge pull request #6142 from cdvrooman/patch-12
[skip ci] Update runtime-logging.md
This commit is contained in:
@@ -5,7 +5,7 @@ Yii provides a powerful logging framework that is highly customizable and extens
|
|||||||
can easily log various types of messages, filter them, and gather them at different targets, such as files, databases,
|
can easily log various types of messages, filter them, and gather them at different targets, such as files, databases,
|
||||||
emails.
|
emails.
|
||||||
|
|
||||||
Using the Yii logging framework involves the following steps of work:
|
Using the Yii logging framework involves the following steps:
|
||||||
|
|
||||||
* Record [log messages](#log-messages) at various places in your code;
|
* Record [log messages](#log-messages) at various places in your code;
|
||||||
* Configure [log targets](#log-targets) in the application configuration to filter and export log messages;
|
* Configure [log targets](#log-targets) in the application configuration to filter and export log messages;
|
||||||
@@ -39,7 +39,7 @@ it will be exported as a string by calling [[yii\helpers\VarDumper::export()]].
|
|||||||
To better organize and filter log messages, it is recommended that you specify an appropriate category for each
|
To better organize and filter log messages, it is recommended that you specify an appropriate category for each
|
||||||
log message. You may choose a hierarchical naming scheme for categories, which will make it easier for
|
log message. You may choose a hierarchical naming scheme for categories, which will make it easier for
|
||||||
[log targets](#log-targets) to filter messages based on their categories. A simple yet effective naming scheme
|
[log targets](#log-targets) to filter messages based on their categories. A simple yet effective naming scheme
|
||||||
is to use the PHP magic constant `__METHOD__` as category names. This is also the approach used in the core
|
is to use the PHP magic constant `__METHOD__` for the category names. This is also the approach used in the core
|
||||||
Yii framework code. For example,
|
Yii framework code. For example,
|
||||||
|
|
||||||
```php
|
```php
|
||||||
@@ -47,7 +47,7 @@ Yii::trace('start calculating average revenue', __METHOD__);
|
|||||||
```
|
```
|
||||||
|
|
||||||
The `__METHOD__` constant evaluates as the name of the method (prefixed with the fully qualified class name) where
|
The `__METHOD__` constant evaluates as the name of the method (prefixed with the fully qualified class name) where
|
||||||
the constant appears. For example, it equals to the string `'app\controllers\RevenueController::calculate'` if
|
the constant appears. For example, it is equal to the string `'app\controllers\RevenueController::calculate'` if
|
||||||
the above line of code is called within this method.
|
the above line of code is called within this method.
|
||||||
|
|
||||||
> Info: The logging methods described above are actually shortcuts to the [[yii\log\Logger::log()|log()]] method
|
> Info: The logging methods described above are actually shortcuts to the [[yii\log\Logger::log()|log()]] method
|
||||||
@@ -58,9 +58,9 @@ enough messages are logged or when the application ends, the logger object will
|
|||||||
|
|
||||||
## Log Targets <a name="log-targets"></a>
|
## Log Targets <a name="log-targets"></a>
|
||||||
|
|
||||||
A log target is an instance of [[yii\log\Target]] class or its child class. It filters the log messages by their
|
A log target is an instance of the [[yii\log\Target]] class or its child class. It filters the log messages by their
|
||||||
severity levels and categories and then exports them to some medium. For example, a [[yii\log\DbTarget|database target]]
|
severity levels and categories and then exports them to some medium. For example, a [[yii\log\DbTarget|database target]]
|
||||||
exports the filtered log messages to a database table, while a [[yii\log\EmailTarget|email target]] exports
|
exports the filtered log messages to a database table, while an [[yii\log\EmailTarget|email target]] exports
|
||||||
the log messages to specified email addresses.
|
the log messages to specified email addresses.
|
||||||
|
|
||||||
You can register multiple log targets in an application by configuring them through the `log` [application component](structure-application-components.md)
|
You can register multiple log targets in an application by configuring them through the `log` [application component](structure-application-components.md)
|
||||||
@@ -169,7 +169,7 @@ under the categories whose names match either `yii\db\*` or `yii\web\HttpExcepti
|
|||||||
|
|
||||||
### Message Formatting <a name="message-formatting"></a>
|
### Message Formatting <a name="message-formatting"></a>
|
||||||
|
|
||||||
Log targets export the filtered log messages in certain format. For example, if you install
|
Log targets export the filtered log messages in a certain format. For example, if you install
|
||||||
a log target of the class [[yii\log\FileTarget]], you may find a log message similar to the following in the
|
a log target of the class [[yii\log\FileTarget]], you may find a log message similar to the following in the
|
||||||
`runtime/log/app.log` file:
|
`runtime/log/app.log` file:
|
||||||
|
|
||||||
@@ -185,7 +185,7 @@ Timestamp [IP address][User ID][Session ID][Severity Level][Category] Message Te
|
|||||||
|
|
||||||
You may customize this format by configuring the [[yii\log\Target::prefix]] property which takes a PHP callable
|
You may customize this format by configuring the [[yii\log\Target::prefix]] property which takes a PHP callable
|
||||||
returning a customized message prefix. For example, the following code configures a log target to prefix each
|
returning a customized message prefix. For example, the following code configures a log target to prefix each
|
||||||
log message with the current user ID (IP address and Session ID are removed for privacy reason).
|
log message with the current user ID (IP address and Session ID are removed for privacy reasons).
|
||||||
|
|
||||||
```php
|
```php
|
||||||
[
|
[
|
||||||
@@ -246,7 +246,7 @@ or when debugging an application.
|
|||||||
|
|
||||||
As aforementioned, log messages are maintained in an array by the [[yii\log\Logger|logger object]]. To limit the
|
As aforementioned, log messages are maintained in an array by the [[yii\log\Logger|logger object]]. To limit the
|
||||||
memory consumption by this array, the logger will flush the recorded messages to the [log targets](#log-targets)
|
memory consumption by this array, the logger will flush the recorded messages to the [log targets](#log-targets)
|
||||||
each time the array accumulates certain number of log messages. You can customize this number by configuring
|
each time the array accumulates a certain number of log messages. You can customize this number by configuring
|
||||||
the [[yii\log\Dispatcher::flushInterval|flushInterval]] property of the `log` component:
|
the [[yii\log\Dispatcher::flushInterval|flushInterval]] property of the `log` component:
|
||||||
|
|
||||||
|
|
||||||
@@ -280,7 +280,7 @@ Because of the flushing and exporting level setting, by default when you call `Y
|
|||||||
method, you will NOT see the log message immediately in the log targets. This could be a problem for some long-running
|
method, you will NOT see the log message immediately in the log targets. This could be a problem for some long-running
|
||||||
console applications. To make each log message appear immediately in the log targets, you should set both
|
console applications. To make each log message appear immediately in the log targets, you should set both
|
||||||
[[yii\log\Dispatcher::flushInterval|flushInterval]] and [[yii\log\Target::exportInterval|exportInterval]] to be 1,
|
[[yii\log\Dispatcher::flushInterval|flushInterval]] and [[yii\log\Target::exportInterval|exportInterval]] to be 1,
|
||||||
like shown below:
|
as shown below:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
return [
|
return [
|
||||||
@@ -311,7 +311,7 @@ You may do so via the log target configuration or by the following PHP statement
|
|||||||
Yii::$app->log->targets['file']->enabled = false;
|
Yii::$app->log->targets['file']->enabled = false;
|
||||||
```
|
```
|
||||||
|
|
||||||
The above code requires you to name a target as `file`, like shown below by using string keys in the
|
The above code requires you to name a target as `file`, as shown below by using string keys in the
|
||||||
`targets` array:
|
`targets` array:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
|||||||
Reference in New Issue
Block a user