php實現(xiàn)的debug log日志操作類實例
本文實例講述了php實現(xiàn)的debug log日志操作類。分享給大家供大家參考,具體如下:
<?php
class Tool {
public static function log($info) {
$time = date('m-d H:i:s');
$backtrace = debug_backtrace();
$backtrace_line = array_shift($backtrace); // 哪一行調(diào)用的log方法
$backtrace_call = array_shift($backtrace); // 誰調(diào)用的log方法
$file = substr($backtrace_line['file'], strlen($_SERVER['DOCUMENT_ROOT']));
$line = $backtrace_line['line'];
$class = isset($backtrace_call['class']) ? $backtrace_call['class'] : '';
$type = isset($backtrace_call['type']) ? $backtrace_call['type'] : '';
$func = $backtrace_call['function'];
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/debug.log', "$time $file:$line $class$type$func: $info\n", FILE_APPEND);
}
}
class Action {
public function a() {
$this->b();
}
public function b() {
$this->c();
}
public function c() {
Tool::log('sdfsdf');
}
}
$action = new Action();
$action->a();
這里再補(bǔ)充一個函數(shù):
function loginfo($format) {
$args = func_get_args();
array_shift($args);
$d = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1)[0];
$info = vsprintf($format, $args);
$data = sprintf("%s %s,%d: %s\n", date("Ymd His"), $d["file"], $d["line"], $info);
file_put_contents(__DIR__."/log.txt", $data, FILE_APPEND);
}
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP錯誤與異常處理方法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
源碼分析系列之json_encode()如何轉(zhuǎn)化一個對象
這篇文章主要介紹了源碼分析系列之json_encode()如何轉(zhuǎn)化一個對象,對json_encode()感興趣的同學(xué),可以參考下2021-04-04
PHP file_get_contents 函數(shù)超時的幾種解決方法
在使用file_get_contents函數(shù)的時候,經(jīng)常會出現(xiàn)超時的情況,在這里要通過查看一下錯誤提示,看看是哪種錯誤,比較常見的是讀取超時,這種情況大家可以通過一些方法來盡量的避免或者解決。2009-07-07
php中OR與|| AND與&&的區(qū)別總結(jié)
以下是對php中OR與|| AND與&&的區(qū)別進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過來參考下2013-10-10

