TEDIA会員に登録したメールアドレスとパスワードを入力してください

メールアドレス:

     パスワード:


パスワードを忘れた方はパスワードの確認を行ってください。

TEDIA会員へのご登録がお済みで無い方はこちらで登録ができます


>> テクノロジーポータル TEDIA トップページへ戻る <<

Think IT Software Developer's Think IT Find-IT 失敗しないソフト選び Find-IT TEDIA テクノロジーポータル TEDIA インストールマニアックス2008 インストールマニアックス2008

TEDIA SponsorsOpen Source Web Development Tutorials - Dev Shed

PHPでログ機能を作ろう
(2009/02/13公開)

トレースログ

 ログの設計構想を終えたところで、アプリケーション中にトレースログを実装する方法を考えよう。PHPにはネーティブレベルで必要な情報をログに記録するerror_log機能がある。ただし、これは極めて簡単なもので、先に考慮した根本的な設計上の機能のいくつかが欠落している。

 それで、ここではPEAR::Logを利用して、MyLogというクラスを書いてみる。自分自身でクラスを作ることによって、自らログメッセージの前後に容易に必要な文字列を追加できるという、柔軟性を手に入れることができる。

 MyLogクラスの構造は次のとおり:


*********************** MyLog.php***************************
<?php
/**
* MyLog -> This class is subclass of pear::log 
*/
 
require_once 'Log.php' ; // pear Log class
 
class MyLog 
{
 
var $logger = Null ; // Log class singleton instance 
 
function MyLog() // constructor
{
global $myGlobal ; 
$conf = array('mode' => 0600, 'timeFormat' => '%X %x');

$this->logger = &Log::singleton('file', $myGlobal['log']['logfile'], '', $conf, $myGlobal['log']['level']); 
 
} // end of function MyLog
 
/**
* @desc logs to the specified file
* @param
* below are the valid levels , always use nos for specifying log levels
 
0 System is unusable
1 Immediate action required
2 Critical conditions
3 Error conditions
4 Warning conditions
5 Normal but significant
6 Informational
7 Debug-level messages
 
*/
 
function writelog($logmsg, $priority=7, $file_name = "" , $line_no = "")
{
global $myGlobal ;
 
// Check the current file size 
 
// we have two log files -> trace.last.log ang trace.log 
// both of these should not be greater than specified size limit
// suppose specified size limit is 100 MB
// in this case if trace.log (current log file) increases more than 50 MB
// trace.last.log is deleted , trace.log is renamed to trace.log.last and 
// a new trace.log is created for logging
 
$lsize = filesize ( $myGlobal['log']['logfile'] ) ; // in bytes
$fsize = $lsize/2 ;
 
if ($lsize >= $myGlobal['log']['size'] *1024*1024/2){ // exceeded the limit
// delete if there's already some old file like .last
unlink($myGlobal['log']['logfile']."last") ; 
// rename the current log file to filenaame.last.log
rename($myGlobal['log']['logfile'], $myGlobal['log']['logfile'].".last") ; 
 

 
// prepend the log message with unix timestamp
$logmsg = mktime()." ".$logmsg ;
 
// append file name and line no 
$logmsg .= " ".$file_name ;
$logmsg .= " ".$line_no ;
 
//append user login
$logmsg .= " ".$_SESSION[current_login_id] ;
 
$this->logger->log($logmsg, $priority) ;
 
// mail logs if required
if ($priority <= $myGlobal['log']['email_level'] ){
 
// send email
$this->sendemail($logmsg, $priority) ; 
 
}
 
 
 
 
}
 
function sendemail($logmsg, $priority) // implementation for sending notification through mail
{
// implement yourself 
}
 
/**
* Destructor
*/
function _MyLog()
{
if ($this->_opened) {
$this->close();
}
}
}
 
?>



前のページ     1    2    3    4    5    次のページ

Copyright © 2008 Ziff Davis Enterprise, Inc.
Originally appearing in the U.S. Edition of Dev Shed. All Rights Reserved.