• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Services/Logging/classes/class.ilLog.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2008 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00037 class ilLog
00038 {
00039 
00045         var $path;
00046         var $filename;
00047         var $tag;
00048         var $log_format;
00049  
00055         var $FATAL;
00056 
00062         var $WARNING;
00063 
00069         var $MESSAGE;
00070 
00080         function ilLog($a_log_path, $a_log_file, $a_tag = "", $a_enabled = true, $a_log_level = NULL)
00081         {
00082                 // init vars
00083                 $this->FATAL     = 10;
00084                 $this->WARNING   = 20;
00085                 $this->MESSAGE   = 30;
00086   
00087         $this->default_log_level= $this->WARNING;
00088         $this->current_log_level = $this->setLogLevel($a_log_level);
00089 
00090                 $this->path = ($a_log_path) ? $a_log_path : ILIAS_ABSOLUTE_PATH;
00091                 $this->filename = ($a_log_file) ? $a_log_file : "ilias.log";
00092                 $this->tag = ($a_tag == "") ? "unknown" : $a_tag;
00093                 $this->enabled = (bool) $a_enabled;
00094                 $this->setLogFormat(date("[y-m-d H:i:s] ")."[".$this->tag."] ");
00095                 
00096                 $this->open();
00097 
00098         }
00099  
00107     function setLogLevel($a_log_level)
00108     {
00109         switch (strtolower($a_log_level))
00110         {
00111             case "fatal":
00112                 return $this->FATAL;
00113             case "warning":
00114                 return $this->WARNING;
00115             case "message":
00116                 return $this->MESSAGE;
00117             default:
00118                 return $this->default_log_level;
00119         }
00120     }
00121 
00129     function checkLogLevel($a_log_level)
00130     {
00131         if (empty($a_log_level))
00132             return $this->default_log_level;
00133 
00134         $level = (int) $a_log_level;
00135         
00136         if ($a_log_level != (int) $a_log_level)
00137             return $this->default_log_level;
00138         
00139         return $level;
00140     }
00141 
00142         function setLogFormat($a_format)
00143         {
00144                 $this->log_format = $a_format;
00145         }
00146         
00147         function getLogFormat()
00148         {
00149                 return $this->log_format;
00150         }
00151 
00152         function setPath($a_str)
00153         {
00154                 $this->path = $a_str;
00155         }
00156 
00157         function setFilename($a_str)
00158         {
00159                 $this->filename = $a_str;
00160         }
00161 
00162         function setTag($a_str)
00163         {
00164                 $this->tag = $a_str;
00165         }
00166         
00176         function writeLanguageLog($a_topic,$a_lang_key)
00177         {
00178                 //TODO: go through logfile and search for the topic
00179                 //only write the log if the error wasn't reported yet
00180                 $this->write("Language (".$a_lang_key."): topic -".$a_topic."- not present",$this->MESSAGE);
00181         }
00182 
00189         function writeWarning($a_message)
00190         {
00191                 $this->write("WARNING: ".$a_message);
00192         }
00193         
00202         function logError($a_code,$a_msg)
00203         {
00204                 switch ($a_code)
00205                 {
00206                         case "3":
00207                                 return; // don't log messages
00208                                 $error_level = "message";
00209                                 break;
00210 
00211                         case "2":
00212                                 $error_level = "warning";
00213                                 break;
00214 
00215                         case "1":
00216                                 $error_level = "fatal";
00217                                 break;
00218                                 
00219                         default:
00220                                 $error_level = "unknown";
00221                                 break;
00222                 }
00223                 
00224                 $this->write("ERROR (".$error_level."): ".$a_msg);      
00225         }
00226 
00241         function write($a_msg, $a_log_level = NULL)
00242         {
00243                 if ($this->enabled and $this->current_log_level >= $this->checkLogLevel($a_log_level))
00244                 {
00245                         $this->open();
00246                         
00247                         if ($this->fp == false)
00248                         {
00249                                 die("Logfile: cannot open file. Please give Logfile Writepermissions.");
00250                         }
00251 
00252                         if (fwrite($this->fp,$this->getLogFormat().$a_msg."\n") == -1)
00253                         {
00254                                 die("Logfile: cannot write to file. Please give Logfile Writepermissions.");
00255                         }
00256                         
00257                         // note: logStack() calls write() again, so do not make this call
00258                         // if no log level is given
00259                         if ($a_log_level == $this->FATAL)
00260                         {
00261                                 $this->logStack();
00262                         }
00263                 }
00264         }
00265         
00266         public function logStack($a_message = '')
00267         {
00268                 try
00269                 {
00270                         throw new Exception($a_message);
00271                 }
00272                 catch(Exception $e)
00273                 {
00274                         $this->write($e->getTraceAsString());
00275                 }
00276         }
00277         
00278         
00279         private function open()
00280         {
00281                 if(!$this->fp)
00282                 {
00283                     $this->fp = @fopen ($this->path."/".$this->filename, "a");
00284                 }
00285         }
00286         
00287         public function __destruct()
00288         {
00289                 @fclose($this->fp);
00290         }
00291 
00292         
00293 
00297         function delete()
00298         {
00299                 if (@is_file($this->path."/".$this->filename))
00300                 {
00301                         @unlink($this->path."/".$this->filename);
00302                 }
00303         }
00304 } // END class.ilLog
00305 ?>

Generated on Fri Dec 13 2013 17:56:57 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1