openrat-cms

# OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

Logger.class.php (3569B)


      1 <?php
      2 // OpenRat Content Management System
      3 // Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
      4 //
      5 // This program is free software; you can redistribute it and/or
      6 // modify it under the terms of the GNU General Public License
      7 // as published by the Free Software Foundation; either version 2
      8 // of the License, or (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU General Public License
     16 // along with this program; if not, write to the Free Software
     17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     18 
     19 
     20 define('DAV_LOG_LEVEL_TRACE',5 );
     21 define('DAV_LOG_LEVEL_DEBUG',4  );
     22 define('DAV_LOG_LEVEL_INFO' ,3  );
     23 define('DAV_LOG_LEVEL_WARN' ,2  );
     24 define('DAV_LOG_LEVEL_ERROR',1  );
     25 
     26 define('DAV_LOG_LEVEL', constant('DAV_LOG_LEVEL_'.strtoupper($config['log.level'])));
     27 
     28 
     29 /**
     30  * Schreiben eines Eintrages in die Logdatei
     31  *
     32  * @author $Author$
     33  * @version $Rev: $
     34  * @package openrat.services
     35  */
     36 class Logger
     37 {
     38 	/**
     39 	 * Schreiben einer Trace-Meldung in die Logdatei
     40 	 *
     41 	 * @param message Log-Text
     42 	 */
     43 	function trace( $message )
     44 	{
     45 		if	( DAV_LOG_LEVEL >= DAV_LOG_LEVEL_TRACE )
     46 			Logger::doLog( 'trace',$message );
     47 	}
     48 
     49 
     50 	/**
     51 	 * Schreiben einer Debug-Meldung in die Logdatei
     52 	 *
     53 	 * @param message Log-Text
     54 	 */
     55 	function debug( $message )
     56 	{
     57 		if	( DAV_LOG_LEVEL >= DAV_LOG_LEVEL_DEBUG )
     58 			Logger::doLog( 'debug',$message );
     59 	}
     60 
     61 
     62 	/**
     63 	 * Schreiben einer Information in die Logdatei
     64 	 *
     65 	 * @param message Log-Text
     66 	 */
     67 	function info( $message )
     68 	{
     69 		if	( DAV_LOG_LEVEL >= DAV_LOG_LEVEL_INFO )
     70 			Logger::doLog( 'TemplateEngineInfo',$message );
     71 	}
     72 
     73 
     74 	/**
     75 	 * Schreiben einer Warnung in die Logdatei
     76 	 *
     77 	 * @param message Log-Text
     78 	 */
     79 	function warn( $message )
     80 	{
     81 		if	( DAV_LOG_LEVEL >= DAV_LOG_LEVEL_WARN )
     82 			Logger::doLog( 'warn',$message );
     83 	}
     84 
     85 
     86 	/**
     87 	 * Schreiben einer normalen Fehlermeldung in die Logdatei
     88 	 *
     89 	 * @param message Log-Text
     90 	 */
     91 	function error( $message )
     92 	{
     93 		if	( DAV_LOG_LEVEL >= DAV_LOG_LEVEL_ERROR )
     94 			Logger::doLog( 'error',$message );
     95 	}
     96 
     97 
     98 	/**
     99 	 * Schreiben der Meldung in die Logdatei
    100 	 *
    101 	 * @author $Author$
    102 	 * @param facility Schwere dieses Logdatei-Eintrages (z.B. warning)
    103 	 * @param message Log-Text
    104 	 * @access private
    105 	 */
    106 	function doLog( $facility,$message )
    107 	{
    108 		global $config;
    109 		
    110 		$filename = $config['log.file'];
    111 
    112 		if	( empty($filename) )
    113 			return;
    114 
    115 		if	( ! is_writable($filename) )
    116 			throw new \LogicException( "logfile $filename is not writable by the server" );
    117 
    118 		$thisLevel = strtoupper($facility);
    119 		
    120 		$text = "%time %level %host %action %text"; // Format der Logdatei
    121 
    122 		// Ersetzen von Variablen
    123 		$text = str_replace( '%host',getenv('REMOTE_ADDR'),$text );
    124 		
    125 		$action = strtoupper($_SERVER['REQUEST_METHOD']);
    126 			
    127 		$text = str_replace( '%level' ,str_pad($thisLevel,5),$text );
    128 		$text = str_replace( '%agent' ,getenv('HTTP_USER_AGENT'),$text );
    129 		$text = str_replace( '%action',str_pad($action,12),$text );
    130 		$text = str_replace( '%text'  ,$message,$text );
    131 		$text = str_replace( '%time'  ,date('M j H:i:s'),$text );
    132 		$text = str_replace( "\n"     ,"\n ",$text );
    133 		
    134 		// Schreiben in Logdatei
    135 		error_log( $text."\n",3,$filename );
    136 	}
    137 }
    138 
    139 ?>