PHP如何将log信息写入服务器中的log文件


Posted in PHP onJuly 29, 2015

将log信息写入服务器中的log文件文件,折腾了一大圈终于找到了解决方案,具体内容如下:

折腾:

【记录】php中如何写类和如何使用类

期间,需要整理出一份,可配置的,通用的,log系统。

支持写入log信息到log文件中。

【折腾过程】

1.搜:

php log to file

参考:

PHP: error_log ? Manual

PHP: syslog ? Manual

How to create logs with PHP ? Web Services Wiki

Write to a log file with PHP | Redips spideR Net

下载代码:

Download redips10.tar.gz

2.期间:

【已解决】PHP中函数前面加上at符号@的作用

3.然后用代码:

crifanLib.php

<?php
/*
[Filename]
crifanLib.php
[Function]
crifan's php lib, implement common functions
[Author]
Crifan Li
[Contact]
http://www.crifan.com/contact_me/
[Note]
1.online see code:
http://code.google.com/p/crifanlib/source/browse/trunk/php/crifanLib.php
[TODO]
[History]
[v1.0]
1.initial version, need clean up later
*/
class crifanLib {
 private $logFile;
 private $logFp;
 /*
  Init log file
 */
 function logInit($inputLogFile = null){
  // set default log file name
  // in case of Windows set default log file
  //http://stackoverflow.com/questions/1482260/how-to-get-the-os-on-which-php-is-running
  //http://php.net/manual/zh/function.php-uname.php
  if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
   $defautLogFile = 'C:/php/defLogFile.log';
  }
  // set default log file for Linux and other systems
  else {
   $defautLogFile = '/tmp/defLogFile.log';
  }
  $this->logFile = $inputLogFile ? $inputLogFile : $defautLogFile;
  // open log file for writing only and place file pointer at the end of the file
  // (if the file does not exist, try to create it)
  $this->logFp = fopen($this->logFile, 'a') or exit("Can't open $this->logFile!");
 }
 /*
  Write log info to file
 */
 function logWrite($logContent){
  // if file pointer doesn't exist, then open log file
  if (!is_resource($this->logFp)) {
   $this->logInit();
  }
  // define script name
  $script_name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
  // define current time and suppress E_WARNING if using the system TZ settings
  // (don't forget to set the INI setting date.timezone)
  $time = @date('[Y-m-d H:i:s] ');
  // write current time, script name and message to the log file
  fwrite($this->logFp, "$time ($script_name) $logContent" . PHP_EOL);
 }
 /*
  Deinit log
 */
 function logDeinit(){
  if (is_resource($this->logFp)) {
   fclose($this->logFp);
  }
 }
}
 ?>

然后测试代码:

<?php
/*
 Author: Crifan Li
 Version: 2015-07-27
 Contact: http://www.crifan.com/about/me/
 Function:  Wechat get access token
*/
include_once "crifanLib.php";
//test log
$crifanLib = new crifanLib();
$crifanLib->logInit("/xxx/access_token/crifanLibTest.log");
$crifanLib->logWrite("This is crifanLib log test message.");
$crifanLib->logDeinit();
?>

然后去执行对应的代码:

http://xxx/access_token/wx_access_token.php

页面是没有任何输出的:

PHP如何将log信息写入服务器中的log文件

然后的确生成了log文件了:

root@chantyou:php# cd access_token/
root@chantyou:access_token# ll
total 16
-rwxrwxrwx 1 root root 9335 Jul 27 17:51 crifanLib.php
-rwxrwxrwx 1 root root 567 Jul 27 17:52 wx_access_token.php
root@chantyou:access_token# ll
total 20
-rwxrwxrwx 1 root  root  9335 Jul 27 17:51 crifanLib.php
-rw-r--r-- 1 apache apache  77 Jul 27 17:56 crifanLibTest.log
-rwxrwxrwx 1 root  root  567 Jul 27 17:52 wx_access_token.php
root@chantyou:access_token# cat crifanLibTest.log 
[2015-07-27 10:10:33] (wx_access_token) This is crifanLib log test message.
root@chantyou:access_token#

【注意】

要记得给对应的(此处是Linux服务器中的对应的文件夹添加写权限:

root@chantyou:php# ll
total 48
drwxr-xr-x 2 root root 4096 Jul 27 17:55 access_token
-rwxr-xr-x 1 root root 1091 Sep 25 2014 errorCode.php
-rw-r--r-- 1 root root 2230 Jun 10 14:16 MicromsgVerify.php
-rwxr-xr-x 1 root root 4288 Sep 25 2014 pkcs7Encoder.php
-rwxr-xr-x 1 root root 452 Sep 15 2014 Readme.txt
-rwxr-xr-x 1 root root 724 Sep 22 2014 sha1.php
drwxr-xr-x 2 root root 4096 Jul 20 12:34 wechat_encypt
-rwxr-xr-x 1 root root 5327 Sep 15 2014 WXBizMsgCrypt.php
-rwxrwxrwx 1 root root 2455 Jul 16 18:06 wx_didaosuzhou.php
-rwxr-xr-x 1 root root 1346 Sep 22 2014 xmlparse.php
root@chantyou:php# chmod ugo+wx access_token/  
root@chantyou:php# ll
total 48
drwxrwxrwx 2 root root 4096 Jul 27 17:55 access_token
-rwxr-xr-x 1 root root 1091 Sep 25 2014 errorCode.php
-rw-r--r-- 1 root root 2230 Jun 10 14:16 MicromsgVerify.php
-rwxr-xr-x 1 root root 4288 Sep 25 2014 pkcs7Encoder.php
-rwxr-xr-x 1 root root 452 Sep 15 2014 Readme.txt
-rwxr-xr-x 1 root root 724 Sep 22 2014 sha1.php
drwxr-xr-x 2 root root 4096 Jul 20 12:34 wechat_encypt
-rwxr-xr-x 1 root root 5327 Sep 15 2014 WXBizMsgCrypt.php
-rwxrwxrwx 1 root root 2455 Jul 16 18:06 wx_didaosuzhou.php
-rwxr-xr-x 1 root root 1346 Sep 22 2014 xmlparse.php

否则会报错的:

Can't open /xxx/access_token/crifanLibTest.log file!

4.不过突然想起来:

之前已经学过了,

file_put_contents

就可以替代了:fopen,fwrite,fclose了。

所以再去优化为:

crifanLib.php

<?php
/*
[Filename]
crifanLib.php
[Function]
crifan's php lib, implement common functions
[Author]
Crifan Li
[Contact]
http://www.crifan.com/contact_me/
[Note]
1.online see code:
http://code.google.com/p/crifanlib/source/browse/trunk/php/crifanLib.php
[TODO]
[History]
[v2015-07-27]
1.add logInit, logWrite
[v1.0]
1.initial version, need clean up later
*/
class crifanLib {
 private $logFile;
 private $logFp;
 /*
  Init log file
 */
 function logInit($inputLogFile = null){
  // set default log file name
  // in case of Windows set default log file
  //http://stackoverflow.com/questions/1482260/how-to-get-the-os-on-which-php-is-running
  //http://php.net/manual/zh/function.php-uname.php
  if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
   $defautLogFile = 'C:/php/defLogFile.log';
  }
  // set default log file for Linux and other systems
  else {
   $defautLogFile = '/tmp/defLogFile.log';
  }
  $this->logFile = $inputLogFile ? $inputLogFile : $defautLogFile;
 }
 /*
  Write log info to file
 */
 function logWrite($logContent){
  // define script name
  $scriptName = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
  // define current time and suppress E_WARNING if using the system TZ settings
  // (don't forget to set the INI setting date.timezone)
  $timeStr = @date('[Y-m-d H:i:s]');
  // write current time, script name and message to the log file
  file_put_contents($this->logFile, "$timeStr ($scriptName) $logContent" . PHP_EOL, FILE_APPEND);
 }
}
 ?>

测试文件为:

<?php
/*
 Author: Crifan Li
 Version: 2015-07-27
 Contact: http://www.crifan.com/about/me/
 Function: test crifanLib log
*/
include_once "crifanLib.php";
//test log
$crifanLib = new crifanLib();
$crifanLib->logInit("/xxx/logTest.log");
$crifanLib->logWrite("This is crifanLib log test message using file_put_contents");
?>

效果是:

root@chantyou:access_token# ll
total 16
-rw-r--r-- 1 root root 9524 Jul 27 18:16 crifanLib.php
-rwxrwxrwx 1 root root  561 Jul 27 18:18 wx_access_token.php
root@chantyou:access_token# ll
total 20
-rw-r--r-- 1 root   root   9524 Jul 27 18:16 crifanLib.php
-rw-r--r-- 1 apache apache   76 Jul 27 18:19 logTest.log
-rwxrwxrwx 1 root   root    561 Jul 27 18:18 wx_access_token.php
root@chantyou:access_token# cat logTest.log
[2015-07-27 12:05:47] (wx_access_token) This is crifanLib log test message using file_put_contents
root@chantyou:access_token#
如图:

PHP如何将log信息写入服务器中的log文件

注:

期间参考:

PHP: is_resource ? Manual

【总结】

1.此处可以通过:

fopen创建log文件
fwrite写入文件信息
fclose关闭文件
去实现log信息写入到文件中的。

2.更好的做法是:

直接用更方便的

file_put_contents直接输出内容到log文件
即可。

以上就是将log信息写入服务器中的log文件文件全部内容,希望大家喜欢。

PHP 相关文章推荐
php HandlerSocket的使用
May 02 PHP
PHP的autoload机制的实现解析
Sep 15 PHP
实现PHP多线程异步请求的3种方法
Jan 17 PHP
自己写的php curl库实现整站克隆功能
Feb 12 PHP
PHP添加图片水印、压缩、剪切的封装类
Aug 17 PHP
PHP人民币金额转大写实例代码
Oct 02 PHP
详解WordPress中分类函数wp_list_categories的使用
Jan 04 PHP
php处理json格式数据经典案例总结
May 19 PHP
php实现带读写分离功能的MySQL类完整实例
Jul 28 PHP
PHP 与 UTF-8 的最佳实践详细介绍
Jan 04 PHP
php实现往pdf中加数字签名操作示例【附源码下载】
Aug 07 PHP
php变量与字符串的增删改查操作示例
May 07 PHP
再Docker中架设完整的WordPress站点全攻略
Jul 29 #PHP
php去掉文件前几行的方法
Jul 29 #PHP
PHP实现的简单网络硬盘
Jul 29 #PHP
PHP简单生成缩略图相册的方法
Jul 29 #PHP
PHP之预定义接口详解
Jul 29 #PHP
PHP实现的迷你漂流瓶
Jul 29 #PHP
PHP之浮点数计算比较以及取整数不准确的解决办法
Jul 29 #PHP
You might like
关于拼配咖啡,你要知道
2021/03/03 咖啡文化
解析PHP将对象转换成数组的方法(兼容多维数组类型)
2013/06/21 PHP
php读取csv文件后,uft8 bom导致在页面上显示出现问题的解决方法
2013/08/10 PHP
CMS中PHP判断系统是否已经安装的方法示例
2014/07/26 PHP
复制本贴标题和地址的js代码
2008/07/01 Javascript
jQuery 使用手册(五)
2009/09/23 Javascript
Js 随机数产生6位数字
2010/05/13 Javascript
一个简单的JavaScript数据缓存系统实现代码
2010/10/24 Javascript
js获取图片大小的函数代码
2011/09/20 Javascript
JS获取各种浏览器窗口大小的方法
2014/01/14 Javascript
IE与FF下javascript获取网页及窗口大小的区别详解
2014/01/14 Javascript
javascript转换日期字符串为Date日期对象的方法
2015/02/13 Javascript
JS实现带圆弧背景渐变效果的导航菜单代码
2015/10/13 Javascript
JavaScript实现页面跳转的方式汇总
2016/05/16 Javascript
有关suggest快速删除后仍然出现下拉列表的bug问题
2016/12/02 Javascript
JS控件bootstrap datepicker使用方法详解
2017/03/25 Javascript
详谈commonjs模块与es6模块的区别
2017/10/18 Javascript
webpack 如何解析代码模块路径的实现
2019/09/04 Javascript
js操作两个json数组合并、去重,以及删除某一项元素
2020/09/22 Javascript
Python实现的彩票机选器实例
2015/06/17 Python
在Django的模型中添加自定义方法的示例
2015/07/21 Python
python实现红包裂变算法
2016/02/16 Python
Python中循环引用(import)失败的解决方法
2018/04/22 Python
python Pandas库基础分析之时间序列的处理详解
2019/07/13 Python
深入了解Python在HDA中的应用
2019/09/05 Python
综合办公室个人的自我评价
2013/12/22 职场文书
初中校园之声广播稿
2014/01/15 职场文书
艺术设计专业个人求职信
2014/04/10 职场文书
纠纷协议书
2014/04/16 职场文书
公共场所禁烟标语
2014/06/25 职场文书
房屋买卖委托书格式范本格式
2014/10/13 职场文书
夫妻忠诚协议范文
2014/11/16 职场文书
2014年车间主任工作总结
2014/12/10 职场文书
退税申请报告怎么写
2015/05/18 职场文书
英语专业毕业论文答辩开场白
2015/05/27 职场文书
MySQL 使用事件(Events)完成计划任务
2021/05/24 MySQL