ILIAS  release_4-3 Revision
 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 
63  function ilLog($a_log_path, $a_log_file, $a_tag = "", $a_enabled = true, $a_log_level = NULL)
64  {
65  // init vars
66  $this->FATAL = 10;
67  $this->WARNING = 20;
68  $this->MESSAGE = 30;
69 
70  $this->default_log_level= $this->WARNING;
71  $this->current_log_level = $this->setLogLevel($a_log_level);
72 
73  $this->path = ($a_log_path) ? $a_log_path : ILIAS_ABSOLUTE_PATH;
74  $this->filename = ($a_log_file) ? $a_log_file : "ilias.log";
75  $this->tag = ($a_tag == "") ? "unknown" : $a_tag;
76  $this->enabled = (bool) $a_enabled;
77 
78  $this->setLogFormat(@date("[y-m-d H:i:s] ")."[".$this->tag."] ");
79 
80  $this->open();
81 
82  }
83 
91  function setLogLevel($a_log_level)
92  {
93  switch (strtolower($a_log_level))
94  {
95  case "fatal":
96  return $this->FATAL;
97  case "warning":
98  return $this->WARNING;
99  case "message":
100  return $this->MESSAGE;
101  default:
102  return $this->default_log_level;
103  }
104  }
105 
113  function checkLogLevel($a_log_level)
114  {
115  if (empty($a_log_level))
116  return $this->default_log_level;
117 
118  $level = (int) $a_log_level;
119 
120  if ($a_log_level != (int) $a_log_level)
121  return $this->default_log_level;
122 
123  return $level;
124  }
125 
126  function setLogFormat($a_format)
127  {
128  $this->log_format = $a_format;
129  }
130 
131  function getLogFormat()
132  {
133  return $this->log_format;
134  }
135 
136  function setPath($a_str)
137  {
138  $this->path = $a_str;
139  }
140 
141  function setFilename($a_str)
142  {
143  $this->filename = $a_str;
144  }
145 
146  function setTag($a_str)
147  {
148  $this->tag = $a_str;
149  }
150 
160  function writeLanguageLog($a_topic,$a_lang_key)
161  {
162  //TODO: go through logfile and search for the topic
163  //only write the log if the error wasn't reported yet
164  $this->write("Language (".$a_lang_key."): topic -".$a_topic."- not present",$this->MESSAGE);
165  }
166 
173  function writeWarning($a_message)
174  {
175  $this->write("WARNING: ".$a_message);
176  }
177 
186  function logError($a_code,$a_msg)
187  {
188  switch ($a_code)
189  {
190  case "3":
191  return; // don't log messages
192  $error_level = "message";
193  break;
194 
195  case "2":
196  $error_level = "warning";
197  break;
198 
199  case "1":
200  $error_level = "fatal";
201  break;
202 
203  default:
204  $error_level = "unknown";
205  break;
206  }
207 
208  $this->write("ERROR (".$error_level."): ".$a_msg);
209  }
210 
225  function write($a_msg, $a_log_level = NULL)
226  {
227  if ($this->enabled and $this->current_log_level >= $this->checkLogLevel($a_log_level))
228  {
229  $this->open();
230 
231  if ($this->fp == false)
232  {
233  //die("Logfile: cannot open file. Please give Logfile Writepermissions.");
234  }
235 
236  if (fwrite($this->fp,$this->getLogFormat().$a_msg."\n") == -1)
237  {
238  //die("Logfile: cannot write to file. Please give Logfile Writepermissions.");
239  }
240 
241  // note: logStack() calls write() again, so do not make this call
242  // if no log level is given
243  if ($a_log_level == $this->FATAL)
244  {
245  $this->logStack();
246  }
247  }
248  }
249 
250  public function logStack($a_message = '')
251  {
252  try
253  {
254  throw new Exception($a_message);
255  }
256  catch(Exception $e)
257  {
258  $this->write($e->getTraceAsString());
259  }
260  }
261 
268  function dump($a_var, $a_log_level = NULL)
269  {
270  $this->write(print_r($a_var, true), $a_log_level);
271  }
272 
273  private function open()
274  {
275  if(!$this->fp)
276  {
277  $this->fp = @fopen ($this->path."/".$this->filename, "a");
278  }
279 
280  if (!$this->fp && $this->enabled)
281  {
282  include_once("./Services/Logging/exceptions/class.ilLogException.php");
283  throw new ilLogException('Unable to open log file for writing. Please check setup path to log file and possible write access.');
284  }
285  }
286 
287  public function __destruct()
288  {
289  @fclose($this->fp);
290  }
291 
292 
293 
297  function delete()
298  {
299  if (@is_file($this->path."/".$this->filename))
300  {
301  @unlink($this->path."/".$this->filename);
302  }
303  }
304 } // END class.ilLog
305 ?>