📄 正在查看:twcms/kongphp/base/log.class.php
大小:2,013 字节 · 修改:2014-04-07 07:15:32 · 行数:75
1<?php
2/**
3 * Copyright (C) 2013-2014 www.kongphp.com All rights reserved.
4 * Licensed http://www.gnu.org/licenses/lgpl.html
5 * Author: wuzhaohuan <kongphp@gmail.com>
6 */
7
8class log {
9 /**
10 * 写入日志
11 * @param string $s 写入字符串
12 * @param string $file 保存文件名
13 * @return boot
14 */
15 public static function write($s, $file = 'php_error.php') {
16 $time = date('Y-m-d H:i:s', $_ENV['_time']);
17 $ip = $_ENV['_ip'];
18 $url = self::to_str($_SERVER['REQUEST_URI']);
19 $s = self::to_str($s);
20 self::write_log('<?php exit;?>'." $time $ip $url $s \r\n", $file);
21 return TRUE;
22 }
23
24 /**
25 * 清理空白字符
26 * @param string $s 字符串
27 * @return string
28 */
29 public static function to_str($s) {
30 return str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $s);
31 }
32
33 /**
34 * 文件末尾写入日志
35 * @param string $s 写入字符串
36 * @param string $file 保存文件名
37 * @return boot
38 */
39 public static function write_log($s, $file) {
40 $logfile = LOG_PATH.$file;
41 try{
42 $fp = fopen($logfile, 'ab+');
43 if(!$fp) {
44 throw new Exception("写入日志失败,可能文件 $file 不可写或磁盘已满。");
45 }
46 fwrite($fp, $s);
47 fclose($fp);
48 }catch(Exception $e) {}
49 return TRUE;
50 }
51
52 /**
53 * 跟踪调试
54 * @param string $s 描述
55 */
56 public static function trace($s) {
57 if(!DEBUG) return;
58 empty($_ENV['_trace']) && $_ENV['_trace'] = '';
59 $_ENV['_trace'] .= $s.' - '.number_format(microtime(1) - $_ENV['_start_time'], 4)."\r\n";
60 }
61
62 /**
63 * 保存 trace
64 * @param string $file 保存文件名
65 */
66 public static function trace_save($file = 'php_trace.php') {
67 if(empty($_ENV['_trace'])) return;
68 $s = "<?php exit;?>\r\n========================================================================\r\n";
69 $s .= $_SERVER['REQUEST_URI']."\r\nPOST:".print_r($_POST, 1)."\r\nSQL:".print_r($_ENV['_sqls'], 1)."\r\n";
70 $s .= $_ENV['_trace']."\r\n\r\n";
71 self::write_log($s, $file);
72 }
73}
74?>
75