PHP如何将log信息写入服务器中的log文件
将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://*.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(); ?>
然后去执行对应的代码:
页面是没有任何输出的:
然后的确生成了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://*.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"); ?>
效果是:
# 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
# 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
# cat logtest.log
[2015-07-27 12:05:47] (wx_access_token) this is crifanlib log test message using file_put_contents
#
如图:
注:
期间参考:
php: is_resource – manual
【总结】
1.此处可以通过:
fopen创建log文件
fwrite写入文件信息
fclose关闭文件
去实现log信息写入到文件中的。
2.更好的做法是:
直接用更方便的
file_put_contents直接输出内容到log文件
即可。
以上就是将log信息写入服务器中的log文件文件全部内容,希望大家喜欢。
推荐阅读
-
PHP如何将log信息写入服务器中的log文件
-
PHP error_log()将错误信息写入一个文件(定义和用法)_PHP
-
如何将集合中的信息通过输出流写入到指定文件中
-
PHP error_log()将错误信息写入一个文件(定义和用法)
-
php error_log() 将错误信息写入一个文件
-
PHP如何将log信息写入服务器中的log文件_PHP
-
PHP如何将log信息写入服务器中的log文件_php实例
-
PHP error_log()将错误信息写入一个文件(定义和用法)_php技巧
-
PHP error_log()将错误信息写入一个文件(定义和用法)_php技巧
-
如何将一个PHP数组有格式的写入文件中