How PHP writes log information to a log file on the server _php instance

Source: Internet
Author: User
Tags at sign
Log information to the log file on the server file, toss a big circle finally found the solution, the specific content is as follows:

Toss:

"Record"how to write classes and how to use classes in PHP

During this period, a single, configurable, universal, log system needs to be sorted out.

Support for writing log information to the log file.

"Toss the process"

1. Search:

PHP Log to File

Reference:

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 code:

Download redips10.tar.gz

2. Period:

"Resolved" function in PHP preceded by the at sign @

3. Then use the code:

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 DEFA Ult 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/def  LogFile.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-&GT;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 dat  E.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-&GT;LOGFP); }}}?>

Then test the code:

<?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 ();? >


Then go to execute the corresponding code:

http://xxx/access_token/wx_access_token.php

The page does not have any output:

Then it does generate a log file:

root@chantyou:php# CD access_token/root@chantyou:access_token# lltotal 16-rwxrwxrwx 1 root root 9335 Jul 17:51 crifanLi b.php-rwxrwxrwx 1 root root 567 Jul 17:52 wx_access_token.phproot@chantyou:access_token# lltotal 20-rwxrwxrwx 1 root
  root  9335 Jul 17:51 crifanlib.php-rw-r--r--1 apache Apache 1 17:56 crifanlibtest.log-rwxrwxrwx  T  root  567 Jul 17:52 wx_access_token.phproot@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#

Note

Remember to add write permissions to the corresponding (here is the corresponding folder in the Linux server:

root@chantyou:php# lltotal 48drwxr-xr-x 2 root root 4096 Jul 17:55 access_token-rwxr-xr-x 1 root root 1091 Sep 25 2014 errorcode.php-rw-r--r--1 root root 2230 June 14:16 micromsgverify.php-rwxr-xr-x 1 root root 4288 Sep pkcs7encod Er.php-rwxr-xr-x 1 root root 452 Sep readme.txt-rwxr-xr-x 1 root root 724 Sep sha1.phpdrwxr-xr-x 2 root ro  ot 4096 Jul 12:34 wechat_encypt-rwxr-xr-x 1 root root 5327 Sep wxbizmsgcrypt.php-rwxrwxrwx 1 root root 2455 Jul 18:06 wx_didaosuzhou.php-rwxr-xr-x 1 root root 1346 Sep xmlparse.phproot@chantyou:php# chmod ugo+wx Access_tok en/root@chantyou:php# lltotal 48drwxrwxrwx 2 root root 4096 Jul 17:55 access_token-rwxr-xr-x 1 root root 1091 Sep 25 errorcode.php-rw-r--r--1 root root 2230 June 14:16 micromsgverify.php-rwxr-xr-x 1 root root 4288 Sep PKCS7 Encoder.php-rwxr-xr-x 1 root root 452 Sep readme.txt-rwxr-xr-x 1 root root 724 Sep sha1.phpdrwxr-xr-x 2 ro OT root 4096 Jul 20 12:34 wechat_encypt-rwxr-xr-x 1 root root 5327 Sep wxbizmsgcrypt.php-rwxrwxrwx 1 root root 2455 Jul 18:06 Wx_didaos Uzhou.php-rwxr-xr-x 1 root root 1346 Sep xmlparse.php

Otherwise the error will be:

Can ' t Open/xxx/access_token/crifanlibtest.log file!

4. But suddenly it comes to mind:

I've learned it before,

File_put_contents

Can be replaced by: Fopen,fwrite,fclose.

So again to optimize for:

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 D Efault 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 dat  E.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 ($scriptNam e) $logContent ". Php_eol, File_append); }}?>


The test files are:

<?php/* author:crifan Li version:2015-07-27 contact:http://www.crifan.com/about/me/function:test crifanLib log*/i Nclude_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");? >

The effect is:

root@chantyou:access_token# LL
Total 16
-rw-r--r--1 root root 9524 Jul 18:16 crifanlib.php
-rwxrwxrwx 1 root root 561 Jul 18:18 wx_access_token.php
root@chantyou:access_token# LL
Total 20
-rw-r--r--1 root root 9524 Jul 18:16 crifanlib.php
-rw-r--r--1 Apache Apache 18:19 logTest.log
-rwxrwxrwx 1 root root 561 Jul 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#

Note:

Period Reference:

Php:is_resource–manual

Summary

1. Here you can do this by:

fopen Creating a log file
Fwrite Writing file information
Fclose closing files
To implement the log information written to the file.

2. A better approach would be to:

Direct use of more convenient

File_put_contents Direct output content to log file
Can.

The above is to write log information to the log file on the server all the content, I hope you like it.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.