Simple discussion on PHP custom error log

Source: Internet
Author: User
Tags error code ini sprintf system log

Usually read the error log PHP, very few opportunities to write their own log, saw Wang Jian's "Best log Practice" that write a clear, structured log is still very necessary.

Before writing a journal, we ask ourselves: why do we sometimes have to record custom logs? Instead of the system default logging method?

I think there are two reasons:

1. The team needs a unified form of log to facilitate management

2. A large number of useless error logs occupy hard disk space, only need to record meaningful logs.

So, practice.

1. Open your php.ini.

2. Open the log record to

The code is as follows:

Log_errors = Off

Change into

The code is as follows:

Log_errors = On

3. Save PHP.ini Exit and restart the Web server

4. Add the following code at the front of your code

The code is as follows:

  

Error-handling functions

function MyErrorHandler ($errno, $errstr, $errfile, $errline)

{

$log _file = "./php_%s_log_". Date ("Ymd"). ". Log "//" to define the directory and file name

$template = ';

Switch ($errno) {

Case E_USER_ERROR:

$template. = "User error level errors, must fix the error number [$errno] $errstr";

$template. = "Error location file $errfile, paragraph $errline line n";

$log _file = sprintf ($log _file, ' error ');

Exit (1);//System exit

Break

Case e_user_warning:

$template. = "User warning level error, recommended fix error number [$errno] $errstr";

$template. = "Error location file $errfile, paragraph $errline line n";

$log _file = sprintf ($log _file, ' warning ');

Break

Case E_user_notice:

$template. = "User notice level error, does not affect the system, does not fix the error number [$errno] $errstr";

$template. = "Error location file $errfile, paragraph $errline line n";

$log _file = sprintf ($log _file, ' notice ');

Break

Default

$template. = "Unknown error type: error number [$errno] $errstr";

$template. = "Error location file $errfile, paragraph $errline line n";

$log _file = sprintf ($log _file, ' unknown ');

Break

}

File_put_contents ($log _file, $template, file_append);

return true;

}

$error _handler = Set_error_handler ("MyErrorHandler");//Open Custom error log

5. Try writing the next error code after just the code

Echo 1/0;

Look at the path you've defined. Is there a more log file?:)

Note: The following level of 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 in the call to set_ Most e_strict generated in the file where the Error_Handler () function resides.

However, when you turn on the error log system (Log_error = on in php.ini) and specify the system log file (which is also the error_log= pathname in php.ini), and the error_reporting is all open, The above errors are recorded in the file you defined as the system error log.

This is the entire content described in this article, I hope you can have a new understanding of PHP custom error log.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.