Class yii\log\Logger
| 继承 | yii\log\Logger » yii\base\Component » yii\base\BaseObject |
|---|---|
| 实现 | yii\base\Configurable |
| 可用版本自 | 2.0 |
| 源码 | https://github.com/yiichina/yii2/blob/api/framework/log/Logger.php |
Logger 如果设置了 $dispatcher,它将把日志消息记在内存并且发送到所设置的日志 $dispatcher。
可以通过 Yii::getLogger() 获取 Logger 实例,并调用当前实例的 log() 方法去记录一条日志消息。
为了方便起见,
Yii 类提供了一组用于记录各种级别消息的快捷方法。
有关于 Logger 的详细信息和使用方法,请参考权威指南的 Logger 章节。
当应用程序结束或者执行到 $flushInterval 时 Logger 将会自动调用 flush() 从而通过 $dispatcher 把消息记录到如 file,email, 或者 database 不同的目标。
公共属性
| 属性 | 类型 | 描述 | 被定义在 |
|---|---|---|---|
| $behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
| $dbProfiling | array | 第一个元素表示执行的 SQL 语句的数量, 第二个元素是 SQL 执行花费的总时间。 | yii\log\Logger |
| $dispatcher | yii\log\Dispatcher | 消息调度器。 | yii\log\Logger |
| $elapsedTime | float | 当前请求的总耗时(以秒为单位)。 | yii\log\Logger |
| $flushInterval | integer | 在从内存刷新并发送到目标之前,应该记录多少消息。 默认值 1000,这意味着每记录 1000 条消息,就会调用 flush() 方法一次。 如果此属性设置为 0,程序终止之前将不会刷新消息。 此属性主要影响日志消息将占用多少内存。 较小的值意味着占用更少的内存,但是由于 [[flush()] 的开销,会增加执行时间。 | yii\log\Logger |
| $messages | array | 日志消息。该属性由 log() 和 flush() 操作。
下面是日志消息的结构:
`
[
[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. |
yii\log\Logger |
| $profiling | array | 分析的结果。每个元素都是由这些元素组成的数组:
info,category,timestamp,trace,level,duration,memory,memoryDiff,
从版本 2. |
yii\log\Logger |
| $traceLevel | integer | 应该为每个消息记录多少调用堆栈信息(文件名和行号)。 如果大于 0,最多记录调用堆栈的数量。 注意,只计算应用程序调用的堆栈信息。 | yii\log\Logger |
公共方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| __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() | 计算给定日志消息的运行时间。 | 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() | 将消息从内存中发送到指定目标。 | yii\log\Logger |
| getBehavior() | Returns the named behavior object. | yii\base\Component |
| getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component |
| getDbProfiling() | 返回数据库查询的统计结果。 返回的结果包括执行的 SQL 语句的数量和花费的总时间。 | yii\log\Logger |
| getElapsedTime() | 返回自当前请求开始以来经过的总时间。
该方法计算当前与在 yii\BaseYii 类文件常量
YII_BEGIN_TIME
定义的时间戳之间的差异。 |
yii\log\Logger |
| getLevelName() | Returns 具体的等级文字描述。 | yii\log\Logger |
| getProfiling() | 返回分析结果。 | 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() | 通过注册 flush() 作为一个关闭函数进行初始化 logger。 | yii\log\Logger |
| log() | 使用给定的类型和类别记录消息。 如果 $traceLevel 是一个大于 0 的值, 有关应用程序代码的其他调用堆栈信息也将被记录。 | 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 |
常量
| 常量 | 值 | 描述 | 被定义在 |
|---|---|---|---|
| LEVEL_ERROR | 1 | Error 消息等级。错误消息表示应用程序异常终止。 可能需要开发人员处理。 | yii\log\Logger |
| LEVEL_INFO | 4 | Informational 消息等级。信息性消息是指包含特定信息的消息, 供开发人员评审。 | yii\log\Logger |
| LEVEL_PROFILE | 64 | Profiling 消息等级。这表明消息是用于分析。 | yii\log\Logger |
| LEVEL_PROFILE_BEGIN | 80 | Profiling 消息等级。这表明消息是用于分析目的的, 标志着分析块的开头。 | yii\log\Logger |
| LEVEL_PROFILE_END | 96 | Profiling 消息等级。这表明消息是用于分析目的的, 标志着分析块的结束。 | yii\log\Logger |
| LEVEL_TRACE | 8 | Tracing 消息等级。跟踪消息是揭示代码执行流的消息。 | yii\log\Logger |
| LEVEL_WARNING | 2 | Warning 消息等级。警告消息是指一些异常发生, 但是应用程序能够继续运行,开发人员应该注意这个消息。 | yii\log\Logger |
属性详情
第一个元素表示执行的 SQL 语句的数量, 第二个元素是 SQL 执行花费的总时间。
消息调度器。
当前请求的总耗时(以秒为单位)。
在从内存刷新并发送到目标之前,应该记录多少消息。 默认值 1000,这意味着每记录 1000 条消息,就会调用 flush() 方法一次。 如果此属性设置为 0,程序终止之前将不会刷新消息。 此属性主要影响日志消息将占用多少内存。 较小的值意味着占用更少的内存,但是由于 [[flush()] 的开销,会增加执行时间。
日志消息。该属性由 log() 和 flush() 操作。 下面是日志消息的结构:
[
[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.
]
分析的结果。每个元素都是由这些元素组成的数组:
info,category,timestamp,trace,level,duration,memory,memoryDiff,
从版本 2.0.11 起,添加 memory 和 memoryDiff 元素。
应该为每个消息记录多少调用堆栈信息(文件名和行号)。 如果大于 0,最多记录调用堆栈的数量。 注意,只计算应用程序调用的堆栈信息。
方法详情
计算给定日志消息的运行时间。
| public array calculateTimings($messages) | ||
| $messages | array | 从分析中获得的日志消息 |
| return | array | Timings。每个元素都是由这些元素组成的数组:
|
|---|---|---|
将消息从内存中发送到指定目标。
| public void flush($final = false) | ||
| $final | boolean | 是否为请求期间的最终调用。 |
返回数据库查询的统计结果。 返回的结果包括执行的 SQL 语句的数量和花费的总时间。
| public array getDbProfiling() | ||
| return | array | 第一个元素表示执行的 SQL 语句的数量, 第二个元素是 SQL 执行花费的总时间。 |
|---|---|---|
返回自当前请求开始以来经过的总时间。
该方法计算当前与在 yii\BaseYii 类文件常量
YII_BEGIN_TIME
定义的时间戳之间的差异。
| public float getElapsedTime() | ||
| return | float | 当前请求的总耗时(以秒为单位)。 |
|---|---|---|
Returns 具体的等级文字描述。
| public static string getLevelName($level) | ||
| $level | integer | 消息等级,如 LEVEL_ERROR,LEVEL_WARNING。 |
| return | string | 返回当前等级的文字描述。 |
|---|---|---|
返回分析结果。
默认情况下,将返回所有分析结果。你可以提供
$categories 和 $excludeCategories 作为参数来获取
你需要关注的结果。
| public array getProfiling($categories = [], $excludeCategories = []) | ||
| $categories | array | 您感兴趣的类别的列表。 您可以在类别的末尾使用星号来匹配前缀。 'yii\db*' 将匹配以 'yii\db\' 开头的类别, 例如 'yii\db\Connection'。 |
| $excludeCategories | array | 要排除的类别的列表 |
| return | array | 分析的结果。每个元素都是由这些元素组成的数组:
|
|---|---|---|
通过注册 flush() 作为一个关闭函数进行初始化 logger。
| public void init() |
使用给定的类型和类别记录消息。 如果 $traceLevel 是一个大于 0 的值, 有关应用程序代码的其他调用堆栈信息也将被记录。
| public void log($message, $level, $category = 'application') | ||
| $message | string|array | 要被记录的消息。它可以是一个简单的字符串或者一个复杂的数据结构并使用指定的 log target 进行处理。 |
| $level | integer | 消息等级。必须是以下类型之一:
|
| $category | string | 当前消息类型分类。 |