6 日志操作类Log.class.php

<?php
/*
 * 日志操作类
 * 默认需要在inc文件里配置常量LOG_PATH
 * define('LOG_PATH','/www');
 */
 
class Log{
 
	const FILENAME = "Log";
 
	static public function write($content, $logPath = "", $fileName = ""){
 
		if($fileName == ""){
 
			$fileName = self::FILENAME;
 
		}
 
		if($logPath != ""){
 
			$logPath = LOG_PATH . $logPath . "/";
 
		}else{
 
			$logPath = LOG_PATH;
 
		}
 
		$content = date("Y-m-d H:i:s") . " " . $content . "\n";
 
		$fileFullName = $logPath . $fileName . date('Ymd') . '.txt';
 
		if (!is_dir($logPath)) {
 
			mkdir($logPath, 0777);
 
		}
 
		if ( !file_exists ( $fileFullName ) ){
 
			touch ( $fileFullName );
 
			chmod ( $fileFullName, 0777 );
 
		}
 
		$fp = fopen($fileFullName, 'ab');
 
		if (fputs($fp, $content)){
 
			fclose($fp);
 
			return true;
 
		}else{
 
			return false;
 
		}
 
	}
 
}
 
?>

Related posts:

Leave a Reply

Your email address will not be published.