ILIAS  Release_3_10_x_branch Revision 61812
 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 
80  function ilLog($a_log_path, $a_log_file, $a_tag = "", $a_enabled = true, $a_log_level = NULL)
81  {
82  // init vars
83  $this->FATAL = 10;
84  $this->WARNING = 20;
85  $this->MESSAGE = 30;
86 
87  $this->default_log_level= $this->WARNING;
88  $this->current_log_level = $this->setLogLevel($a_log_level);
89 
90  $this->path = ($a_log_path) ? $a_log_path : ILIAS_ABSOLUTE_PATH;
91  $this->filename = ($a_log_file) ? $a_log_file : "ilias.log";
92  $this->tag = ($a_tag == "") ? "unknown" : $a_tag;
93  $this->enabled = (bool) $a_enabled;
94  $this->setLogFormat(@date("[y-m-d H:i:s] ")."[".$this->tag."] ");
95 
96  $this->open();
97 
98  }
99 
107  function setLogLevel($a_log_level)
108  {
109  switch (strtolower($a_log_level))
110  {
111  case "fatal":
112  return $this->FATAL;
113  case "warning":
114  return $this->WARNING;
115  case "message":
116  return $this->MESSAGE;
117  default:
118  return $this->default_log_level;
119  }
120  }
121 
129  function checkLogLevel($a_log_level)
130  {
131  if (empty($a_log_level))
132  return $this->default_log_level;
133 
134  $level = (int) $a_log_level;
135 
136  if ($a_log_level != (int) $a_log_level)
137  return $this->default_log_level;
138 
139  return $level;
140  }
141 
142  function setLogFormat($a_format)
143  {
144  $this->log_format = $a_format;
145  }
146 
147  function getLogFormat()
148  {
149  return $this->log_format;
150  }
151 
152  function setPath($a_str)
153  {
154  $this->path = $a_str;
155  }
156 
157  function setFilename($a_str)
158  {
159  $this->filename = $a_str;
160  }
161 
162  function setTag($a_str)
163  {
164  $this->tag = $a_str;
165  }
166 
176  function writeLanguageLog($a_topic,$a_lang_key)
177  {
178  //TODO: go through logfile and search for the topic
179  //only write the log if the error wasn't reported yet
180  $this->write("Language (".$a_lang_key."): topic -".$a_topic."- not present",$this->MESSAGE);
181  }
182 
189  function writeWarning($a_message)
190  {
191  $this->write("WARNING: ".$a_message);
192  }
193 
202  function logError($a_code,$a_msg)
203  {
204  switch ($a_code)
205  {
206  case "3":
207  return; // don't log messages
208  $error_level = "message";
209  break;
210 
211  case "2":
212  $error_level = "warning";
213  break;
214 
215  case "1":
216  $error_level = "fatal";
217  break;
218 
219  default:
220  $error_level = "unknown";
221  break;
222  }
223 
224  $this->write("ERROR (".$error_level."): ".$a_msg);
225  }
226 
241  function write($a_msg, $a_log_level = NULL)
242  {
243  if ($this->enabled and $this->current_log_level >= $this->checkLogLevel($a_log_level))
244  {
245  $this->open();
246 
247  if ($this->fp == false)
248  {
249  //die("Logfile: cannot open file. Please give Logfile Writepermissions.");
250  }
251 
252  if (fwrite($this->fp,$this->getLogFormat().$a_msg."\n") == -1)
253  {
254  //die("Logfile: cannot write to file. Please give Logfile Writepermissions.");
255  }
256 
257  // note: logStack() calls write() again, so do not make this call
258  // if no log level is given
259  if ($a_log_level == $this->FATAL)
260  {
261  $this->logStack();
262  }
263  }
264  }
265 
266  public function logStack($a_message = '')
267  {
268  try
269  {
270  throw new Exception($a_message);
271  }
272  catch(Exception $e)
273  {
274  $this->write($e->getTraceAsString());
275  }
276  }
277 
278 
279  private function open()
280  {
281  if(!$this->fp)
282  {
283  $this->fp = @fopen ($this->path."/".$this->filename, "a");
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 ?>