Home > Backend Development > PHP Tutorial > PHP debugging function and logging function sharing, debugging logging_PHP tutorial

PHP debugging function and logging function sharing, debugging logging_PHP tutorial

WBOY
Release: 2016-07-13 10:08:22
Original
1477 people have browsed it

Sharing of PHP debugging functions and logging functions, debugging logging

The development process of website programs often requires debugging, and operation logs also need to be recorded during the release phase to facilitate the discovery of problems and recovery of events. This requires debugging and logging capabilities.

The functions used for debugging and the functions used for recording errors are written below.

The method of use is very simple, and the log file is automatically generated based on the date:

Copy code The code is as follows:

//When debugging, multiple parameters are available:
sysdebug("hello");
sysdebug("hello", "tiger is coming now");

//The same is true for error records:
syserror("error");
syserror("error", "unfortunately tiger is dead ", "we are sad");

php debugging and logging functions, as follows:

Copy code The code is as follows:

/**
* Record debugging information
​*/
function sysdebug($msg) {
if (defined("DEBUG_MODE")) {
//TODO detects debugging switch and does not print when publishing
$params = func_get_args();
$traces = debug_backtrace();
$trace = array_pop($traces);
sysrecord($params, $trace, 'debug');
}
}

/**
* Record 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 saving directory is 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) . "rn";
sysfile($filename, $msg, 'add');
}

http://www.bkjia.com/PHPjc/951232.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/951232.htmlTechArticleSharing of PHP debugging functions and logging functions. Debugging is often required during the development process of debugging logging website programs, and is also required during the release phase. Record running logs to facilitate problem discovery and recovery events...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template