<?
/**
*日誌類
*將日誌信息寫入文件
*@authe StruggleLinux
*@date 2009-11-25
* 例:
* $log = new Log_Class();
*
* $log->path = "log/test";
* $log->user = "system";
* $log->info = "添加成功!";
* $t=$log->makeDir($log->path); //創建目錄
* $te= $log->writeLog();
* if($te)
* {
* echo true;
* }else{
* echo FALSE;
* }
*
*
*/
class Log_Class
{
#自定義信息變量
public $info = "";
#自定義用戶變量
public $user = "";
#自定義路徑
public $path = "";
/*信息格式化
*@param $info string
*@param $user string 用戶信息 如:系統 sys 、用戶 admin
*return string $data
*/
function infoFormat()
{
$data = "TIME:[".date("Y-m-d H:i:s")."]#USER:[".$this->user."]#INFO:[".$this->info."]#PATH:[".$this->path."]\n";
return $data;
}
/*寫入文件
*@param string $path 目錄路徑
*@param string $data 寫入信息
*return boolean
*/
function writeLog()
{
$status = FALSE;
$filePath = $this->getFile(); #獲取文件路
if( file_exists($filePath))
{
$fileData = file_put_contents($filePath,$this->infoFormat(), FILE_APPEND );
if($fileData)
{
$status = true;
}
}else{
$fileExist = $this->creatFile(); //判斷是否以創建文件
if( !$fileExist )
{
$this->writeLog();
}
}
return $status;
}
/*創建文件
*@param string $path 文件路徑
*@param string $data 寫入信息
*return boolean
*/
function creatFile($data =" " )
{
$status = FALSE;
$fileName=date('Y-m-d H-i-s').".txt"; #定義文件名
$filePath = $this -> path."/".$fileName; #文件路徑
if( is_dir($this -> path) )
{
$dataSize = file_put_contents($filePath,$data, FILE_APPEND ); #創建寫入文件
if( $dataSize > 0)
{
$status = TRUE;
}
}
return $status;
}
/*獲取當前目錄近期的文件
*@param string $path 目錄路勁
*return string $filePath 完整路徑文件名
*/
function getFile()
{
$limitSize = 1048576; #定義限制文件的大小字節
$status = FALSE;
$fileArray = $this->getAllFiles( $this->path );
if( !empty($fileArray) )
{
$lastFile = max($fileArray); #獲取最近的文件
if( filesize($lastFile) < $limitSize)
{
$status = $lastFile;
}
}
return $status;
}
/*
*遍歷目錄文件
*@param string $path 目錄路勁
*return array $list 文件路經數組
*/
function getAllFiles($path)
{
$list = array(); #存儲目錄下的所有文件名
foreach( glob( $path.'/*') as $item )
{
if( is_dir( $item ) )
{
$list = array_merge( $list , $this->getAllFiles( $item ) );
}else{
$list[] = $item;
}
}
return $list;
}
/*創建目錄
*@param string $path
*@return Boolean
*/
function makeDir($path)
{
$dirs = array();
$status = FALSE;
$path = preg_replace('/(\/){2,}|{\\\}{1,}/','/',$this->path); #對路徑逕行檢查
if(!is_dir($path)) //判斷路徑是否存在
{
$dirs = explode("/",$path);
$this->path ="";
foreach($dirs as $folder)
{
$this->path.=$folder."/";
}
mkdir($this->path,0777,true); //可循環創建目錄
if( is_dir($path) )
{
$status =TRUE;
}
}else{
$status =TRUE;
}
return $status;
}
}
?>
/**
*日誌類
*將日誌信息寫入文件
*@authe StruggleLinux
*@date 2009-11-25
* 例:
* $log = new Log_Class();
*
* $log->path = "log/test";
* $log->user = "system";
* $log->info = "添加成功!";
* $t=$log->makeDir($log->path); //創建目錄
* $te= $log->writeLog();
* if($te)
* {
* echo true;
* }else{
* echo FALSE;
* }
*
*
*/
class Log_Class
{
#自定義信息變量
public $info = "";
#自定義用戶變量
public $user = "";
#自定義路徑
public $path = "";
/*信息格式化
*@param $info string
*@param $user string 用戶信息 如:系統 sys 、用戶 admin
*return string $data
*/
function infoFormat()
{
$data = "TIME:[".date("Y-m-d H:i:s")."]#USER:[".$this->user."]#INFO:[".$this->info."]#PATH:[".$this->path."]\n";
return $data;
}
/*寫入文件
*@param string $path 目錄路徑
*@param string $data 寫入信息
*return boolean
*/
function writeLog()
{
$status = FALSE;
$filePath = $this->getFile(); #獲取文件路
if( file_exists($filePath))
{
$fileData = file_put_contents($filePath,$this->infoFormat(), FILE_APPEND );
if($fileData)
{
$status = true;
}
}else{
$fileExist = $this->creatFile(); //判斷是否以創建文件
if( !$fileExist )
{
$this->writeLog();
}
}
return $status;
}
/*創建文件
*@param string $path 文件路徑
*@param string $data 寫入信息
*return boolean
*/
function creatFile($data =" " )
{
$status = FALSE;
$fileName=date('Y-m-d H-i-s').".txt"; #定義文件名
$filePath = $this -> path."/".$fileName; #文件路徑
if( is_dir($this -> path) )
{
$dataSize = file_put_contents($filePath,$data, FILE_APPEND ); #創建寫入文件
if( $dataSize > 0)
{
$status = TRUE;
}
}
return $status;
}
/*獲取當前目錄近期的文件
*@param string $path 目錄路勁
*return string $filePath 完整路徑文件名
*/
function getFile()
{
$limitSize = 1048576; #定義限制文件的大小字節
$status = FALSE;
$fileArray = $this->getAllFiles( $this->path );
if( !empty($fileArray) )
{
$lastFile = max($fileArray); #獲取最近的文件
if( filesize($lastFile) < $limitSize)
{
$status = $lastFile;
}
}
return $status;
}
/*
*遍歷目錄文件
*@param string $path 目錄路勁
*return array $list 文件路經數組
*/
function getAllFiles($path)
{
$list = array(); #存儲目錄下的所有文件名
foreach( glob( $path.'/*') as $item )
{
if( is_dir( $item ) )
{
$list = array_merge( $list , $this->getAllFiles( $item ) );
}else{
$list[] = $item;
}
}
return $list;
}
/*創建目錄
*@param string $path
*@return Boolean
*/
function makeDir($path)
{
$dirs = array();
$status = FALSE;
$path = preg_replace('/(\/){2,}|{\\\}{1,}/','/',$this->path); #對路徑逕行檢查
if(!is_dir($path)) //判斷路徑是否存在
{
$dirs = explode("/",$path);
$this->path ="";
foreach($dirs as $folder)
{
$this->path.=$folder."/";
}
mkdir($this->path,0777,true); //可循環創建目錄
if( is_dir($path) )
{
$status =TRUE;
}
}else{
$status =TRUE;
}
return $status;
}
}
?>
转载于:https://blog.51cto.com/strugglelinux/235114