PHP debugging functions and logging functions sharing, debug logging _php Tutorial

Source: Internet
Author: User

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 ...

  • 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.