ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLog.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
37 class ilLog
38 {
39 
45  var $path;
46  var $filename;
47  var $tag;
49 
55  var $FATAL;
56 
62  var $WARNING;
63 
69  var $MESSAGE;
70 
71  var $fp = false;
72 
82  function ilLog($a_log_path, $a_log_file, $a_tag = "", $a_enabled = true, $a_log_level = NULL)
83  {
84  // init vars
85  $this->FATAL = 10;
86  $this->WARNING = 20;
87  $this->MESSAGE = 30;
88 
89  $this->default_log_level= $this->WARNING;
90  $this->current_log_level = $this->setLogLevel($a_log_level);
91 
92  $this->path = ($a_log_path) ? $a_log_path : ILIAS_ABSOLUTE_PATH;
93  $this->filename = ($a_log_file) ? $a_log_file : "ilias.log";
94  $this->tag = ($a_tag == "") ? "unknown" : $a_tag;
95  $this->enabled = (bool) $a_enabled;
96  $this->setLogFormat(@date("[y-m-d H:i:s] ")."[".$this->tag."] ");
97 
98  $this->open();
99 
100  }
101 
109  function setLogLevel($a_log_level)
110  {
111  switch (strtolower($a_log_level))
112  {
113  case "fatal":
114  return $this->FATAL;
115  case "warning":
116  return $this->WARNING;
117  case "message":
118  return $this->MESSAGE;
119  default:
120  return $this->default_log_level;
121  }
122  }
123 
131  function checkLogLevel($a_log_level)
132  {
133  if (empty($a_log_level))
134  return $this->default_log_level;
135 
136  $level = (int) $a_log_level;
137 
138  if ($a_log_level != (int) $a_log_level)
139  return $this->default_log_level;
140 
141  return $level;
142  }
143 
144  function setLogFormat($a_format)
145  {
146  $this->log_format = $a_format;
147  }
148 
149  function getLogFormat()
150  {
151  return $this->log_format;
152  }
153 
154  function setPath($a_str)
155  {
156  $this->path = $a_str;
157  }
158 
159  function setFilename($a_str)
160  {
161  $this->filename = $a_str;
162  }
163 
164  function setTag($a_str)
165  {
166  $this->tag = $a_str;
167  }
168 
178  function writeLanguageLog($a_topic,$a_lang_key)
179  {
180  //TODO: go through logfile and search for the topic
181  //only write the log if the error wasn't reported yet
182  $this->write("Language (".$a_lang_key."): topic -".$a_topic."- not present",$this->MESSAGE);
183  }
184 
191  function writeWarning($a_message)
192  {
193  $this->write("WARNING: ".$a_message);
194  }
195 
204  function logError($a_code,$a_msg)
205  {
206  switch ($a_code)
207  {
208  case "3":
209  return; // don't log messages
210  $error_level = "message";
211  break;
212 
213  case "2":
214  $error_level = "warning";
215  break;
216 
217  case "1":
218  $error_level = "fatal";
219  break;
220 
221  default:
222  $error_level = "unknown";
223  break;
224  }
225 
226  $this->write("ERROR (".$error_level."): ".$a_msg);
227  }
228 
243  function write($a_msg, $a_log_level = NULL)
244  {
245  if ($this->enabled and $this->current_log_level >= $this->checkLogLevel($a_log_level))
246  {
247  $this->open();
248 
249  if ($this->fp == false)
250  {
251  //die("Logfile: cannot open file. Please give Logfile Writepermissions.");
252  }
253 
254  if (fwrite($this->fp,$this->getLogFormat().$a_msg."\n") == -1)
255  {
256  //die("Logfile: cannot write to file. Please give Logfile Writepermissions.");
257  }
258 
259  // note: logStack() calls write() again, so do not make this call
260  // if no log level is given
261  if ($a_log_level == $this->FATAL)
262  {
263  $this->logStack();
264  }
265  }
266  }
267 
268  public function logStack($a_message = '')
269  {
270  try
271  {
272  throw new Exception($a_message);
273  }
274  catch(Exception $e)
275  {
276  $this->write($e->getTraceAsString());
277  }
278  }
279 
286  function dump($a_var, $a_log_level = NULL)
287  {
288  $this->write(print_r($a_var, true), $a_log_level);
289  }
290 
291  private function open()
292  {
293  if(!$this->fp)
294  {
295  $this->fp = @fopen ($this->path."/".$this->filename, "a");
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 ?>