《PHP實(shí)戰(zhàn):php實(shí)現(xiàn)的debug log日志操作類(lèi)實(shí)例》要點(diǎn):
本文介紹了PHP實(shí)戰(zhàn):php實(shí)現(xiàn)的debug log日志操作類(lèi)實(shí)例,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
PHP實(shí)戰(zhàn)本文實(shí)例講述了php實(shí)現(xiàn)的debug log日志操作類(lèi).分享給大家供大家參考,具體如下:
PHP實(shí)戰(zhàn)
<?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); // 誰(shuí)調(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();
PHP實(shí)戰(zhàn)這里再補(bǔ)充一個(gè)函數(shù):
PHP實(shí)戰(zhàn)
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);
}
PHP實(shí)戰(zhàn)更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《PHP錯(cuò)誤與異常處理方法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
PHP實(shí)戰(zhàn)希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助.
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/5702.html