This article mainly introduces the reasons and methods for enabling logging, both of which are personal experiences and hope to help you. 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 ";
$ 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 ";
$ 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 ";
$ Log_file = sprintf ($ log_file, 'notic ');
Break;
Default:
$ Template. = "unknown error type: error number [$ errno] $ errstr ";
$ Template. = "error location file $ errfile, row $ errline ";
$ 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.