本文实例讲述了php实现的debug log日志操作类。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?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(); |
这里再补充一个函数:
1
2
3
4
5
6
7
8
|
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程序设计有所帮助。