yii之Log(日志)使用

作者:zccst


Yii 提供了一个灵活可扩展的日志功能。记录的日志可以通过日志级别和信息分类进行归类。通过使用级别和分类过滤器,所选的信息还可以进一步路由到不同的目的地,例如一个文件,Email,浏览器窗口等。

[size=large]1. 信息记录[/size]

信息可以通过 Yii::log 或 Yii::trace 记录。其区别是后者只在当应用程序运行在 调试模式(debug mode) 中时才会记录信息。

Yii::log($message, $level, $category);
Yii::trace($message, $category);

当记录信息时,我们需要指定它的分类和级别分类是一段格式类似于 路径别名 的字符串。例如,如果一条信息是在 CController 中记录的,我们可以使用 system.web.CController 作为分类。信息级别应该是下列值中的一种:

trace: 这是在 Yii::trace 中使用的级别。它用于在开发中跟踪程序的执行流程。

info: 这个用于记录普通的信息。

profile: 这个是性能概述(profile)。下面马上会有更详细的说明。

warning: 这个用于警告(warning)信息。

error: 这个用于致命错误(fatal error)信息。


[size=large]2. 信息路由[/size]

通过 Yii::log 或 Yii::trace 记录的信息是保存在内存中的。我们通常需要将它们显示到浏览器窗口中,或者将他们保存到一些持久存储例如文件、Email中。这个就叫作 信息路由,例如,发送信息到不同的目的地。

在 Yii 中,信息路由是由一个叫做 CLogRouter 的应用组件管理的。它负责管理一系列称作 日志路由 的东西。每个日志路由代表一个单独的日志目的地。通过一个日志路由发送的信息会被他们的级别和分类过滤。

要使用信息路由,我们需要安装并预加载一个 CLogRouter 应用组件。我们也还需要配置它的 routes 属性为我们想要的那些日志路由。下面的代码演示了一个所需的 应用配置 示例:

array(
......
'preload'=>array('log'),
'components'=>array(
......
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'trace, info',
'categories'=>'system.*',
),
array(
'class'=>'CEmailLogRoute',
'levels'=>'error, warning',
'emails'=>'admin@example.com',
),
),
),
),
)

在上面的例子中,我们定义了两个日志路由。第一个是 CFileLogRoute ,它会把信息保存在位于应用程序 runtime 目录中的一个文件中。而且只有级别为 trace 或 info 、分类以 system. 开头的信息才会被保存。 第二个路由是 CEmailLogRoute ,它会将信息发送到指定的 email 地址,且只有级别为 error 或 warning 的才会发送。

在 Yii 中,有下列几种日志路由可用:

CDbLogRoute: 将信息保存到数据库的表中。
CEmailLogRoute: 发送信息到指定的 Email 地址。
[color=red]CFileLogRoute: 保存信息到应用程序 runtime 目录中的一个文件中。 [/color]
CWebLogRoute: 将 信息 显示在当前页面的底部。
CProfileLogRoute: 在页面的底部显示概述(profiling)信息。

注:信息路由发生在当前请求周期最后的 onEndRequest 事件触发时。 要显式终止当前请求过程,请调用 CApplication::end() 而不是使用 die() 或 exit(),因为 CApplication::end() 将会触发 onEndRequest 事件, 这样信息才会被顺利地记录。


[size=large]3. 信息过滤[/size]

正如我们所提到的,信息可以在他们被发送到一个日志路由之前通过它们的级别和分类过滤。这是通过设置对应日志路由的 levels 和 categories 属性完成的。多个级别或分类应使用逗号连接。

由于信息分类是类似 xxx.yyy.zzz 格式的,我们可以将其视为一个分类层级。具体地,我们说 xxx 是 xxx.yyy 的父级,而xxx.yyy 又是 xxx.yyy.zzz 的父级。这样我们就可以使用 xxx.* 表示分类 xxx 及其所有的子级和孙级分类


[size=large]4. 记录上下文信息[/size]

从版本 1.0.6 起,我们可以设置记录附加的上下文信息,比如 PHP 的预定义变量(例如 $_GET, $_SERVER),session ID,用户名等。这是通过指定一个日志路由的 CLogRoute::filter属性为一个合适的日志过滤规则实现的。

The framework comes with the convenient CLogFilter that may be used as the needed log filter in most cases. By default, CLogFilter will log a message with variables like $_GET, $_SERVER which often contains valuable system context information. CLogFilter can also be configured to prefix each logged message with session ID, username, etc., which may greatly simplifying the global search when we are checking the numerous logged messages.

The following configuration shows how to enable logging context information. Note that each log route may have its own log filter. And by default, a log route does not have a log filter.

array(
......
'preload'=>array('log'),
'components'=>array(
......
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error',
'filter'=>'CLogFilter',
),
...other log routes...
),
),
),
)

Starting from version 1.0.7, Yii supports logging call stack information in the messages that are logged by calling Yii::trace. This feature is disabled by default because it lowers performance. To use this feature, simply define a constant named YII_TRACE_LEVEL at the beginning of the entry script (before including yii.php) to be an integer greater than 0. Yii will then append to every trace message with the file name and line number of the call stacks belonging to application code. The number YII_TRACE_LEVEL determines how many layers of each call stack should be recorded. This information is particularly useful during development stage as it can help us identify the places that trigger the trace messages.


[size=large]5. Performance Profiling[/size]

Performance profiling is a special type of message logging. Performance profiling can be used to measure the time needed for the specified code blocks and find out what the performance bottleneck is.

To use performance profiling, we need to identify which code blocks need to be profiled. We mark the beginning and the end of each code block by inserting the following methods:

Yii::beginProfile('blockID');
...code block being profiled...
Yii::endProfile('blockID');

where blockID is an ID that uniquely identifies the code block.

Note, code blocks need to be nested properly. That is, a code block cannot intersect with another. It must be either at a parallel level or be completely enclosed by the other code block.

To show profiling result, we need to install a CLogRouter application component with a CProfileLogRoute log route. This is the same as we do with normal message routing. The CProfileLogRoute route will display the performance results at the end of the current page.


[size=large]6. Profiling SQL Executions[/size]

Profiling is especially useful when working with database since SQL executions are often the main performance bottleneck of an application. While we can manually insert beginProfile and endProfile statements at appropriate places to measure the time spent in each SQL execution, starting from version 1.0.6, Yii provides a more systematic approach to solve this problem.

By setting CDbConnection::enableProfiling to be true in the application configuration, every SQL statement being executed will be profiled. The results can be readily displayed using the aforementioned CProfileLogRoute, which can show us how much time is spent in executing what SQL statement. We can also call CDbConnection::getStats() to retrieve the total number SQL statements executed and their total execution time.


使用实例:想查看如下结果,但由于较大,var_dump时浏览器会崩溃,于是想到如果知道执行的sql是什么,那么就可以推出执行后的结果是什么。


//1,在 ../config/main.php里增加'class'=>'CProfileLogRoute'
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning',
),
/* 此处为本次增加 */
array(
'class'=>'CProfileLogRoute',
),
// uncomment the following to show log messages on web pages
/*
array(
'class'=>'CWebLogRoute',
),
*/
),
),

//2,在源码处增加三行代码
$c = new CDbCriteria();
$c->join = "JOIN idc_user on t.id=idc_user.user_id";
$c->condition = "idc_user.idc_id=$idc_id";

Yii::beginProfile('block1');
$r = User::model()->with('Idcs')->findAll($c);
Yii::endProfile('block1');


//整理打印结果如下
SELECT `t`.`id` AS `t0_c0` , `t`.`username` AS `t0_c1` , `t`.`password` AS `t0_c2` , `t`.`display_name` AS `t0_c3` , `t`.`tel` AS `t0_c4` , `t`.`mobile` AS `t0_c5` , `t`.`email` AS `t0_c6` , `t`.`hi_id` AS `t0_c7` , `t`.`company_id` AS `t0_c8` , `t`.`last_login_at` AS `t0_c9` , `t`.`last_pwd_updated_at` AS `t0_c10` , `t`.`created_at` AS `t0_c11` , `Idcs`.`id` AS `t1_c0` , `Idcs`.`name` AS `t1_c1` , `Idcs`.`desc` AS `t1_c2` , `Idcs`.`created_at` AS `t1_c3` , `Idcs`.`chinese_name` AS `t1_c4` , `Idcs`.`clientele` AS `t1_c5` , `Idcs`.`contact_man` AS `t1_c6` , `Idcs`.`contact_phone` AS `t1_c7` , `Idcs`.`post_code` AS `t1_c8` , `Idcs`.`address` AS `t1_c9` , `Idcs`.`email` AS `t1_c10` , `Idcs`.`weight` AS `t1_c11` , `Idcs`.`pool_id` AS `t1_c12` , `Idcs`.`provider_id` AS `t1_c13`
FROM `user` `t`
JOIN idc_user ON t.id = idc_user.user_id
LEFT OUTER JOIN `idc_user` `Idcs_Idcs` ON ( `t`.`id` = `Idcs_Idcs`.`user_id` )
LEFT OUTER JOIN `idc` `Idcs` ON ( `Idcs`.`id` = `Idcs_Idcs`.`idc_id` )
WHERE (
idc_user.idc_id =6
)

//打印结果如下
block2 1 0.01122 0.01122 0.01122 0.01122
system.db.CDbCommand.query(SELECT `t`.`id` AS `t0_c0`, `t`.`username` AS `t0_c1`, `t`.`password` AS `t0_c2`, `t`.`display_name` AS `t0_c3`, `t`.`tel` AS `t0_c4`, `t`.`mobile` AS `t0_c5`, `t`.`email` AS `t0_c6`, `t`.`hi_id` AS `t0_c7`, `t`.`company_id` AS `t0_c8`, `t`.`last_login_at` AS `t0_c9`, `t`.`last_pwd_updated_at` AS `t0_c10`, `t`.`created_at` AS `t0_c11`, `Idcs`.`id` AS `t1_c0`, `Idcs`.`name` AS `t1_c1`, `Idcs`.`desc` AS `t1_c2`, `Idcs`.`created_at` AS `t1_c3`, `Idcs`.`chinese_name` AS `t1_c4`, `Idcs`.`clientele` AS `t1_c5`, `Idcs`.`contact_man` AS `t1_c6`, `Idcs`.`contact_phone` AS `t1_c7`, `Idcs`.`post_code` AS `t1_c8`, `Idcs`.`address` AS `t1_c9`, `Idcs`.`email` AS `t1_c10`, `Idcs`.`weight` AS `t1_c11`, `Idcs`.`pool_id` AS `t1_c12`, `Idcs`.`provider_id` AS `t1_c13` FROM `user` `t` JOIN idc_user on t.id=idc_user.user_id LEFT OUTER JOIN `idc_user` `Idcs_Idcs` ON (`t`.`id`=`Idcs_Idcs`.`user_id`) LEFT OUTER JOIN `idc` `Idcs` ON (`Idcs`.`id`=`Idcs_Idcs`.`idc_id`) WHERE (idc_user.idc_id=6)) 1 0.00086 0.00086 0.00086 0.00086
system.db.CDbCommand.query(SHOW COLUMNS FROM `user`) 1 0.00083 0.00083 0.00083 0.00083
system.db.CDbCommand.query(SHOW COLUMNS FROM `idc`) 1 0.00078 0.00078 0.00078 0.00078
system.db.CDbCommand.query(SHOW COLUMNS FROM `idc_user`) 1 0.00060 0.00060 0.00060 0.00060
system.db.CDbCommand.query( SELECT data FROM YiiSession WHERE expire>1339135578 AND id=:id ) 1 0.00047 0.00047 0.00047 0.00047
system.db.CDbCommand.query(SELECT * FROM `user` `t` WHERE `t`.`id`=8 LIMIT 1) 1 0.00045 0.00045 0.00045 0.00045
system.db.CDbCommand.query(SHOW CREATE TABLE `user`) 1 0.00038 0.00038 0.00038 0.00038
system.db.CDbCommand.query(SHOW CREATE TABLE `idc`) 1 0.00038 0.00038 0.00038 0.00038
system.db.CDbCommand.query(SHOW CREATE TABLE `idc_user`) 1 0.00036 0.00036 0.00036 0.00036





如果您觉得本文的内容对您的学习有所帮助,您可以微信:
[img]http://dl2.iteye.com/upload/attachment/0109/0668/fb266dfa-95ca-3d09-b41e-5f04a19ba9a1.png[/img]
CANopen中SDO、PDO字典以及COB-ID理解 CANopen SDO PDO 阅读详情

相关推荐

Vue3 - 详细实现虚拟列表前端虚拟滚动列表解决方案,vue3长列表优化之虚拟列表,解决列表动态高度不固定高度及图片视频图文异步请求加载问题,虚拟列表DOM大量数据同时加载渲染卡顿太慢及下滑列表闪烁

vue3,vue3虚拟列表,用vue3实现一个虚拟列表,长列表高度不一致如何实现虚拟滚动加载列表,虚拟列表有图片视频,图文列表,上万条数据同时渲染解决,长列表dom渲染太卡,列表滚动条加载超多数据,瀑布流,在Vue3中如何实现列表的虚拟滚动,虚拟列表滑动白屏闪烁闪一下,虚拟列表每个项的高度不确定,可动态改变高度,vue3虚拟滚动列表,页面数据太多加载很慢怎么办,vue3实现不定高的虚拟列表,定高和不定高,虚拟列表非固定高度,vue3长列表优化之虚拟列表实现,vue3虚拟列表高度不固定,用vue3实现一个虚拟

🏆 前端领域优质创作者 1万+

yii配置log日志功能

首先修改config下的web.php或者main.php 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => 'yii\log\FileTarget', ...

qq_34345149的博客 1016

Saber仿真电源案例详解.rar_saber_saber电源仿真_电源saber_电源仿真_电源仿真 saber

Saber仿真电源案例详解,可以很好的帮组大家使用saber来搭建电源的仿真

Yiilog分析

我们可以通过Yii日志级别和信息分类进行归类,所选的信息还可以进一步设置信息路由到答不同的目的地,例如一个文件,数据库,Email,浏览器窗口等。 日志级别有: * trace: 这是在 Yii::trace 中使用的级别。它用于在开发中 跟踪程序的执行流程。 * info: 这个用于记录普通的信息。 * profile: 这个是性能概述(pr...

xishizhaohua的专栏 269

配置yii2 log日志

        'log' => [            'traceLevel' => YII_DEBUG ? 3 : 0,            'targets' => [                [                    'class' => 'yii\log\FileTarget',                    'levels' ...

weixin_37581116的博客 1719

日志功能--yii::trace()yii::log()

Yii提供了一个灵活的和可扩展的日志记录功能。根据日志级别和消息的类别可以将消息记录分类。使用级别和类别过滤器,选中的消息可以进一步记录到不同的目标,如文件、电子邮件、浏览器窗口等。 1、开启trace()log() 日志路由class: CDbLogRoute: 将信息保存到数据库的表中。 CEmailLogRoute: 发送信息到指定的 Email 地址。 CFileL

刘锐群的笔记 4281

Yii::log()Yii::trace()日志使用

  首先在config文件中设置log   'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( array( ...

做个好的手艺人 739

yii 打印sql运行的日志log文件中

最近突然意识到日志是很好滴debug工具,所以研究了一下yii日志配置,想想应该还会有像我这样的小白不懂这些问题的,就分享一下了。 通过配置日志文件,方便我们快速的定位问题,config/pubilc.php 中配置,这个想必都知道吧,在这个文件中设置我们的log日志配置: 在这个文件中加入如下的配置代码: 'log'=>array( 'class'=>'CLogRou...

lchmyhua88的博客 1938

Yii2中记录log日志的配置和使用方法

日志配置 'components' => [     'log' => [  'traceLevel' => YII_DEBUG? 3 : 0,           'targets' => [               [                 'class' =>'yii\log\FileTarget',           'levels' =>

qiuxuemei915的专栏 6883

yii2 日志操作(log)以及写入不同的log文件

yii2 日志操作(log)以及写入不同的log文件

Simael的专栏 2万+

Yii2 日志log)配置与使用

1.配置在配置文件main.php或者main-local.php中配置参数return [ //日志使用时需要使用的 'bootstrap' => ['log'], 'components' => [ //日志配置 'log' => [ 'targets' => [ /*

高玉龙的博客 4万+

Yii2 log 模块,消息日志

Yii::trace():记录一条消息去跟踪一段代码是怎样运行的。这主要在开发的时候使用Yii::info():记录一条消息来传达一些有用的信息。 Yii::warning():记录一个警告消息用来指示一些已经发生的意外。 Yii::error():记录一个致命的错误,这个错误应该尽快被检查 1). 配置 //common/config/main.php 配置 'component...

Dafei4的博客 1511

关于yii2 的db log 日志 错误处理errorHandler

log 通过配置Web.config来完成 1 数据库增加 ‘前缀_log’表 2 配置Web.config 'bootstrap' => ['log'], 'components' =>[ 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, //级别 't...

weixin_33832340的博客 271

yii2框架关于log的配置

log里面记录了大量的不需需要的401 401的错误 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'],

cominglately的博客 905

Yii2的log组件一共包含哪些部分?使用场景是什么?底层原理是什么?

【代码】Yii2的log组件一共包含哪些部分?使用场景是什么?底层原理是什么?

长风破浪会有时的博客 720

Yii将需要的log写入到数据库

一,配置环境(main.php) 'log' => array(    'class' => 'CLogRouter',    'routes' => array(          //写入文件的log,默认categories为application          array(               'class' => 'CFileLogRoute',        

Jerry's Home 1627

YII Framework学习教程-YII日志

YII中的日志很好很强大,允许你把日志信息存放到数据库,发送到制定email,存放咋文件中,意见显示页面是,甚至可以用来做性能分析。 YII日志的基本配置:/yii_dev/testwebap/protected/config/main.php 'log'=>array( 'class'=>'CLogRouter', 'routes'=>a

sondx的专栏 1052

Yii 日志操作

今天来写一下Yii 框架的日志操作 1.在Yii项目下的config/ 主配置文件中 (app.php/main.php/…项目可能配置文件不同) 找到 $config 下的components数组如下 在这里配置日志,举例如下: 'log' => isset($local['log']) ? $local['log'] : [ 'traceLevel' => YII_DEBUG ? 3 : 0, //日志目标,可定义多个

weixin_46044420的博客 556

yii2 log使用

文档:http://www.yiichina.com/doc/guide/2.0/runtime-logging 1.在frontend/config/main.php 中进行加入log的配置 'log' =>[ # 追踪级别 # 消息跟踪级别 # 在开发的时候,通常希望看到每个日志消息来自哪里。这个是能够被实现的,通过配置 log 组件的 yii\log\Dispa

Terry - 专注外贸B2C 3204
上一篇: 互联网行业变迁
下一篇: 命令行方式使用FTP实战练习
zccst
博客等级 码龄19年 153粉丝 661原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值