作者:天极
来源:天极
热度:
2006-11-16 17:42:18
3. 控制Log信息的函数
调试PHP程序的另外一种重要的方法就是查看Log信息。如果能够方便地控制Log信息的级别以及Log信息的显示内容,将会给程序调试带来更多的便利。下面的几个函数可以方便地实现这个功能。
| $ss_log_level = 0; $ss_log_filename = /tmp/ss-log; $ss_log_levels = array( NONE => 0, ERROR => 1, INFO => 2, DEBUG => 3); function ss_log_set_level ($level = ERROR) { global $ss_log_level; $ss_log_level = $level; } function ss_log ($level, $message) { global $ss_log_level, $ss-log-filename; if ($ss_log_levels[$ss_log_level] < $ss_log_levels[$level]) { // 不显示Log信息 return false; } $fd = fopen($ss_log_filename, "a+"); fputs($fd, $level. - [.ss_timestamp_pretty().] - .$message."n"); fclose($fd); return true; } function ss_log_reset () { global $ss_log_filename; @unlink($ss_log_filename); } |
| ss_log(ERROR, "testing level ERROR"); ss_log(INFO, "testing level INFO"); ss_log(DEBUG, "testing level DEBUG"); |
| function ss_timing_start ($name = default) { global $ss_timing_start_times; $ss_timing_start_times[$name] = explode( , microtime()); } function ss_timing_stop ($name = default) { global $ss_timing_stop_times; $ss_timing_stop_times[$name] = explode(, microtime()); } function ss_timing_current ($name = default) { global $ss_timing_start_times, $ss_timing_stop_times; if (!isset($ss_timing_start_times[$name])) { return 0; } if (!isset($ss_timing_stop_times[$name])) { $stop_time = explode(, microtime()); } else { $stop_time = $ss_timing_stop_times[$name]; } $current = $stop_time[1] - $ss_timing_start_times[$name][1]; $current += $stop_time[0] - $ss_timing_start_times[$name][0]; return $current; } |
我来说两句:
推荐文章
相关文章