YII Framework框架教程之日志用法詳解
本文實(shí)例講述了YII Framework框架日志用法。分享給大家供大家參考,具體如下:
日志的作用(此處省略1000字)
YII中的日志很好很強(qiáng)大,允許你把日志信息存放到數(shù)據(jù)庫(kù),發(fā)送到制定email,存放咋文件中,意見(jiàn)顯示頁(yè)面是,甚至可以用來(lái)做性能分析。
YII中日志的基本配置:/yii_dev/testwebap/protected/config/main.php
'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( array( 'class'=>'CFileLogRoute', 'levels'=>'error, warning', ), // uncomment the following to show log messages on web pages /* array( 'class'=>'CWebLogRoute', ), */ ), ),
YII中日志的基本使用:
可以通過(guò)YII提供的Yii::log和Yii::trace進(jìn)行日志信息的輸出,兩者的區(qū)別看看定義就知道了。
函數(shù)定義
public static function trace($msg,$category='application') { if(YII_DEBUG) self::log($msg,CLogger::LEVEL_TRACE,$category); } public static function log($msg,$level=CLogger::LEVEL_INFO,$category='application') { if(self::$_logger===null) self::$_logger=new CLogger; if(YII_DEBUG && YII_TRACE_LEVEL>0 && $level!==CLogger::LEVEL_PROFILE) { $traces=debug_backtrace(); $count=0; foreach($traces as $trace) { if(isset($trace['file'],$trace['line']) && strpos($trace['file'],YII_PATH)!==0) { $msg.="\nin ".$trace['file'].' ('.$trace['line'].')'; if(++$count>=YII_TRACE_LEVEL) break; } } } self::$_logger->log($msg,$level,$category); }
$msg:你要輸出的日志信息
$category:日志信息所屬分類
$level:日志信息的級(jí)別:
const LEVEL_TRACE='trace';用于調(diào)試環(huán)境,追蹤程序執(zhí)行流程
const LEVEL_WARNING='warning';警告信息
const LEVEL_ERROR='error';致命錯(cuò)誤信息
const LEVEL_INFO='info';普通提示信息
const LEVEL_PROFILE='profile';性能調(diào)試信息
基本使用方法舉例
<?php class DefaultController extends Controller { public function actionCache () { $category='system.testmod.defaultController'; $level=CLogger::LEVEL_INFO; $msg='action begin '; Yii::log($msg,$level,$category); } }
YII中日志的輸出位置
上文提到Y(jié)II中日志的輸出位置可以定義為很多位置。主要通過(guò)配置文件修改例如:
'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( array( 'class'=>'CFileLogRoute', 'levels'=>'error, warning', ), // uncomment the following to show log messages on web pages array( 'class'=>'CWebLogRoute', ), ), ),
不僅輸出到日志文件中,還輸出到web頁(yè)面上。
配置文件中:
routes用于配置日志輸出的位置,
class是日志,日志路由的類名
levels是日志的頂級(jí),字符串序列,用都好分割。具體對(duì)應(yīng)CLooger中的常量
注意:
日志文件的存放位置是:/yii_dev/testwebap/protected/runtime/application.log
官方的日志介紹的很詳細(xì),但是后半部分中文翻譯缺失了,這里進(jìn)行翻譯補(bǔ)全。
Yii 提供了一個(gè)靈活可擴(kuò)展的日志功能。記錄的日志 可以通過(guò)日志級(jí)別和信息分類進(jìn)行歸類。通過(guò)使用 級(jí)別和分類過(guò)濾器,所選的信息還可以進(jìn)一步路由到 不同的目的地,例如一個(gè)文件,Email,瀏覽器窗口等。
1. 信息記錄
信息可以通過(guò) Yii::log 或 Yii::trace 記錄。其 區(qū)別是后者只在當(dāng)應(yīng)用程序運(yùn)行在 調(diào)試模式(debug mode) 中時(shí)才會(huì)記錄信息。
Yii::log($message, $level, $category); Yii::trace($message, $category);
當(dāng)記錄信息時(shí),我們需要指定它的分類和級(jí)別 分類是一段格式類似于 路徑別名 的字符串。 例如,如果一條信息是在 CController 中記錄的,我們可以使用 system.web.CController 作為分類。信息級(jí)別應(yīng)該是下列值中的一種:
trace: 這是在 Yii::trace 中使用的級(jí)別。它用于在開(kāi)發(fā)中 跟蹤程序的執(zhí)行流程。
info: 這個(gè)用于記錄普通的信息。
profile: 這個(gè)是性能概述(profile)。下面馬上會(huì)有更詳細(xì)的說(shuō)明。
warning: 這個(gè)用于警告(warning)信息。
error: 這個(gè)用于致命錯(cuò)誤(fatal error)信息。
2. 信息路由
通過(guò) Yii::log 或 Yii::trace 記錄的信息是保存在內(nèi)存中的。 我們通常需要將它們顯示到瀏覽器窗口中,或者將他們保存到一些 持久存儲(chǔ)例如文件、Email中。這個(gè)就叫作 信息路由,例如, 發(fā)送信息到不同的目的地。
在 Yii 中,信息路由是由一個(gè)叫做 CLogRouter 的應(yīng)用組件管理的。 它負(fù)責(zé)管理一系列稱作 日志路由 的東西。每個(gè)日志路由 代表一個(gè)單獨(dú)的日志目的地。通過(guò)一個(gè)日志路由發(fā)送的信息會(huì)被他們的級(jí)別和分類過(guò)濾。
要使用信息路由,我們需要安裝并預(yù)加載一個(gè) CLogRouter 應(yīng)用組件。我們也還需要配置它的 routes 屬性為我們想要的那些日志路由。 下面的代碼演示了一個(gè)所需的 應(yīng)用配置 示例:
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', ), ), ), ), )
在上面的例子中,我們定義了兩個(gè)日志路由。第一個(gè)是 CFileLogRoute ,它會(huì)把信息保存在位于應(yīng)用程序 runtime 目錄中的一個(gè)文件中。 而且只有級(jí)別為 trace 或 info 、分類以 system. 開(kāi)頭的信息才會(huì)被保存。 第二個(gè)路由是 CEmailLogRoute ,它會(huì)將信息發(fā)送到指定的 email 地址,且只有級(jí)別為 error 或 warning 的才會(huì)發(fā)送。
在 Yii 中,有下列幾種日志路由可用:
CDbLogRoute: 將信息保存到數(shù)據(jù)庫(kù)的表中。
CEmailLogRoute: 發(fā)送信息到指定的 Email 地址。
CFileLogRoute: 保存信息到應(yīng)用程序 runtime 目錄中的一個(gè)文件中。
CWebLogRoute: 將 信息 顯示在當(dāng)前頁(yè)面的底部。
CProfileLogRoute: 在頁(yè)面的底部顯示概述(profiling)信息。
信息: 信息路由發(fā)生在當(dāng)前請(qǐng)求周期最后的 onEndRequest 事件觸發(fā)時(shí)。 要顯式終止當(dāng)前請(qǐng)求過(guò)程,請(qǐng)調(diào)用 CApplication::end() 而不是使用 die() 或 exit(),因?yàn)?CApplication::end() 將會(huì)觸發(fā)onEndRequest 事件, 這樣信息才會(huì)被順利地記錄。
3. 信息過(guò)濾
正如我們所提到的,信息可以在他們被發(fā)送到一個(gè)日志路由之前通過(guò)它們的級(jí)別和分類過(guò)濾。 這是通過(guò)設(shè)置對(duì)應(yīng)日志路由的 levels 和 categories 屬性完成的。 多個(gè)級(jí)別或分類應(yīng)使用逗號(hào)連接。
由于信息分類是類似 xxx.yyy.zzz 格式的,我們可以將其視為一個(gè)分類層級(jí)。 具體地,我們說(shuō) xxx 是 xxx.yyy的父級(jí),而xxx.yyy 又是 xxx.yyy.zzz 的父級(jí)。 這樣我們就可以使用 xxx.* 表示分類 xxx 及其所有的子級(jí)和孫級(jí)分類
4. 記錄上下文信息
從版本 1.0.6 起,我們可以設(shè)置記錄附加的上下文信息, 比如 PHP 的預(yù)定義變量(例如 $_GET, $_SERVER),session ID,用戶名等。 這是通過(guò)指定一個(gè)日志路由的 CLogRoute::filter屬性為一個(gè)合適的日志過(guò)濾規(guī)則實(shí)現(xiàn)的。
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.
框架可能在許多數(shù)情況下會(huì)用到日志過(guò)濾器CLogFilter來(lái)過(guò)濾日志。默認(rèn)情況下,CLogFilter日志消息包含了許多系統(tǒng)上下文信息的變量,像$ _GET,$_SERVER。 CLogFilter也可以配置的前綴與會(huì)話ID,用戶名等,我們?cè)跈z查無(wú)數(shù)記錄的消息每個(gè)記錄的消息時(shí),這可能會(huì)極大地簡(jiǎn)化了搜索難度
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.
下面的配置顯示了如何啟用日志記錄的上下文信息。請(qǐng)注意,每個(gè)日志路由可能有其自己的日志過(guò)濾器。 默認(rèn)情況下,日志路由不會(huì)有日志篩選器。
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 includingyii.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.
從版本1.0.7開(kāi)始,Yii的日志記錄可以采用堆棧的方式記錄消息,此功能默認(rèn)是關(guān)閉的,因?yàn)樗鼤?huì)降低性能。要使用此功能,只需在入口腳本(前includingyii.php)定義一個(gè)命名為YII_TRACE_LEVEL的常量即一個(gè)大于0的整數(shù)。 Yii將在堆棧信息中追加應(yīng)用程序要到的每一個(gè)文件名和行號(hào)??梢酝ㄟ^(guò)設(shè)置YII_TRACE_LEVEL來(lái)設(shè)定堆棧的層數(shù)。這種方式在開(kāi)發(fā)階段特別有用,因?yàn)樗梢詭椭覀兇_定觸發(fā)跟蹤消息的地方。
5. Performance Profiling 性能分析
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.
性能分析是一類特殊類型的消息記錄。性能分析可用于測(cè)量指定代碼塊所需的時(shí)間,并找出性能瓶頸是什么。
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:
要使用性能分析日志,我們需要確定哪些代碼塊需要分析。我們要在分析性能的代碼短的開(kāi)始和結(jié)尾添加如下方法:
Yii::beginProfile('blockID'); ...code block being profiled... Yii::endProfile('blockID');
where blockID is an ID that uniquely identifies the code block.
其中blockID是一個(gè)標(biāo)識(shí)代碼塊的唯一ID。
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.
為了顯示分析結(jié)果,我們需要為CLogRouter增加CProfileLogRoute路由。然后通過(guò)CProfileLogRoute可以把性能測(cè)試結(jié)果顯示在當(dāng)前頁(yè)面結(jié)束。
6. Profiling SQL Executions 分析SQL執(zhí)行
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 endProfilestatements 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.
在數(shù)據(jù)庫(kù)開(kāi)發(fā)中分析是特別有用的,因?yàn)镾QL執(zhí)行往往是應(yīng)用程序的主要性能瓶頸。盡管我們可以手動(dòng)在每個(gè)SQL執(zhí)行的適當(dāng)?shù)牡胤讲迦隻eginProfile和endProfile來(lái)衡量花費(fèi)的時(shí)間,但從1.0.6版本開(kāi)始,Yii提供了更系統(tǒng)的方法來(lái)解決這個(gè)問(wèn)題。
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 aforementionedCProfileLogRoute, 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.
再實(shí)際的應(yīng)用程序當(dāng)中通過(guò)設(shè)置CDbConnection::enableProfiling愛(ài)分析每一個(gè)正在執(zhí)行的SQL語(yǔ)句。使用 CProfileLogRoute,結(jié)果可以很容易地顯示。它可以顯示我們是在執(zhí)行什么SQL語(yǔ)句花費(fèi)多少時(shí)間。我們也可以調(diào)用CDbConnection:getStats()來(lái)分析檢索SQL語(yǔ)句的執(zhí)行總數(shù)和其總的執(zhí)行時(shí)間。
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
smarty內(nèi)置函數(shù)foreach用法實(shí)例
這篇文章主要介紹了smarty內(nèi)置函數(shù)foreach用法,實(shí)例分析了smarty內(nèi)置的foreach函數(shù)使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2015-01-01教你如何用php實(shí)現(xiàn)LOL數(shù)據(jù)遠(yuǎn)程獲取
LOL(英雄聯(lián)盟)最近非常的火爆,哥自然也在玩了,最近遇到個(gè)問(wèn)題,就是每次想看看自己的戰(zhàn)斗力啥的,還得先開(kāi)盒子等等,麻煩,最近有一個(gè)想法,打算把它實(shí)現(xiàn)出來(lái)。2014-06-06destoon實(shí)現(xiàn)調(diào)用自增數(shù)字從1開(kāi)始的方法
這篇文章主要介紹了destoon實(shí)現(xiàn)調(diào)用自增數(shù)字從1開(kāi)始的方法,很有實(shí)用價(jià)值的一個(gè)技巧,需要的朋友可以參考下2014-08-08php使用strtotime和date函數(shù)判斷日期是否有效代碼分享
php使用strtotime和date函數(shù)進(jìn)行檢驗(yàn)判斷日期是否有效代碼分享,大家參考使用吧2013-12-12Yii框架操作cookie與session的方法實(shí)例詳解
這篇文章主要介紹了Yii框架操作cookie與session的方法,結(jié)合實(shí)例形式詳細(xì)分析了Yii針對(duì)cookie與session操作的常用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2019-09-09Laravel框架學(xué)習(xí)筆記(二)項(xiàng)目實(shí)戰(zhàn)之模型(Models)
上一篇已經(jīng)介紹開(kāi)發(fā)環(huán)境的搭建,這篇將從項(xiàng)目實(shí)戰(zhàn)開(kāi)發(fā),一步一步了解laravel框架。首先我們來(lái)了解下laravel框架的模型 (Models)2014-10-10