Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 149 lines (123 sloc) 3.736 kb
34c84c8 @deiu Added support for basic logging (for now). Fixed minor bugs.
deiu authored
1 <?php
2
3 /* Finally, A light, permissions-checking logging class.
4 *
5 * Author : Kenneth Katzgrau < katzgrau@gmail.com >
6 * Date : July 26, 2008
7 * Comments : Originally written for use with wpSearch
8 * Website : http://codefury.net
9 * Version : 1.0
10 *
11 * Usage:
12 * $log = new KLogger ( "log.txt" , KLogger::INFO );
13 * $log->LogInfo("Returned a million search results"); //Prints to the log file
14 * $log->LogFATAL("Oh dear."); //Prints to the log file
15 * $log->LogDebug("x = 5"); //Prints nothing due to priority setting
16 */
17
18 class KLogger
19 {
20
21 const DEBUG = 1; // Most Verbose
22 const INFO = 2; // ...
23 const WARN = 3; // ...
24 const ERROR = 4; // ...
25 const FATAL = 5; // Least Verbose
26 const OFF = 6; // Nothing at all.
27
28 const LOG_OPEN = 1;
29 const OPEN_FAILED = 2;
30 const LOG_CLOSED = 3;
31
32 /* Public members: Not so much of an example of encapsulation, but that's okay. */
33 public $Log_Status = KLogger::LOG_CLOSED;
34 public $DateFormat = "Y-m-d G:i:s";
35 public $MessageQueue;
36
37 private $log_file;
38 private $priority = KLogger::INFO;
39
40 private $file_handle;
41
42 public function __construct( $filepath , $priority )
43 {
44 if ( $priority == KLogger::OFF ) return;
45
46 $this->log_file = $filepath;
47 $this->MessageQueue = array();
48 $this->priority = $priority;
49
50 if ( file_exists( $this->log_file ) )
51 {
52 if ( !is_writable($this->log_file) )
53 {
54 $this->Log_Status = KLogger::OPEN_FAILED;
55 $this->MessageQueue[] = "The file exists, but could not be opened for writing. Check that appropriate permissions have been set.";
56 return;
57 }
58 }
59
60 if ( $this->file_handle = fopen( $this->log_file , "a" ) )
61 {
62 $this->Log_Status = KLogger::LOG_OPEN;
63 $this->MessageQueue[] = "The log file was opened successfully.";
64 }
65 else
66 {
67 $this->Log_Status = KLogger::OPEN_FAILED;
68 $this->MessageQueue[] = "The file could not be opened. Check permissions.";
69 }
70
71 return;
72 }
73
74 public function __destruct()
75 {
76 if ( $this->file_handle )
77 fclose( $this->file_handle );
78 }
79
80 public function LogInfo($line)
81 {
82 $this->Log( $line , KLogger::INFO );
83 }
84
85 public function LogDebug($line)
86 {
87 $this->Log( $line , KLogger::DEBUG );
88 }
89
90 public function LogWarn($line)
91 {
92 $this->Log( $line , KLogger::WARN );
93 }
94
95 public function LogError($line)
96 {
97 $this->Log( $line , KLogger::ERROR );
98 }
99
100 public function LogFatal($line)
101 {
102 $this->Log( $line , KLogger::FATAL );
103 }
104
105 public function Log($line, $priority)
106 {
107 if ( $this->priority <= $priority )
108 {
109 $status = $this->getTimeLine( $priority );
110 $this->WriteFreeFormLine ( "$status $line \n" );
111 }
112 }
113
114 public function WriteFreeFormLine( $line )
115 {
116 if ( $this->Log_Status == KLogger::LOG_OPEN && $this->priority != KLogger::OFF )
117 {
118 if (fwrite( $this->file_handle , $line ) === false) {
119 $this->MessageQueue[] = "The file could not be written to. Check that appropriate permissions have been set.";
120 }
121 }
122 }
123
124 private function getTimeLine( $level )
125 {
126 $time = date( $this->DateFormat );
127
128 switch( $level )
129 {
130 case KLogger::INFO:
131 return "$time - INFO -->";
132 case KLogger::WARN:
133 return "$time - WARN -->";
134 case KLogger::DEBUG:
135 return "$time - DEBUG -->";
136 case KLogger::ERROR:
137 return "$time - ERROR -->";
138 case KLogger::FATAL:
139 return "$time - FATAL -->";
140 default:
141 return "$time - LOG -->";
142 }
143 }
144
145 }
146
147
148 ?>
Something went wrong with that request. Please try again.