Php custom error log, php custom _ PHP Tutorial

Source: Internet
Author: User
Tags php error
Let's talk about php custom error logs and php custom logs. Talking about php custom error logs, talking about php custom errors that are often viewed in php at ordinary times, rarely having the opportunity to write logs by yourself, after reading Wang Jian's "best log practices", I thought I 'd like to write a brief introduction to php custom error logs and php custom logs.

I often read php error logs and rarely have the opportunity to write logs by myself. after reading Wang Jian's "best log practices", I think it is necessary to write clear and well-structured logs.

Before writing logs, let's ask ourselves: why do we sometimes need to record custom logs? Instead of using the system's default logging method?

I think there are two reasons:

1. The team needs a uniform log format for easy management

2. a large number of useless error logs occupy hard disk space and only meaningful logs need to be recorded.

So, practice it.

1. open your php. ini

2. open the log record and set

The code is as follows:


Log_errors = Off

Change

The code is as follows:


Log_errors = On

3. save php. ini and restart the web server.

4. add the following code at the beginning of your code

The code is as follows:


<? Php
// Error handling function
Function myErrorHandler ($ errno, $ errstr, $ errfile, $ errline)
{
$ Log_file = "./php _ % s_log _". date ("Ymd"). ". log"; // defines the log file storage directory and file name
$ Template = '';
Switch ($ errno ){
Case E_USER_ERROR:
$ Template. = "ERROR-level user ERROR. the ERROR code [$ errno] $ errstr must be fixed ";
$ Template. = "error location file $ errfile, row $ errline \ n ";
$ Log_file = sprintf ($ log_file, 'error ');
Exit (1); // exit the system
Break;
Case E_USER_WARNING:
$ Template. = "the user's WARNING-level error. we recommend that you fix the error number [$ errno] $ errstr ";
$ Template. = "error location file $ errfile, row $ errline \ n ";
$ Log_file = sprintf ($ log_file, 'warning ');
Break;
Case E_USER_NOTICE:
$ Template. = "the user's NOTICE-level error does not affect the system. do not fix the error number [$ errno] $ errstr ";
$ Template. = "error location file $ errfile, row $ errline \ n ";
$ Log_file = sprintf ($ log_file, 'notic ');
Break;
Default:
$ Template. = "unknown error type: error number [$ errno] $ errstr ";
$ Template. = "error location file $ errfile, row $ errline \ n ";
$ Log_file = sprintf ($ log_file, 'Unknown ');
Break;
}
File_put_contents ($ log_file, $ template, FILE_APPEND );
Return true;
}
$ Error_handler = set_error_handler ("myErrorHandler"); // enable custom error logs

5. try to write an error code after the code.

Echo 1/0;

Check whether a log file exists in the defined path? :)

Note: the following errors cannot be handled by user-defined functions: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and set_error_handler () most E_STRICT operations are generated in the file where the function is located.

However, when you enable the error log system (php. log_error = on in ini and specify the system log file (also php. error_log = path name) in ini, and after error_reporting is enabled, all the above errors will be recorded in your defined file as system error logs.

The above is all the content described in this article. I hope you will have a new understanding of php custom error logs.

Ghost often reads php error logs and rarely has the opportunity to write logs by himself. after reading Wang Jian's "best log practices", he thought that he would write a log...

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.