PHP 日志记录功能实现
2014-02-12 10:21
232 查看
此文作者的日志是单一日志,加数学序号扩展。
我现在用的是日期日志,应用功能环境不一样,实现的东西有些不同。
对于现在的应用程序来说,日志的重要性是不言而喻的。很难想象没有任何日志记录功能的应用程序运行在生产环境中。日志所能提供的功能是多种多样的,包括记录程序运行时产生的错误信息、状态信息、调试信息和执行时间信息等。在生产环境中,日志是查找问题来源的重要依据。应用程序运行时的产生的各种信息,都应该通过日志类库来进行记录。
废话不多说了,附上日志类库的源代码:
Php代码
/**
* 日志处理类
*
* @since alpha 0.0.1
* @date 2014.03.04
* @author genialx
*
*/
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);
}
}
功能说明:
该日志类利用单例模式,节省资源。自行判断文件大小,超出指定大小则按序自行创建文件。如:文件log_0大于指定大小,则重新创建log_1文件(注意:创建文件是安装文件名后缀的数字的,请勿随意更改日志文件名)。
有待优化:
没有指定文件的最大个数,所以定期要手动删除过多的日志文件。
调用示例:
Php代码
//LOG
$L = Log::get_instance();
//第一个参数 int 0代表事件记录(THING LOG:),1代表错误记录(ERROR LOG:)
//第二个参数 string 描述文字
//第三个参数 string 时间
$L->log(1,'日志描述', date('Y-n-j H:m:s'));
$L->close();
感谢您的查阅!
文章来源:http://www.ihuxu.com/p/223.html
微信公众号(每日分享有价值的互联网资讯):胡旭个人博客
我现在用的是日期日志,应用功能环境不一样,实现的东西有些不同。
对于现在的应用程序来说,日志的重要性是不言而喻的。很难想象没有任何日志记录功能的应用程序运行在生产环境中。日志所能提供的功能是多种多样的,包括记录程序运行时产生的错误信息、状态信息、调试信息和执行时间信息等。在生产环境中,日志是查找问题来源的重要依据。应用程序运行时的产生的各种信息,都应该通过日志类库来进行记录。
废话不多说了,附上日志类库的源代码:
Php代码
/**
* 日志处理类
*
* @since alpha 0.0.1
* @date 2014.03.04
* @author genialx
*
*/
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);
}
}
功能说明:
该日志类利用单例模式,节省资源。自行判断文件大小,超出指定大小则按序自行创建文件。如:文件log_0大于指定大小,则重新创建log_1文件(注意:创建文件是安装文件名后缀的数字的,请勿随意更改日志文件名)。
有待优化:
没有指定文件的最大个数,所以定期要手动删除过多的日志文件。
调用示例:
Php代码
//LOG
$L = Log::get_instance();
//第一个参数 int 0代表事件记录(THING LOG:),1代表错误记录(ERROR LOG:)
//第二个参数 string 描述文字
//第三个参数 string 时间
$L->log(1,'日志描述', date('Y-n-j H:m:s'));
$L->close();
感谢您的查阅!
文章来源:http://www.ihuxu.com/p/223.html
微信公众号(每日分享有价值的互联网资讯):胡旭个人博客
相关文章推荐
- PHP添加Xdebug扩展的方法
- PHPer 的成长书录
- php5.3升级脚本
- yii 设置loginUrl
- PHP可能不知道的函数,介绍
- PHP 5.4 中经 htmlspecialchars 转义后的中文字符串为空的问题
- PHP添加Xdebug扩展的方法
- php-fpm配置详解
- php教程之魔术方法的使用示例(php魔术函数)
- php教程之phpize使用方法
- CI框架源码完全分析之核心文件(钩子)Hooks.php
- ThinkPHP 字段映射
- ThinkPHP 关于用create方法实现数据更新的问题
- 基于SAE的应用开发
- php理论学习(三)
- php理论学习(二)
- [转]PHP 真正多线程的使用
- PHP-----函数和二进制
- 如何解决zend studio不能提示代码?
- iis添加php支持