PHP Class yii\log\Logger

A Logger instance can be accessed via Yii::getLogger(). You can call the method Logger::log to record a single log message. For convenience, a set of shortcut methods are provided for logging messages of various severity levels via the [[Yii]] class: - [[Yii::trace()]] - [[Yii::error()]] - [[Yii::warning()]] - [[Yii::info()]] - [[Yii::beginProfile()]] - [[Yii::endProfile()]] For more details and usage information on Logger, see the guide article on logging. When the application ends or [[flushInterval]] is reached, Logger will call Logger::flush to send logged messages to different log targets, such as [[FileTarget|file]], [[EmailTarget|email]], or [[DbTarget|database]], with the help of the [[dispatcher]].
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends yii\base\Component
Datei anzeigen Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$dispatcher the message dispatcher
$flushInterval how many messages should be logged before they are flushed from memory and sent to targets. Defaults to 1000, meaning the [[flush]] method will be invoked once every 1000 messages logged. Set this property to be 0 if you don't want to flush messages until the application terminates. This property mainly affects how much memory will be taken by the logged messages. A smaller value means less memory, but will increase the execution time due to the overhead of Logger::flush.
$messages logged messages. This property is managed by Logger::log and Logger::flush. Each log message is of the following structure: [ [0] => message (mixed, can be a string or some complex data, such as an exception object) [1] => level (integer) [2] => category (string) [3] => timestamp (float, obtained by microtime(true)) [4] => traces (array, debug backtrace, contains the application code call stacks) ]
$traceLevel how much call stack information (file name and line number) should be logged for each message. If it is greater than 0, at most that number of call stacks will be logged. Note that only application call stacks are counted.

Public Methods

Method Description
calculateTimings ( array $messages ) : array Calculates the elapsed time for the given log messages.
flush ( boolean $final = false ) Flushes log messages from memory to targets.
getDbProfiling ( ) : array Returns the statistical results of DB queries.
getElapsedTime ( ) : float Returns the total elapsed time since the start of the current request.
getLevelName ( integer $level ) : string Returns the text display of the specified level.
getProfiling ( array $categories = [], array $excludeCategories = [] ) : array Returns the profiling results.
init ( ) Initializes the logger by registering Logger::flush as a shutdown function.
log ( string | array $message, integer $level, string $category = 'application' ) Logs a message with the given type and category.

Method Details

calculateTimings() public method

Calculates the elapsed time for the given log messages.
public calculateTimings ( array $messages ) : array
$messages array the log messages obtained from profiling
return array timings. Each element is an array consisting of these elements: `info`, `category`, `timestamp`, `trace`, `level`, `duration`.

flush() public method

Flushes log messages from memory to targets.
public flush ( boolean $final = false )
$final boolean whether this is a final call during a request.

getDbProfiling() public method

The results returned include the number of SQL statements executed and the total time spent.
public getDbProfiling ( ) : array
return array the first element indicates the number of SQL statements executed, and the second element the total time spent in SQL execution.

getElapsedTime() public method

This method calculates the difference between now and the timestamp defined by constant YII_BEGIN_TIME which is evaluated at the beginning of BaseYii class file.
public getElapsedTime ( ) : float
return float the total elapsed time in seconds for current request.

getLevelName() public static method

Returns the text display of the specified level.
public static getLevelName ( integer $level ) : string
$level integer the message level, e.g. [[LEVEL_ERROR]], [[LEVEL_WARNING]].
return string the text display of the level

getProfiling() public method

By default, all profiling results will be returned. You may provide $categories and $excludeCategories as parameters to retrieve the results that you are interested in.
public getProfiling ( array $categories = [], array $excludeCategories = [] ) : array
$categories array list of categories that you are interested in. You can use an asterisk at the end of a category to do a prefix match. For example, 'yii\db\*' will match categories starting with 'yii\db\', such as 'yii\db\Connection'.
$excludeCategories array list of categories that you want to exclude
return array the profiling results. Each element is an array consisting of these elements: `info`, `category`, `timestamp`, `trace`, `level`, `duration`.

init() public method

Initializes the logger by registering Logger::flush as a shutdown function.
public init ( )

log() public method

If [[traceLevel]] is greater than 0, additional call stack information about the application code will be logged as well.
public log ( string | array $message, integer $level, string $category = 'application' )
$message string | array the message to be logged. This can be a simple string or a more complex data structure that will be handled by a [[Target|log target]].
$level integer the level of the message. This must be one of the following: `Logger::LEVEL_ERROR`, `Logger::LEVEL_WARNING`, `Logger::LEVEL_INFO`, `Logger::LEVEL_TRACE`, `Logger::LEVEL_PROFILE_BEGIN`, `Logger::LEVEL_PROFILE_END`.
$category string the category of the message.

Property Details

$dispatcher public_oe property

the message dispatcher
public $dispatcher

$flushInterval public_oe property

how many messages should be logged before they are flushed from memory and sent to targets. Defaults to 1000, meaning the [[flush]] method will be invoked once every 1000 messages logged. Set this property to be 0 if you don't want to flush messages until the application terminates. This property mainly affects how much memory will be taken by the logged messages. A smaller value means less memory, but will increase the execution time due to the overhead of Logger::flush.
public $flushInterval

$messages public_oe property

logged messages. This property is managed by Logger::log and Logger::flush. Each log message is of the following structure: [ [0] => message (mixed, can be a string or some complex data, such as an exception object) [1] => level (integer) [2] => category (string) [3] => timestamp (float, obtained by microtime(true)) [4] => traces (array, debug backtrace, contains the application code call stacks) ]
public $messages

$traceLevel public_oe property

how much call stack information (file name and line number) should be logged for each message. If it is greater than 0, at most that number of call stacks will be logged. Note that only application call stacks are counted.
public $traceLevel