Yii Logging - Yii

What are the different Yii methods to log a message?

Yii provides a highly customizable and extensible frame which facilitates to easily log various types of messages.

To log a message, you should call one of the following methods −

  • Yii::error() − Records a fatal error message.
  • Yii::warning() − Records a warning message.
  • Yii::info() − Records a message with some useful information.
  • Yii::trace() − Records a message to trace how a piece of code runs.

The above methods record log messages at various categories. They share the following function signature −

where −

  • $message − The log message to be recorded
  • $category − The category of the log message

A simple and convenient way of naming scheme is using the PHP __METHOD__ magic constant. For instance −

A log target is an instance of the yii\log\Target class. It filters all log messages by categories and exports them to file, database, and/or email.

Step 1 − You can register multiple log target as well, like.

By the above code, two targets are registered. All errors, warnings, traces, and info messages are selected and saved in a database by the first target. The second target sends all error and warning messages to the admin email.

Yii provides the following built-in log targets −

  • yii\log\DbTarget − Stores log messages in a database.
  • yii\log\FileTarget − Saves log messages in files.
  • yii\log\EmailTarget − Sends log messages to predefined email addresses.
  • yii\log\SyslogTarget − Saves log messages to syslog by calling the PHP function syslog().

By default, log messages are formatted as follows −

Step 2 − Configure the yii\log\Target::$prefix property to customize the format. For instance,

The above code snippet configures a log target to prefix all log messages with the current userID.

By default, log messages include the values from these global PHP variables: $_GET, $_POST, $_SESSION, $_COOKIE, $_FILES, and $_SERVER. This behaviour is modified by configuring the yii\log\Target::$logVars property with the names of variables that are desired to be include.

All log messages are maintained in an array by the logger object. The logger object flushed the recorded messages to the log targets each time the array accumulates a certain number of messages(default is 1000).

Step 3 − This number is customized by calling the flushInterval property.

Even when the logger object flushes log messages to log targets, they do not get exported immediately. The export occurs when a log target accumulates a certain number of messages(default is 1000).

Step 4 – This number is customized by configuring the exportInterval property.

Step 5 − The config/web.php file is modified by the code:

By the above code, a log application component is defined and the flushInterval and exportInteval properties are set to 1 so that log messages appear in the log files immediately. Also omit the levels property of the log target. It means that log messages of all categories(error, warning, info, trace) will appear in the log files.

Step 6 − A function actionLog() is created in the SiteController.

In the above code, four log messages of different categories are written to the log files.

Step 7 – Enter the URL http://localhost:8080/index.php?r=site/log in the address bar of the web browser. Log messages should appear under the app/runtime/logs directory in the app.log file.

Action Log Function

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Yii Topics