ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
18 class ilLog
19 {
20 
26  var $path;
27  var $filename;
28  var $tag;
30 
36  var $FATAL;
37 
43  var $WARNING;
44 
50  var $MESSAGE;
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 
142  function setFilename($a_str)
143  {
144  $this->filename = $a_str;
145 
146  // on filename change reload close current file
147  if($this->fp)
148  {
149  fclose($this->fp);
150  $this->fp = null;
151  }
152  }
153 
154  function setTag($a_str)
155  {
156  $this->tag = $a_str;
157  }
158 
168  function writeLanguageLog($a_topic,$a_lang_key)
169  {
170  //TODO: go through logfile and search for the topic
171  //only write the log if the error wasn't reported yet
172  $this->write("Language (".$a_lang_key."): topic -".$a_topic."- not present",$this->MESSAGE);
173  }
174 
181  function writeWarning($a_message)
182  {
183  $this->write("WARNING: ".$a_message);
184  }
185 
194  function logError($a_code,$a_msg)
195  {
196  switch ($a_code)
197  {
198  case "3":
199  return; // don't log messages
200  $error_level = "message";
201  break;
202 
203  case "2":
204  $error_level = "warning";
205  break;
206 
207  case "1":
208  $error_level = "fatal";
209  break;
210 
211  default:
212  $error_level = "unknown";
213  break;
214  }
215 
216  $this->write("ERROR (".$error_level."): ".$a_msg);
217  }
218 
233  function write($a_msg, $a_log_level = NULL)
234  {
235  if ($this->enabled and $this->current_log_level >= $this->checkLogLevel($a_log_level))
236  {
237  $this->open();
238 
239  if ($this->fp == false)
240  {
241  //die("Logfile: cannot open file. Please give Logfile Writepermissions.");
242  }
243 
244  if (fwrite($this->fp,$this->getLogFormat().$a_msg."\n") == -1)
245  {
246  //die("Logfile: cannot write to file. Please give Logfile Writepermissions.");
247  }
248 
249  // note: logStack() calls write() again, so do not make this call
250  // if no log level is given
251  if ($a_log_level == $this->FATAL)
252  {
253  $this->logStack();
254  }
255  }
256  }
257 
258  public function logStack($a_message = '')
259  {
260  try
261  {
262  throw new Exception($a_message);
263  }
264  catch(Exception $e)
265  {
266  $this->write($e->getTraceAsString());
267  }
268  }
269 
276  function dump($a_var, $a_log_level = NULL)
277  {
278  $this->write(print_r($a_var, true), $a_log_level);
279  }
280 
285  private function open()
286  {
287  if(!$this->fp)
288  {
289  $this->fp = @fopen ($this->path."/".$this->filename, "a");
290  }
291 
292  if (!$this->fp && $this->enabled)
293  {
294  include_once("./Services/Logging/exceptions/class.ilLogException.php");
295  throw new ilLogException('Unable to open log file for writing. Please check setup path to log file and possible write access.');
296  }
297  }
298 
299  public function __destruct()
300  {
301  @fclose($this->fp);
302  }
303 
304 
305 
309  function delete()
310  {
311  if (@is_file($this->path."/".$this->filename))
312  {
313  @unlink($this->path."/".$this->filename);
314  }
315  }
316 } // END class.ilLog
317 ?>