PHP調試函數和日誌記錄函數分享,調試日誌記錄_PHP教程

來源:互聯網
上載者:User

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

http://www.bkjia.com/PHPjc/951232.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/951232.htmlTechArticlePHP調試函數和日誌記錄函數分享,調試日誌記錄 網站程式開發過程經常需要調試,發布階段也需要記錄作業記錄,方便發現問題和還原事件...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.