首页 技术 正文
技术 2022年11月11日
0 收藏 800 点赞 4,611 浏览 2151 个字

php文件日志类,按年月日组织目录结构。

<?phpclass FileLog
{
private $_filepath; //文件路径
private $_filename; //日志文件名
private $_filehandle; //文件句柄 function __construct($fileName = 'log')
{
date_default_timezone_set('PRC');
$this->_filename = $fileName;
$this->init();
} /*
* 构造函数调用,初始化文件保存路径,文件名
*/
function init()
{
//如果有定义LOG_PATH常量,日志记录在LOG_PATH下,如果没有定义记录在当前目录 的logs
$this->_filepath = defined('LOG_PATH') ? LOG_PATH : './logs';
$this->_filepath = rtrim($this->_filepath, '/');
$this->_filepath .= '/' . date('y', time()) . '/' . date('m', time());
if (!is_dir($this->_filepath)) {
mkdir($this->_filepath, 0777, true);
}
if (!is_dir($this->_filepath)) {
//如果目录创建失败直接返回
$this->_filehandle = null;
return;
}
//拼接完整的文件名
$pathinfo = pathinfo($this->_filename);
$extension = isset($pathinfo['extension']) ? $pathinfo['extension'] : '';//取文件的扩展名
$this->_filename = $this->_filepath . '/' . $pathinfo['filename'] . '_' . date('d', time());
$this->_filename .= empty($extension) ? '.log' : '.' . $extension;
//打开文件
$this->_filehandle = fopen($this->_filename, "a+");
} /**
*作用:初始化记录类,写入记录
*输入:要写入的记录,可以是数组
*输出:写入成功返回true失败返回false
*/
public function addLog($log)
{
if (empty($this->_filehandle))
return false;
$strLog = '';
$strLog .= date("Y-m-d H:i:s") . ' ' . $this->_getUrl() . "\r\n";
$strLog .= "POST: " . $this->_postData() . "\r\n";
if (is_array($log)) {
$strLog .= $this->array2string($log);
} else {
$strLog .= $log . "\r\n";
}
$strLog .= "\r\n"; //写日志
fwrite($this->_filehandle, $strLog) !== false;
} function array2string($data)
{
$log_a = "";
foreach ($data as $key => $value) {
if (is_array($value)) $log_a .= "[" . $key . "] => (" . $this->array2string($value) . ") \r\n";
else $log_a .= "[" . $key . "] => " . $value . "\r\n";
}
return $log_a;
} /**
*作用:获取完整URL路径
*输入:完整URL路径
*输出:URL路径字串
*/
private function _getUrl()
{
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost');
return 'http' . (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 's' : '')
. '://'
. $host
. $_SERVER['REQUEST_URI'];
} /**
*作用:获取POST数据
*输入:POST数据
*输出:POST数组
*/
private function _postData()
{
$strPost = '';
if (isset($_POST) && count($_POST) > 0) {
foreach ($_POST as $key => $val) {
$strPost .= $key . '=' . $val . '&';
}
}
$strPost=trim($strPost,'&');
return $strPost;
} /**
*功能: 析构函数,释放文件句柄
*输入: 无
*输出: 无
*/
function __destruct()
{
//关闭文件
if (!empty($this->_filehandle))
fclose($this->_filehandle);
}
}?>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,816
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,899