php實現的debug log日志操作類實例
更新時間:2016年07月12日 09:33:25 作者:dotcoo
這篇文章主要介紹了php實現的debug log日志操作類,結合實例形式分析了php針對日志的相關操作技巧,包括php數組、字符串及文件的寫操作等用法,需要的朋友可以參考下
本文實例講述了php實現的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); // 哪一行調用的log方法 $backtrace_call = array_shift($backtrace); // 誰調用的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();
這里再補充一個函數:
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相關內容感興趣的讀者可查看本站專題:《PHP錯誤與異常處理方法總結》、《php字符串(string)用法總結》、《PHP數組(Array)操作技巧大全》、《PHP運算與運算符用法總結》、《PHP網絡編程技巧總結》、《PHP基本語法入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
PHP file_get_contents 函數超時的幾種解決方法
在使用file_get_contents函數的時候,經常會出現超時的情況,在這里要通過查看一下錯誤提示,看看是哪種錯誤,比較常見的是讀取超時,這種情況大家可以通過一些方法來盡量的避免或者解決。2009-07-07