PHP调试函数和日志记录函数分享,调试日志记录
网站程序开发过程经常需要调试,发布阶段也需要记录运行日志,方便发现问题和还原事件。这就要求有调试和日志记录功能。
下面分别写了用于调试的函数和用于记录错误的函数。
使用方法很简单,且自动根据日期生成日志文件:
复制代码 代码如下:
//调试时,多个参数都可以:
sysdebug("hello");
sysdebug("hello", "tiger is coming now");
//错误记录也一样:
syserror("error");
syserror("error", "unfortunately tiger is dead ", "we are sad");
php调试和日志记录函数,如下:
复制代码 代码如下:
/**
* 记录调试信息
*/
function sysdebug($msg) {
if (defined("DEBUG_MODE")) {
//TODO 检测调试开关,发布时不打印
$params = func_get_args();
$traces = debug_backtrace();
$trace = array_pop($traces);
sysrecord($params, $trace, 'debug');
}
}
/**
* 记录错误信息
*/
function syserror($msg) {
$params = func_get_args();
$traces = debug_backtrace();
$trace = array_pop($traces);
sysrecord($params, $trace, 'error');
}
/**
* 写文件
* @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);
}
/**
* 记录信息
* @ignore
*/
function sysrecord($params, $trace, $level) {
$path = dirname(__FILE__) . "/logs/";
//TODO 日志保存目录最好修改一下
$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');
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.