一个简单的logger类

就是简单封装一下error_log,主要为自己调试程序方便。后面顶多做一个是否debug的选项判断,以确保生产环境中不输出太多废话。现在急用,先顶着吧。

很简单,就是个singleton的类,没啥可解释的。只是在这里留着代码,以后备查。

 1 <?php
2
3
4 class Logger
5 {
6 private static $instance = null;
7
8 private $log_name = "myapp.log";
9
10 private $log_types = "NORMAL,DEBUG,DB,ERROR,FATAL";
11
12 private $types = null;
13
14 private function __construct()
15 {
16 $this->types = explode(",", $this->log_types);
17 }
18
19 public static function getInstance()
20 {
21 if (self::$instance == null) {
22 self::$instance = new Logger();
23 }
24 return self::$instance;
25 }
26
27 public function setLogFilename($filename)
28 {
29 $this->log_name = $filename;
30 }
31
32 public function msg($message, $type=0, $log_file="")
33 {
34 if ( null == $this->types )
35 {
36 $this->types = explode(",", $this->log_types);
37 }
38
39 $log_message = "";
40 $time = date("Y-m-d G:i:s");
41 $log_message .= "$time ";
42
43 if ($type < count($this->types))
44 $typename = $this->types[$type];
45 else
46 $typename = "NORMAL";
47
48 $log_message .= " $typename ";
49
50 $log_message .= "[" . $_SERVER["REQUEST_URI"] . "] ";
51 // $log_message .= $_SERVER['REMOTE_ADDR'] . " ";
52
53 $log_message .= " => $message " . "\r\n";
54
55 if ( $log_file != "" )
56 $this->setLogFilename($log_file);
57
58 error_log($log_message, 3, $this->log_name);
59 }
60 }
61
62 ?>



posted @ 2012-03-12 21:08  tecfaq  阅读(190)  评论(0编辑  收藏  举报