php利用单例模式实现日志处理类库
php  /  管理员 发布于 7年前   209
class Log{
//单例模式
private static $instance = NULL;
//文件句柄
private static $handle = NULL;
//日志开关
private $log_switch = NULL;
//日志相对目录
private $log_file_path = NULL;
//日志文件最大长度,超出长度重新建立文件
private $log_max_len = NULL;
//日志文件前缀,入 log_0
private $log_file_pre = 'log_';
/**
* 构造函数
*
* @since alpha 0.0.1
* @date 2014.02.04
* @author genialx
*/
protected function __construct(){//注意:以下是配置文件中的常量,请读者自行更改
$this->log_file_path = LOG_FILE_PATH;
$this->log_switch = LOG_SWITCH;
$this->log_max_len = LOG_MAX_LEN;
}
/**
* 单利模式
*
* @since alpha 0.0.1
* @date 2014.02.04
* @author genialx
*/
public static function get_instance(){
if(!self::$instance instanceof self){
self::$instance = new self;
}
return self::$instance;
}
/**
*
* 日志记录
*
* @param int $type 0 -> 记录(THING LOG) / 1 -> 错误(ERROR LOG)
* @param string $desc
* @param string $time
*
* @since alpha 0.0.1
* @date 2014.02.04
* @author genialx
*
*/
public function log($type,$desc,$time){
if($this->log_switch){
if(self::$handle == NULL){
$filename = $this->log_file_pre . $this->get_max_log_file_suf();
self::$handle = fopen($this->log_file_path . $filename, 'a');
}
switch($type){
case 0:
fwrite(self::$handle, 'THING LOG:' . ' ' . $desc . ' ' . $time . chr(13));
break;
case 1:
fwrite(self::$handle, 'ERROR LOG:' . ' ' . $desc . ' ' . $time . chr(13));
break;
default:
fwrite(self::$handle, 'THING LOG:' . ' ' . $desc . ' ' . $time . chr(13));
break;
}
}
}
/**
* 获取当前日志的最新文档的后缀
*
* @since alpha 0.0.1
* @date 2014.02.04
* @author genialx
*/
private function get_max_log_file_suf(){
$log_file_suf = null;
if(is_dir($this->log_file_path)){
if($dh = opendir($this->log_file_path)){
while(($file = readdir($dh)) != FALSE){
if($file != '.' && $file != '..'){
if(filetype( $this->log_file_path . $file) == 'file'){
$rs = split('_', $file);
if($log_file_suf < $rs[1]){
$log_file_suf = $rs[1];
}
}
}
}
if($log_file_suf == NULL){
$log_file_suf = 0;
}
//截断文件
if( file_exists($this->log_file_path . $this->log_file_pre . $log_file_suf) && filesize($this->log_file_path . $this->log_file_pre . $log_file_suf) >= $this->log_max_len){
$log_file_suf = intval($log_file_suf) + 1;
}
return $log_file_suf;
}
}
return 0;
}
/**
* 关闭文件句柄
*
* @since alpha 0.0.1
* @date 2014.02.04
* @author genialx
*/
public function close(){
fclose(self::$handle);
}
}
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号