Class yii\log\Logger

继承yii\log\Logger » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable
可用自版本2.0
源代码 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.

公共属性

隐藏继承的属性

属性类型描述定义在
$behaviors yii\base\Behavior[] 附加到此组件的行为列表. 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. yii\log\Logger
$dispatcher yii\log\Dispatcher The message dispatcher yii\log\Logger
$elapsedTime float The total elapsed time in seconds for current request. 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. yii\log\Logger
$traceLevel integer How much call stack information (file name and line number) should be logged for each message. yii\log\Logger

公共方法

隐藏继承的方法

方法描述定义在
__call() 调用不是类方法的指定的方法. yii\base\Component
__clone() 在通过克隆现有对象创建对象之后调用此方法. yii\base\Component
__construct() 构造函数. yii\base\BaseObject
__get() 返回组件属性的值. yii\base\Component
__isset() 检查属性是否已设置, 即已定义且不为null. yii\base\Component
__set() 设置组件属性的值. yii\base\Component
__unset() 将组件属性设置为null. yii\base\Component
attachBehavior() 将行为附加到此组件. yii\base\Component
attachBehaviors() 将行为列表附加到组件. yii\base\Component
behaviors() 返回此组件应该具有的行为列表. yii\base\Component
calculateTimings() Calculates the elapsed time for the given log messages. yii\log\Logger
canGetProperty() 返回一个值, 指示属性是否可读取. yii\base\Component
canSetProperty() 返回一个值, 指示属性是否可设置. yii\base\Component
className() 返回此类的完全限定名称. yii\base\BaseObject
detachBehavior() 从组件中分离一个行为. yii\base\Component
detachBehaviors() 分离组件中的所有行为. yii\base\Component
ensureBehaviors() 确保在 behaviors() 中声明的行为已附加到此组件. yii\base\Component
flush() Flushes log messages from memory to targets. yii\log\Logger
getBehavior() 返回指定的行为对象. yii\base\Component
getBehaviors() 返回附加到此组件的所有行为. 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() 返回一个值, 指示是否有任何处理程序附加到指定的事件. yii\base\Component
hasMethod() 返回一个值, 指示方法是否已定义. yii\base\Component
hasProperty() 返回一个值, 指示属性是否已定义. 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() 从该组件分离现有的事件处理程序. yii\base\Component
on() 将事件处理程序附加到事件. yii\base\Component
trigger() 触发事件. yii\base\Component

常量

隐藏继承的常量

常量描述定义在
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

属性详情

$dbProfiling public 只读 属性

The first element indicates the number of SQL statements executed, and the second element the total time spent in SQL execution.

$dispatcher public 属性

The message dispatcher

$elapsedTime public 只读 属性

The total elapsed time in seconds for current request.

$flushInterval public 属性

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 属性

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 只读 属性

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.

public array getProfiling ( $categories = [], $excludeCategories = [] )
$traceLevel public 属性

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

方法详情

calculateTimings() public 方法

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 方法

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 方法

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 方法

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 方法

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 方法

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 方法

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

public void init ( )
log() public 方法

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.