ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
class.ilLog.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2011 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5
18class ilLog
19{
20
26 var $path;
28 var $tag;
30
36 var $FATAL;
37
44
51
52 var $fp = false;
53
64 function ilLog($a_log_path, $a_log_file, $a_tag = "", $a_enabled = true, $a_log_level = NULL)
65 {
66 // init vars
67 $this->FATAL = 10;
68 $this->WARNING = 20;
69 $this->MESSAGE = 30;
70
71 $this->default_log_level= $this->WARNING;
72 $this->current_log_level = $this->setLogLevel($a_log_level);
73
74 $this->path = ($a_log_path) ? $a_log_path : ILIAS_ABSOLUTE_PATH;
75 $this->filename = ($a_log_file) ? $a_log_file : "ilias.log";
76 $this->tag = ($a_tag == "") ? "unknown" : $a_tag;
77 $this->enabled = (bool) $a_enabled;
78
79 $this->setLogFormat(@date("[y-m-d H:i:s] ")."[".$this->tag."] ");
80
81 $this->open();
82
83 }
84
92 function setLogLevel($a_log_level)
93 {
94 switch (strtolower($a_log_level))
95 {
96 case "fatal":
97 return $this->FATAL;
98 case "warning":
99 return $this->WARNING;
100 case "message":
101 return $this->MESSAGE;
102 default:
103 return $this->default_log_level;
104 }
105 }
106
114 function checkLogLevel($a_log_level)
115 {
116 if (empty($a_log_level))
117 return $this->default_log_level;
118
119 $level = (int) $a_log_level;
120
121 if ($a_log_level != (int) $a_log_level)
122 return $this->default_log_level;
123
124 return $level;
125 }
126
127 function setLogFormat($a_format)
128 {
129 $this->log_format = $a_format;
130 }
131
132 function getLogFormat()
133 {
134 return $this->log_format;
135 }
136
137 function setPath($a_str)
138 {
139 $this->path = $a_str;
140
141 // on filename change reload close current file
142 if($this->fp)
143 {
144 fclose($this->fp);
145 $this->fp = null;
146 }
147 }
148
149 function setFilename($a_str)
150 {
151 $this->filename = $a_str;
152
153 // on filename change reload close current file
154 if($this->fp)
155 {
156 fclose($this->fp);
157 $this->fp = null;
158 }
159 }
160
161 function setTag($a_str)
162 {
163 $this->tag = $a_str;
164 }
165
175 function writeLanguageLog($a_topic,$a_lang_key)
176 {
177 //TODO: go through logfile and search for the topic
178 //only write the log if the error wasn't reported yet
179 $this->write("Language (".$a_lang_key."): topic -".$a_topic."- not present",$this->MESSAGE);
180 }
181
188 function writeWarning($a_message)
189 {
190 $this->write("WARNING: ".$a_message);
191 }
192
201 function logError($a_code,$a_msg)
202 {
203 switch ($a_code)
204 {
205 case "3":
206 return; // don't log messages
207 $error_level = "message";
208 break;
209
210 case "2":
211 $error_level = "warning";
212 break;
213
214 case "1":
215 $error_level = "fatal";
216 break;
217
218 default:
219 $error_level = "unknown";
220 break;
221 }
222
223 $this->write("ERROR (".$error_level."): ".$a_msg);
224 }
225
240 function write($a_msg, $a_log_level = NULL)
241 {
242 if ($this->enabled and $this->current_log_level >= $this->checkLogLevel($a_log_level))
243 {
244 $this->open();
245
246 if ($this->fp == false)
247 {
248 //die("Logfile: cannot open file. Please give Logfile Writepermissions.");
249 }
250
251 if (fwrite($this->fp,$this->getLogFormat().$a_msg."\n") == -1)
252 {
253 //die("Logfile: cannot write to file. Please give Logfile Writepermissions.");
254 }
255
256 // note: logStack() calls write() again, so do not make this call
257 // if no log level is given
258 if ($a_log_level == $this->FATAL)
259 {
260 $this->logStack();
261 }
262 }
263 }
264
265 public function logStack($a_message = '')
266 {
267 try
268 {
269 throw new Exception($a_message);
270 }
271 catch(Exception $e)
272 {
273 $this->write($e->getTraceAsString());
274 }
275 }
276
283 function dump($a_var, $a_log_level = NULL)
284 {
285 $this->write(print_r($a_var, true), $a_log_level);
286 }
287
292 private function open()
293 {
294 if(!$this->fp)
295 {
296 $this->fp = @fopen ($this->path."/".$this->filename, "a");
297 }
298
299 if (!$this->fp && $this->enabled)
300 {
301 include_once("./Services/Logging/exceptions/class.ilLogException.php");
302 throw new ilLogException('Unable to open log file for writing. Please check setup path to log file and possible write access.');
303 }
304 }
305
306 public function __destruct()
307 {
308 @fclose($this->fp);
309 }
310
311
312
316 function delete()
317 {
318 if (@is_file($this->path."/".$this->filename))
319 {
320 @unlink($this->path."/".$this->filename);
321 }
322 }
323} // END class.ilLog
324?>
ILIAS Log exception class.
logging
Definition: class.ilLog.php:19
writeLanguageLog($a_topic, $a_lang_key)
special language checking routine
setFilename($a_str)
setLogFormat($a_format)
setLogLevel($a_log_level)
set global log level
Definition: class.ilLog.php:92
logError($a_code, $a_msg)
this function is automatically called by class.ilErrorHandler in case of an error To log manually ple...
writeWarning($a_message)
special warning message
ilLog($a_log_path, $a_log_file, $a_tag="", $a_enabled=true, $a_log_level=NULL)
constructor
Definition: class.ilLog.php:64
open()
Open log file.
setPath($a_str)
setTag($a_str)
checkLogLevel($a_log_level)
determine log level
write($a_msg, $a_log_level=NULL)
getLogFormat()
dump($a_var, $a_log_level=NULL)
Dump a variable to the log.
logStack($a_message='')
__destruct()
const ILIAS_ABSOLUTE_PATH