Web site program development process often need to debug, the release phase also needs to record the running log, easy to find problems and restore events. This requires debugging and logging capabilities.
The following functions are written for debugging and for logging errors.
Using the method is simple and automatically generates a log file based on the date:
Copy Code code as follows:
When debugging, you can have multiple parameters:
Sysdebug ("Hello");
Sysdebug ("Hello", "Tiger is coming Now");
Error logging is the same:
Syserror ("error");
Syserror ("Error", "Unfortunately Tiger is dead", "We are sad");
PHP debugging and logging functions, as follows:
Copy Code code as follows:
/**
* Recording Debugging information
*/
function Sysdebug ($msg) {
if (Defined ("Debug_mode")) {
TODO detects debug switches and does not print when published
$params = Func_get_args ();
$traces = Debug_backtrace ();
$trace = Array_pop ($traces);
Sysrecord ($params, $trace, ' Debug ');
}
}
/**
* Log error message
*/
function Syserror ($msg) {
$params = Func_get_args ();
$traces = Debug_backtrace ();
$trace = Array_pop ($traces);
Sysrecord ($params, $trace, ' error ');
}
/**
* Write file
* @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 Best modified
$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 ');
}