PHP debugging functions and Logging functions share _php Tips

Source: Internet
Author: User

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 ');
}

Related Article

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.