How PHP writes log information to the log file _php instance in the server

Source: Internet
Author: User
Tags at sign

Write log information to the server log file file, toss a big circle finally found the solution, the specific contents are as follows:

Toss:

How to write classes and how to use classes in "Logging" PHP

A, configurable, generic, log system needs to be sorted out.

Supports writing log information to log file.

"Tossing 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. Duration:

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

3. Then use the code:

crifanlib.php <?php/* [Filename] crifanlib.php [Function] Crifan ' php lib, implement common functions [Author] Crif An Li [contacts] http://www.crifan.com/contact_me/[note] 1.online 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 {privat
 e $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/d
  EfLogFile.log ';
  }//Set default log file for Linux and other systems else {$defautLogFile = '/tmp/deflogfile.log '; $this->logfile = $inputLogFile?
  $inputLogFile: $defautLogFile; Open log file for writingFile pointer at the "end of" the File//(if the file does not exist, try to create it) $this-&GT;LOGFP = fopen ($this-&G
 T;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 the suppress e_warning if using the system TZ Settings//(Don t forget to set the INI setting da
  Te.timezone) $time = @date (' [y-m-d h:i:s] '); The 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 execute the corresponding code:

http://xxx/access_token/wx_access_token.php

There is no output for the page:

Then it did generate the log file:

root@chantyou:php# CD access_token/
root@chantyou:access_token# ll Total
-rwxrwxrwx 1 root 9335 June 17:51 crifanlib.php
-rwxrwxrwx 1 root root 567 June-17:52-wx_access_token.php-
LL Total
-rwxrwxrwx 1 root  root  9335 17:51 crifanlib.php
-rw-r--r--1 Apache Apache  77 June 17:56 CrifanLibTest.log
-rwxrwxrwx 1 root  root 567 June  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#

Attention

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

root@chantyou:php# ll Total drwxr-xr-x 2 root 4096 17:55 access_token-rwxr-xr-x 1 root root 1091 Sep 25 2 014 errorcode.php-rw-r--r--1 Root 2230 June 14:16 micromsgverify.php-rwxr-xr-x 1 root root 4288 Sep 2014 PKCS 7encoder.php-rwxr-xr-x 1 Root 452 Sep 2014 readme.txt-rwxr-xr-x 1 root root 724 Sep 2014 sha1.php drwxr-xr-x 2 root root 4096 12:34 wechat_encypt-rwxr-xr-x 1 root 5327 Sep 2014 wxbizmsgcrypt.php-rwxrwxrwx 1 root RO OT 2455 June 18:06 wx_didaosuzhou.php-rwxr-xr-x 1 root 1346 Sep 2014 xmlparse.php-root@chantyou:php# chmod ugo+ WX access_token/root@chantyou:php# ll Total drwxrwxrwx 2 root root 4096 June 17:55 Access_token-rwxr-xr-x 1 root Root 1091 Sep 2014 errorcode.php-rw-r--r--1 root 2230 June 14:16 micromsgverify.php-rwxr-xr-x 1 root root 428 8 Sep 2014 pkcs7encoder.php-rwxr-xr-x 1 root 452 Sep 2014 readme.txt-rwxr-xr-x 1 root root 724 Sep 2014 sh a1.php Drwxr-xr-x 2 Root Root 4096 June 12:34 wechat_encypt-rwxr-xr-x 1 root 5327 Sep 2014 wxbizmsgcrypt.php-rwxrwxrwx 1 root root 245
 5 June 18:06 wx_didaosuzhou.php-rwxr-xr-x 1 root root 1346 Sep 2014 xmlparse.php

Otherwise there will be an error:

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

4. But it suddenly occurred to me:

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 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/d
  EfLogFile.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 '), Pathi
  Nfo_filename); Define the suppress e_warning if using the system TZ Settings//(Don t forget to set the INI setting da
  Te.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" ($scriptNa Me) $logContent ".
 Php_eol, File_append); }}?>


The test file is:

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

The effect is:

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

Note:

Period Reference:

Php:is_resource–manual

Summary

1. Here you can adopt:

fopen Create log file
Fwrite Write file information
Fclose Close File
To implement log information written to the file.

2. A better approach would be to:

Directly with the more convenient

File_put_contents direct output to log file
Can.

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

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.