ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 include_once './Services/Logging/classes/public/class.ilLogLevel.php';
68
69 $this->FATAL = ilLogLevel::CRITICAL;
70 $this->WARNING = ilLogLevel::WARNING;
71 $this->MESSAGE = ilLogLevel::INFO;
72
73 $this->default_log_level= $this->WARNING;
74 $this->current_log_level = $this->setLogLevel($a_log_level);
75
76 $this->path = ($a_log_path) ? $a_log_path : ILIAS_ABSOLUTE_PATH;
77 $this->filename = ($a_log_file) ? $a_log_file : "ilias.log";
78 $this->tag = ($a_tag == "") ? "unknown" : $a_tag;
79 $this->enabled = (bool) $a_enabled;
80
81 $this->setLogFormat(@date("[y-m-d H:i:s] ")."[".$this->tag."] ");
82
83 $this->open();
84
85 }
86
94 function setLogLevel($a_log_level)
95 {
96 switch (strtolower($a_log_level))
97 {
98 case "fatal":
99 return $this->FATAL;
100 case "warning":
101 return $this->WARNING;
102 case "message":
103 return $this->MESSAGE;
104 default:
105 return $this->default_log_level;
106 }
107 }
108
116 function checkLogLevel($a_log_level)
117 {
118 if (empty($a_log_level))
119 return $this->default_log_level;
120
121 $level = (int) $a_log_level;
122
123 if ($a_log_level != (int) $a_log_level)
124 return $this->default_log_level;
125
126 return $level;
127 }
128
129 function setLogFormat($a_format)
130 {
131 $this->log_format = $a_format;
132 }
133
134 function getLogFormat()
135 {
136 return $this->log_format;
137 }
138
139 function setPath($a_str)
140 {
141 $this->path = $a_str;
142
143 // on filename change reload close current file
144 if($this->fp)
145 {
146 fclose($this->fp);
147 $this->fp = null;
148 }
149 }
150
151 function setFilename($a_str)
152 {
153 $this->filename = $a_str;
154
155 // on filename change reload close current file
156 if($this->fp)
157 {
158 fclose($this->fp);
159 $this->fp = null;
160 }
161 }
162
163 function setTag($a_str)
164 {
165 $this->tag = $a_str;
166 }
167
177 function writeLanguageLog($a_topic,$a_lang_key)
178 {
179 //TODO: go through logfile and search for the topic
180 //only write the log if the error wasn't reported yet
181 $this->write("Language (".$a_lang_key."): topic -".$a_topic."- not present",$this->MESSAGE);
182 }
183
190 function writeWarning($a_message)
191 {
192 $this->write("WARNING: ".$a_message);
193 }
194
203 function logError($a_code,$a_msg)
204 {
205 switch ($a_code)
206 {
207 case "3":
208 return; // don't log messages
209 $error_level = "message";
210 break;
211
212 case "2":
213 $error_level = "warning";
214 break;
215
216 case "1":
217 $error_level = "fatal";
218 break;
219
220 default:
221 $error_level = "unknown";
222 break;
223 }
224
225 $this->write("ERROR (".$error_level."): ".$a_msg);
226 }
227
242 function write($a_msg, $a_log_level = NULL)
243 {
244 if ($this->enabled and $this->current_log_level >= $this->checkLogLevel($a_log_level))
245 {
246 $this->open();
247
248 if ($this->fp == false)
249 {
250 //die("Logfile: cannot open file. Please give Logfile Writepermissions.");
251 }
252
253 if (fwrite($this->fp,$this->getLogFormat().$a_msg."\n") == -1)
254 {
255 //die("Logfile: cannot write to file. Please give Logfile Writepermissions.");
256 }
257
258 // note: logStack() calls write() again, so do not make this call
259 // if no log level is given
260 if ($a_log_level == $this->FATAL)
261 {
262 $this->logStack();
263 }
264 }
265 }
266
267 public function logStack($a_message = '')
268 {
269 try
270 {
271 throw new Exception($a_message);
272 }
273 catch(Exception $e)
274 {
275 $this->write($e->getTraceAsString());
276 }
277 }
278
285 function dump($a_var, $a_log_level = NULL)
286 {
287 $this->write(print_r($a_var, true), $a_log_level);
288 }
289
294 private function open()
295 {
296 if(!$this->fp)
297 {
298 $this->fp = @fopen ($this->path."/".$this->filename, "a");
299 }
300
301 if (!$this->fp && $this->enabled)
302 {
303 include_once("./Services/Logging/exceptions/class.ilLogException.php");
304 throw new ilLogException('Unable to open log file for writing. Please check setup path to log file and possible write access.');
305 }
306 }
307
308 public function __destruct()
309 {
310 @fclose($this->fp);
311 }
312
313
314
318 function delete()
319 {
320 if (@is_file($this->path."/".$this->filename))
321 {
322 @unlink($this->path."/".$this->filename);
323 }
324 }
325} // END class.ilLog
326?>
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:94
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