0 follower

Class yii\log\Logger

Inheritanceyii\log\Logger » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/log/Logger.php

Logger records logged messages in memory and sends them to different targets if $dispatcher is set.

A Logger instance can be accessed via Yii::getLogger(). You can call the method 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:

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 flush() to send logged messages to different log targets, such as file, email, or database, with the help of the $dispatcher.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$behaviors yii\base\Behavior[] List of behaviors attached to this component. This property is read-only. yii\base\Component
$dbProfiling array The first element indicates the number of SQL statements executed, and the second element the total time spent in SQL execution. This property is read-only. yii\log\Logger
$dispatcher yii\log\Dispatcher The message dispatcher yii\log\Logger
$elapsedTime float The total elapsed time in seconds for current request. This property is read-only. yii\log\Logger
$flushInterval integer How many messages should be logged before they are flushed from memory and sent to targets. yii\log\Logger
$messages array Logged messages. yii\log\Logger
$profiling array The profiling results. Each element is an array consisting of these elements: info, category, timestamp, trace, level, duration, memory, memoryDiff. The memory and memoryDiff values are available since version 2.0.11. This property is read-only. yii\log\Logger
$traceLevel integer How much call stack information (file name and line number) should be logged for each message. yii\log\Logger

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
calculateTimings() Calculates the elapsed time for the given log messages. yii\log\Logger
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\BaseObject
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
flush() Flushes log messages from memory to targets. yii\log\Logger
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getDbProfiling() Returns the statistical results of DB queries. yii\log\Logger
getElapsedTime() Returns the total elapsed time since the start of the current request. yii\log\Logger
getLevelName() Returns the text display of the specified level. yii\log\Logger
getProfiling() Returns the profiling results. yii\log\Logger
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() Initializes the logger by registering flush() as a shutdown function. yii\log\Logger
log() Logs a message with the given type and category. yii\log\Logger
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
trigger() Triggers an event. yii\base\Component

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
LEVEL_ERROR 1 Error message level. An error message is one that indicates the abnormal termination of the application and may require developer's handling. yii\log\Logger
LEVEL_INFO 4 Informational message level. An informational message is one that includes certain information for developers to review. yii\log\Logger
LEVEL_PROFILE 64 Profiling message level. This indicates the message is for profiling purpose. yii\log\Logger
LEVEL_PROFILE_BEGIN 80 Profiling message level. This indicates the message is for profiling purpose. It marks the beginning of a profiling block. yii\log\Logger
LEVEL_PROFILE_END 96 Profiling message level. This indicates the message is for profiling purpose. It marks the end of a profiling block. yii\log\Logger
LEVEL_TRACE 8 Tracing message level. An tracing message is one that reveals the code execution flow. yii\log\Logger
LEVEL_WARNING 2 Warning message level. A warning message is one that indicates some abnormal happens but the application is able to continue to run. Developers should pay attention to this message. yii\log\Logger

Property Details

$dbProfiling public property

The first element indicates the number of SQL statements executed, and the second element the total time spent in SQL execution. This property is read-only.

public array $dbProfiling null
$dispatcher public property

The message dispatcher

$elapsedTime public property

The total elapsed time in seconds for current request. This property is read-only.

public float $elapsedTime null
$flushInterval public 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 flush().

public integer $flushInterval 1000
$messages public property

Logged messages. This property is managed by log() and 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)
  [5] => memory usage in bytes (int, obtained by memory_get_usage()), available since version 2.0.11.
]
public array $messages = []
$profiling public property

The profiling results. Each element is an array consisting of these elements: info, category, timestamp, trace, level, duration, memory, memoryDiff. The memory and memoryDiff values are available since version 2.0.11. This property is read-only.

public array $profiling null
$traceLevel public 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 integer $traceLevel 0

Method Details

calculateTimings() public method

Calculates the elapsed time for the given log messages.

public array calculateTimings ( $messages )
$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, memory, memoryDiff. The memory and memoryDiff values are available since version 2.0.11.

flush() public method

Flushes log messages from memory to targets.

public void flush ( $final false )
$final boolean

Whether this is a final call during a request.

getDbProfiling() public method

Returns the statistical results of DB queries.

The results returned include the number of SQL statements executed and the total time spent.

public array getDbProfiling ( )
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

Returns the total elapsed time since the start of the current request.

This method calculates the difference between now and the timestamp defined by constant YII_BEGIN_TIME which is evaluated at the beginning of yii\BaseYii class file.

public float getElapsedTime ( )
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 string getLevelName ( $level )
$level integer

The message level, e.g. LEVEL_ERROR, LEVEL_WARNING.

return string

The text display of the level

getProfiling() public method

Returns the profiling results.

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 array getProfiling ( $categories = [], $excludeCategories = [] )
$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, memory, memoryDiff. The memory and memoryDiff values are available since version 2.0.11.

init() public method

Initializes the logger by registering flush() as a shutdown function.

public void init ( )
log() public method

Logs a message with the given type and category.

If $traceLevel is greater than 0, additional call stack information about the application code will be logged as well.

public void log ( $message, $level, $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 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.