PHP debug functions and Logging functions sharing, debug logging
Web site program development process often need to debug, release phase also need to record run log, easy to find problems and restore events. This requires debugging and logging capabilities.
The following sections write the functions for debugging and the functions for logging errors.
The use method is simple and automatically generates log files based on dates:
Copy the Code code as follows:
When debugging, multiple parameters are available:
Sysdebug ("Hello");
Sysdebug ("Hello", "Tiger is coming Now");
The same is true for error logging:
Syserror ("error");
Syserror ("Error", "Unfortunately Tiger is dead", "We are sad");
PHP debugging and logging functions, as follows:
Copy CodeThe code is as follows:
/**
* Record Debug information
*/
function Sysdebug ($msg) {
if (Defined ("Debug_mode")) {
TODO detects debug switch, does not print when publishing
$params = Func_get_args ();
$traces = Debug_backtrace ();
$trace = Array_pop ($traces);
Sysrecord ($params, $trace, ' Debug ');
}
}
/**
* Log error messages
*/
function Syserror ($msg) {
$params = Func_get_args ();
$traces = Debug_backtrace ();
$trace = Array_pop ($traces);
Sysrecord ($params, $trace, ' error ');
}
/**
* Write files
* @ignore
*/
function Sysfile ($filename, $msg, $mode = null) {
$path = DirName ($filename);
if (!file_exists ($path)) {
mkdir ($path, 0666, true);
}
$flag = LOCK_EX;
if ($mode) {
Switch ($mode) {
Case "Add":
$flag = File_append | LOCK_EX;
Break
Case "a":
$flag = File_append | LOCK_EX;
Break
Default
Break
}
}
File_put_contents ($filename, $msg, $flag);
}
/**
* Record Information
* @ignore
*/
function Sysrecord ($params, $trace, $level) {
$path = DirName (__file__). "/logs/";
TODO Log Save directory It's best to modify it.
$file = $trace [' file '];
$func = $trace [' function '];
if ($func = = "Sys$level") {
$func = ";
}
$filename = $path. "$level/". Date ("y-m-d"). '. log ';
$msg = "[". Date ("M-d h:i:s"). "] file:\" ". BaseName ($file). "\" Func:\ "". $func. "\" MSG: ". Json_encode ($params). "\ r \ n";
Sysfile ($filename, $msg, ' Add ');
}
http://www.bkjia.com/PHPjc/951232.html www.bkjia.com true http://www.bkjia.com/PHPjc/951232.html techarticle PHP debugging functions and logging function sharing, debug logging Site program development process often need to debug, release phase also need to record the running log, easy to find problems and restore events ...