<?php
|
namespace Log4Me;
|
#´´½¨ÈÕÆÚ£º2012-4-13 ÏÂÎç4:10:48
|
#×÷Õߣºxcc
|
#ÅäÖÃÎļþ¶ÁÈ¡Æ÷
|
#date_default_timezone_set('PRC');
|
|
const LOG_LEVEL_DEBUG = 0;
|
const LOG_LEVEL_INFO = 1;
|
const LOG_LEVEL_WARN = 2;
|
const LOG_LEVEL_ERROR = 3;
|
const LOG_LEVEL_FATAL = 4;
|
|
|
const WRITE_MODE_PERDAY = 0; //°´ÌìдÈÕÖ¾
|
const WRITE_MODE_PERHOUR = 1; //°´Ð¡Ê±Ð´ÈÕÖ¾
|
|
const DEF_LOG_LEVEL = LOG_LEVEL_INFO; //ĬÈÏDebugµÈ¼¶
|
const DEF_WRITE_MODE = WRITE_MODE_PERDAY; //ĬÈϰ´ÌìдÈÕÖ¾
|
|
$LOG_LEVEL_STR_ARRAY = array('Debug', 'Info', 'Warn', 'Error', 'Fatal');
|
|
class Log4Me
|
{
|
private $m_sDir;
|
private $m_sFileName;
|
private $m_LOG_LEVEL;
|
private $m_WRITE_MODE;
|
private $m_sFullLogPath; //ÍêÕûÈÕ־·¾¶
|
private $m_sAffixStr;
|
function __construct($dir, $fileName, $logLev = DEF_LOG_LEVEL, $writeMode = DEF_WRITE_MODE)
|
{
|
$this->m_sDir = $dir;
|
$this->m_sFileName = $fileName;
|
$this->m_LOG_LEVEL = $logLev;
|
$this->m_WRITE_MODE = $writeMode;
|
$this->m_sFullLogPath = '';
|
}
|
|
private function _check_and_create_new_log_file()
|
{
|
$bCreate = false;
|
$curLogPath = '';
|
if ($this->m_WRITE_MODE === WRITE_MODE_PERDAY)
|
{
|
$curLogPath = sprintf("%s\\%s_%s.log", $this->m_sDir, $this->m_sFileName, date("Ymd"));
|
}
|
else {
|
//Èç¹ûΪ¿Õ£¬¾Í´´½¨ÈÕÖ¾
|
$curLogPath = sprintf("%s\\%s\\%s_%s.log", $this->m_sDir, date("Ymd"), $this->m_sFileName, date("H"));
|
}
|
|
//Èç¹ûΪ¿Õ£¬¾Í´´½¨ÈÕÖ¾
|
if ($this->m_sFullLogPath === '')
|
{
|
$bCreate = true;
|
}
|
else
|
{
|
//ÈÕÆÚ¸Ä±äÁË£¬ÐèÒª´´½¨ÐÂÈÕÖ¾
|
if ($curLogPath != $this->m_sFullLogPath)
|
{
|
$bCreate = true;
|
}
|
}
|
|
if ($bCreate)
|
{
|
//²»´æÔÚÎļþ¼Ð£¬´´½¨
|
file_exists($dir = dirname($curLogPath)) || mkdir($dir, 0777, true);
|
$this->m_sFullLogPath = $curLogPath;
|
}
|
}
|
|
private function _log($info, $level)
|
{
|
global $LOG_LEVEL_STR_ARRAY;
|
//µÈ¼¶µÍÓÚµ±Ç°ÉèÖõÄÈÕÖ¾µÈ¼¶£¬²»Ð´ÈÕÖ¾
|
if ($level < $this->m_LOG_LEVEL)
|
{
|
return;
|
}
|
|
//¼ì²éÊÇ·ñÐèÒªÖØÐ´´½¨ÈÕÖ¾Îļþ£¬Èç¹ûÐèÒª¾Í´´½¨
|
$this->_check_and_create_new_log_file();
|
//дÈÕÖ¾
|
if (empty($this->m_sAffixStr))
|
{
|
$info = sprintf("%s\t%s\t%s\r\n", date("Y-m-dTH:i:s"), $LOG_LEVEL_STR_ARRAY[$level], $info);
|
}
|
else
|
{
|
$info = sprintf("%s\t%s\t%s\t%s\r\n", date("Y-m-dTH:i:s"), $LOG_LEVEL_STR_ARRAY[$level],
|
$this->m_sAffixStr, $info);
|
}
|
file_put_contents($this->m_sFullLogPath, $info, FILE_APPEND);
|
}
|
|
//ÉèÖø½¼Ó×Ö·û´®
|
public function SetAffixStr($affix)
|
{
|
$this->m_sAffixStr = $affix;
|
}
|
//»ñÈ¡¸½¼Ó×Ö·û´®
|
public function GetAffixStr()
|
{
|
return $this->m_sAffixStr;
|
}
|
|
public function Debug($info)
|
{
|
$this->_log($info, LOG_LEVEL_DEBUG);
|
}
|
|
public function Info($info)
|
{
|
$this->_log($info, LOG_LEVEL_INFO);
|
}
|
|
public function Warn($info)
|
{
|
$this->_log($info, LOG_LEVEL_WARN);
|
}
|
|
public function Error($info)
|
{
|
$this->_log($info, LOG_LEVEL_ERROR);
|
}
|
|
public function Fatal($info)
|
{
|
$this->_log($info, LOG_LEVEL_FATAL);
|
}
|
}
|
|
?>
|