Yii提供了一个灵活的和可扩展的日志记录功能。根据日志级别和消息的类别可以将消息记录分类。使用级别和类别过滤器,选中的消息可以进一步记录到不同的目标,如文件、电子邮件、浏览器窗口等。
1、开启trace()和log()
日志路由class:
CDbLogRoute: 将信息保存到数据库的表中。
CEmailLogRoute: 发送信息到指定的 Email 地址。
CFileLogRoute: 保存信息到应用程序 runtime 目录中的一个文件中。
CWebLogRoute: 将 信息 显示在当前页面的底部。
CProfileLogRoute: 在页面的底部显示概述(profiling)信息。
信息级别levels:
trace: 这是在 Yii::trace 中使用的级别。它用于在开发中 跟踪程序的执行流程。
info: 这个用于记录普通的信息。
profile: 这个是性能概述(profile)。
warning: 这个用于警告(warning)信息。
error: 这个用于致命错误(fatal error)信息。
文件 config/main.php:
2、trace()和log()的区别:
trace()只会在调试模式下生效,即开启debug的时候;
trace()不分level,但log()能设置levels参数。
文件 index.php:
3、使用方式:
1、开启trace()和log()
日志路由class:
CDbLogRoute: 将信息保存到数据库的表中。
CEmailLogRoute: 发送信息到指定的 Email 地址。
CFileLogRoute: 保存信息到应用程序 runtime 目录中的一个文件中。
CWebLogRoute: 将 信息 显示在当前页面的底部。
CProfileLogRoute: 在页面的底部显示概述(profiling)信息。
信息级别levels:
trace: 这是在 Yii::trace 中使用的级别。它用于在开发中 跟踪程序的执行流程。
info: 这个用于记录普通的信息。
profile: 这个是性能概述(profile)。
warning: 这个用于警告(warning)信息。
error: 这个用于致命错误(fatal error)信息。
文件 config/main.php:
'db'=>array(
... ...
/*
* 下面是调试信息,显示执行的数据库操作
*/
'enableProfiling'=>YII_DEBUG,
'enableParamLogging'=>YII_DEBUG,
),
... ...
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'trace, info',
'categories'=>'system.*',
),
array(
'class'=>'CEmailLogRoute',
'levels'=>'error, warning',
'emails'=>'admin@example.com',
),
),
),
2、trace()和log()的区别:
trace()只会在调试模式下生效,即开启debug的时候;
trace()不分level,但log()能设置levels参数。
文件 index.php:
<?php
// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',9);//3
3、使用方式:
Yii::log($message, $level, $category);
Yii::trace($message, $category);