PHP日志处理类
来源:互联网 发布:电话和网络共用配线架 编辑:程序博客网 时间:2024/06/05 20:30
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
/**
* 日志处理类
* @category Think
* @package Think
* @subpackage Core
* @author liu21st <liu21st@gmail.com>
*/
class Log {
// 日志级别 从上到下,由低到高
const EMERG = 'EMERG'; // 严重错误: 导致系统崩溃无法使用
const ALERT = 'ALERT'; // 警戒性错误: 必须被立即修改的错误
const CRIT = 'CRIT'; // 临界值错误: 超过临界值的错误,例如一天24小时,而输入的是25小时这样
const ERR = 'ERR'; // 一般错误: 一般性错误
const WARN = 'WARN'; // 警告性错误: 需要发出警告的错误
const NOTICE = 'NOTIC'; // 通知: 程序可以运行但是还不够完美的错误
const INFO = 'INFO'; // 信息: 程序输出信息
const DEBUG = 'DEBUG'; // 调试: 调试信息
const SQL = 'SQL'; // SQL:SQL语句 注意只在调试模式开启时有效
// 日志记录方式
const SYSTEM = 0;
const MAIL = 1;
const TCP = 2;
const FILE = 3;
// 日志信息
static $log = array();
// 日期格式
static $format = '[ c ]';
/**
* 记录日志 并且会过滤未经设置的级别
* @static
* @access public
* @param string $message 日志信息
* @param string $level 日志级别
* @param boolean $record 是否强制记录
* @return void
*/
static function record($message,$level=self::ERR,$record=false) {
if($record || false!== strpos(C('LOG_RECORD_LEVEL'),$level)) {
$now = date(self::$format);
self::$log[] = "{$now} {$level}: {$message}\r\n";
}
}
/**
* 日志保存
* @static
* @access public
* @param integer $type 日志记录方式
* @param string $destination 写入目标
* @param string $extra 额外参数
* @return void
*/
static function save($type=self::FILE,$destination='',$extra='') {
if(empty($destination))
$destination = LOG_PATH.date('y_m_d').".log";
if(self::FILE == $type) { // 文件方式记录日志信息
//检测日志文件大小,超过配置大小则备份日志文件重新生成
if(is_file($destination) && floor(C('LOG_FILE_SIZE')) <= filesize($destination) )
rename($destination,dirname($destination).'/'.time().'-'.basename($destination));
}
error_log(implode("",self::$log), $type,$destination ,$extra);
// 保存后清空日志缓存
self::$log = array();
//clearstatcache();
}
/**
* 日志直接写入
* @static
* @access public
* @param string $message 日志信息
* @param string $level 日志级别
* @param integer $type 日志记录方式
* @param string $destination 写入目标
* @param string $extra 额外参数
* @return void
*/
static function write($message,$level=self::ERR,$type=self::FILE,$destination='',$extra='') {
$now = date(self::$format);
if(empty($destination))
$destination = LOG_PATH.date('y_m_d').".log";
if(self::FILE == $type) { // 文件方式记录日志
//检测日志文件大小,超过配置大小则备份日志文件重新生成
if(is_file($destination) && floor(C('LOG_FILE_SIZE')) <= filesize($destination) )
rename($destination,dirname($destination).'/'.time().'-'.basename($destination));
}
error_log("{$now} {$level}: {$message}\r\n", $type,$destination,$extra );
//clearstatcache();
}
}
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
/**
* 日志处理类
* @category Think
* @package Think
* @subpackage Core
* @author liu21st <liu21st@gmail.com>
*/
class Log {
// 日志级别 从上到下,由低到高
const EMERG = 'EMERG'; // 严重错误: 导致系统崩溃无法使用
const ALERT = 'ALERT'; // 警戒性错误: 必须被立即修改的错误
const CRIT = 'CRIT'; // 临界值错误: 超过临界值的错误,例如一天24小时,而输入的是25小时这样
const ERR = 'ERR'; // 一般错误: 一般性错误
const WARN = 'WARN'; // 警告性错误: 需要发出警告的错误
const NOTICE = 'NOTIC'; // 通知: 程序可以运行但是还不够完美的错误
const INFO = 'INFO'; // 信息: 程序输出信息
const DEBUG = 'DEBUG'; // 调试: 调试信息
const SQL = 'SQL'; // SQL:SQL语句 注意只在调试模式开启时有效
// 日志记录方式
const SYSTEM = 0;
const MAIL = 1;
const TCP = 2;
const FILE = 3;
// 日志信息
static $log = array();
// 日期格式
static $format = '[ c ]';
/**
* 记录日志 并且会过滤未经设置的级别
* @static
* @access public
* @param string $message 日志信息
* @param string $level 日志级别
* @param boolean $record 是否强制记录
* @return void
*/
static function record($message,$level=self::ERR,$record=false) {
if($record || false!== strpos(C('LOG_RECORD_LEVEL'),$level)) {
$now = date(self::$format);
self::$log[] = "{$now} {$level}: {$message}\r\n";
}
}
/**
* 日志保存
* @static
* @access public
* @param integer $type 日志记录方式
* @param string $destination 写入目标
* @param string $extra 额外参数
* @return void
*/
static function save($type=self::FILE,$destination='',$extra='') {
if(empty($destination))
$destination = LOG_PATH.date('y_m_d').".log";
if(self::FILE == $type) { // 文件方式记录日志信息
//检测日志文件大小,超过配置大小则备份日志文件重新生成
if(is_file($destination) && floor(C('LOG_FILE_SIZE')) <= filesize($destination) )
rename($destination,dirname($destination).'/'.time().'-'.basename($destination));
}
error_log(implode("",self::$log), $type,$destination ,$extra);
// 保存后清空日志缓存
self::$log = array();
//clearstatcache();
}
/**
* 日志直接写入
* @static
* @access public
* @param string $message 日志信息
* @param string $level 日志级别
* @param integer $type 日志记录方式
* @param string $destination 写入目标
* @param string $extra 额外参数
* @return void
*/
static function write($message,$level=self::ERR,$type=self::FILE,$destination='',$extra='') {
$now = date(self::$format);
if(empty($destination))
$destination = LOG_PATH.date('y_m_d').".log";
if(self::FILE == $type) { // 文件方式记录日志
//检测日志文件大小,超过配置大小则备份日志文件重新生成
if(is_file($destination) && floor(C('LOG_FILE_SIZE')) <= filesize($destination) )
rename($destination,dirname($destination).'/'.time().'-'.basename($destination));
}
error_log("{$now} {$level}: {$message}\r\n", $type,$destination,$extra );
//clearstatcache();
}
}
0 0
- PHP日志处理类
- php日志处理
- php日志错误异常处理
- php单例模式实现日志处理类库
- PHP处理日志的一点思考
- PHP error_log()函数处理错误日志
- PHP中的错误处理和日志函数
- php处理大的日志文件
- 【转】PHP错误处理写入日志记录
- PHP中的错误处理和日志函数
- 日志处理类
- php 日志类
- php 日志类
- PHP-异常处理Exception,及错误日志处理
- php中自定义日志处理类(类似于java中的log4j)
- 日志处理类(Log)
- PHP错误处理和日志函数 及函数名
- PowerMTA执行php文件处理发送邮件的日志信息
- Spring MVC @Transactional注解方式事务失效的解决办法
- android之自定义带文本的圆角进度条
- CAS实现SSO单点登录
- 使用责任链模式过滤字符串
- java File操作实例
- PHP日志处理类
- 系统流程图,程序流程图和数据流图
- 史上“最骚”公式编辑器,你了解多少!
- 发现和记录--Nginx基本配置、性能优化指南
- 看《欢乐颂》眉不眉
- ElasticSearch——ELK
- android中的字体闪烁效果和跑马灯效果
- 无聊之语(2007.07.5)
- 报错"Receiver 'EntitlementEntity' type for instance message is a forward declaration"