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