Phperror_log () writes error information to the log file.

Source: Internet
Author: User
Dedecms users may log the program execution error file and inject the SQL into a log file by themselves. next I will introduce an error_log () in php () write error information to the log file instance... dedecms users may log the program execution error file and inject the SQL into a log file by themselves. next I will introduce an error_log () in php () write error information to the log file instance. We hope this method will be helpful to you.

Error_log () is a function that sends error messages to a specific place. it is common in programming, especially in the program debugging stage.

This article describes how to use the error_log () function and some precautions. the code is as follows:

$ Str = 'this is an error message. ';

Error_log ($ str, 3, 'Errors. log ');

The above is the most common error_log () example. it is used to write a piece of information into errors. in the log file, this file is automatically created if it does not exist. In this example, we can see that there is a parameter "3". Note that this number "3" cannot be changed or removed.

The following lists the problems that may occur when using the error_log () function:

(1) program error prompt: Warning: error_log () [function. error-log]: failed to open stream: Permission denied in... on line...

The preceding error occurs because the file has no write permission. enable the file write permission for this directory.

(3) the information written into the log file cannot be wrapped in line breaks.

If you use error_log () to write a log file, no line breaks are found in the text. you can make the following improvements to the above code:

 

Note that $ str uses double quotation marks. The difference between php single quotation marks and double quotation marks is that rn is added at the end of the string, which is different from the first example, next, I will share two custom logging error log instances. the code is as follows:

  0) {switch ($ user_defined_err ['type']) {case 1: $ user_defined_errType = 'fatal runtime error (E_ERROR) '; break; case 2: $ user_defined_errType = 'non-fatal runtime error (E_WARNING) '; break; case 4: $ user_defined_errType = 'syntax parsing error (E_PARSE)'; break; case 8: $ user_defined_errType = 'runtime prompt (E_NOTICE) '; break; case 16: $ user_defined_errType = 'php internal error (E_CORE_ERROR)'; break; case 32: $ user_defined_errType = 'php internal warning (E_CORE_WARNING) '; break; case 64: $ user_defined_errType = 'internal error of Zend script engine (E_COMPILE_ERROR)'; break; case 128: $ user_defined_errType = 'internal warning of Zend script engine (E_COMPILE_WARNING) '; break; case 256: $ user_defined_errType = 'User-defined error (E_USER_ERROR)'; break; case 512: $ user_defined_errType = 'User-defined warning (E_USER_WARNING) '; break; case 1024: $ user_defined_errType = 'User-defined prompt (E_USER_NOTICE)'; break; case 2048: $ user_defined_errType = 'code prompt (E_STRICT) '; break; case 4096: $ user_defined_errType = 'fatal error that can be captured (E_RECOVERABLE_ERROR)'; break; case 8191: $ user_defined_errType = 'All error warnings (E_ALL) '; break; default: $ user_defined_errType = 'unknown type'; break ;} $ msg = sprintf ('% s % s', date ("Y-m-d H: I: s"), $ user_defined_errType, $ user_defined_err ['message'], $ user_defined_err ['file'], $ user_defined_err ['line']); // open source code phprm.com error_log ($ msg, 0 );}} register_shutdown_function ('exceptionhandler ');

Call method:

  

Example 2: The log record class code is as follows:

   SetLog ("test ". time (); * // use the default * $ logs = new Logs (); * $ logs-> setLog ("test ". time (); * // record information array * $ logs = new Logs (); * $ arr = array (* 'type' => 'info ', * 'info' => 'test', * 'time' => date ("Y-m-d H: I: s", time ())*); * $ logs-> setLog ($ arr ); **************************************** * ****************/class Logs {private $ _ filepath; // file path private $ _ filename; // log file name private $ _ filehandle; // file handle/*** function: initialize the record class * Input: file path, file name to be written * output: None */public function Logs ($ dir = null, $ filename = null) {// The default path is the current path $ this-> _ filepath = emptyempty ($ dir )? '': $ Dir; // The default value is the time +. log file $ this-> _ filename = emptyempty ($ filename )? Date ('Y-m-D', time ()). '. log': $ filename; // generated path string $ path = $ this-> _ createPath ($ this-> _ filepath, $ this-> _ filename ); // Determine whether the file exists if (! $ This-> _ isExist ($ path) {// does not exist // if there is no path, the default value is the current directory if (! Emptyempty ($ this-> _ filepath) {// create a directory if (! $ This-> _ createDir ($ this-> _ filepath) {// processing die ("directory creation failed! ") ;}} // Create a file if (! $ This-> _ createLogFile ($ path) {// processing die ("file creation failed! ") ;}} // Generate the path string $ path = $ this-> _ createPath ($ this-> _ filepath, $ this-> _ filename ); // open the file $ this-> _ filehandle = fopen ($ path, "a +");}/*** function: write record * input: output of the record to be written *: no */public function setLog ($ log) {// input array record $ str = ""; if (is_array ($ log )) {foreach ($ log as $ k =>$ v) {$ str. = $ k. ":". $ v. "n" ;}} else {$ str = $ log. "n";} // write the log if (! Fwrite ($ this-> _ filehandle, $ str) {// log writing failure die ("log writing Failure") ;}}/*** function: determine whether the file exists * input: file path, file name to be written * output: true | false */private function _ isExist ($ path) {return file_exists ($ path );} /*** function: create a directory (reference others' Super code-_-;) * input: directory to be created * output: true | false */private function _ createDir ($ dir) {return is_dir ($ dir) or ($ this-> _ createDir (dirname ($ dir )) and mkdir ($ dir, 0777);}/*** role: create a log file * input: directory to be created * output: true | false */private function _ createLogFile ($ path) {$ handle = fopen ($ path, "w"); // create a file fclose ($ handle ); return $ this-> _ isExist ($ path);}/*** function: build path * input: file path, file name to be written * output: constructed path string */private function _ createPath ($ dir, $ filename) {if (emptyempty ($ dir) {return $ filename;} else {return $ dir. "/". $ filename ;}/ *** function: destructor, release file handle * input: None * output: None */function _ destruct () {// close the file fclose ($ this-> _ filehandle );}}

Fixed:

1. you do not need to perform file IO operations every time you write a log, and open the file handle during object declaration.

2. logs of the array type are supported.

3, you can use the default path and default file, for the YYYY-MM-DD.log file under the current directory

Summary: I prefer the built-in functions. if the built-in logging functions cannot meet the requirements, we can use the following custom functions.

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.