commit fe331960228dd06181bec89fd0d5c92ed88dd56b
parent c7b7835277198106a66c9cc1a28cede137c6a9e3
Author: Jan Dankert <devnull@localhost>
Date: Mon, 11 Dec 2017 23:19:39 +0100
Den Logger in ein eigenes Modul verpackt. Der Logger hat nun keine Abhängigkeiten mehr zum CMS.
Diffstat:
6 files changed, 210 insertions(+), 186 deletions(-)
diff --git a/dispatcher.php b/dispatcher.php
@@ -104,9 +104,42 @@ try
// mehr veraendert.
Session::setConfig( $conf );
}
-
+
+
// Nachdem die Konfiguration gelesen wurde, kann nun der Logger benutzt werden.
- require_once( OR_SERVICECLASSES_DIR."Logger.class.".PHP_EXT );
+ require_once( OR_MODULES_DIR."logger/require.".PHP_EXT );
+
+ // Logger initialisieren
+ Logger::$messageFormat = $conf['log']['format'];
+ Logger::$filename = $conf['log']['file'];
+ Logger::$dateFormat = $conf['log']['date_format'];
+ Logger::$nsLookup = $conf['log']['ns_lookup'];
+
+ $cname = 'LOGGER_LOG_'.strtoupper($conf['log']['level']);
+ if (defined($cname))
+ Logger::$level = constant($cname);
+
+
+ Logger::$messageCallback = function()
+ {
+ $action = Session::get('action');
+ if ( empty($action) )
+ $action = '-';
+
+ $action = Session::get('action');
+ if ( empty($action) )
+ $action = '-';
+
+ $user = Session::getUser();
+ if ( is_object($user) )
+ $username = $user->name;
+ else
+ $username = '-';
+
+ return array('user'=>$username,'action'=>$action);
+ };
+ Logger::init();
+
if ( !empty($conf['security']['umask']) )
umask( octdec($conf['security']['umask']) );
diff --git a/init.php b/init.php
@@ -103,11 +103,15 @@ function fatal_handler() {
$errfile = $error["file"];
$errline = $error["line"];
$errstr = $error["message"];
-
+
+ $message = $errno .' '. $errstr.' '. $errfile.' '. $errline;
if(class_exists('Logger'))
- Logger::error( $errno .' '. $errstr.' '. $errfile.' '. $errline);
- else
- var_dump($error);
+ Logger::error( $message);
+ else
+ {
+ error_log($message);
+ var_dump($error);
+ }
}
}
@@ -121,6 +125,8 @@ require_once( OR_SERVICECLASSES_DIR."include.inc.".PHP_EXT );
require_once( OR_AUTHCLASSES_DIR."include.inc.".PHP_EXT );
+
+
require_once( OR_MODULES_DIR."security/require.".PHP_EXT );
require_once( OR_MODULES_DIR."template-engine/require.".PHP_EXT );
require_once( OR_MODULES_DIR."util/require.".PHP_EXT );
diff --git a/modules/logger/Logger.class.php b/modules/logger/Logger.class.php
@@ -0,0 +1,156 @@
+<?php
+
+
+define('LOGGER_LOG_TRACE', 5);
+define('LOGGER_LOG_DEBUG', 4);
+define('LOGGER_LOG_INFO', 3);
+define('LOGGER_LOG_WARN', 2);
+define('LOGGER_LOG_ERROR', 1);
+
+/**
+ * Schreiben eines Eintrages in die Logdatei.
+ *
+ * @author Jan Dankert
+ */
+class Logger
+{
+ public static $level = LOGGER_LOG_ERROR;
+ public static $filename = null;
+
+ public static $messageFormat = '%time %level %host %text';
+ public static $dateFormat = 'M j H:i:s';
+ public static $nsLookup = false;
+ public static $messageCallback;
+
+ public static function init()
+ {
+
+ }
+
+
+ /**
+ * Schreiben einer Trace-Meldung in die Logdatei
+ *
+ * @param string message Log-Text
+ */
+ public static function trace($message)
+ {
+ if (Logger::$level >= LOGGER_LOG_TRACE)
+ Logger::doLog(LOGGER_LOG_TRACE, $message);
+ }
+
+
+ /**
+ * Schreiben einer Debug-Meldung in die Logdatei
+ *
+ * @param string message Log-Text
+ */
+ public static function debug($message)
+ {
+ if (Logger::$level >= LOGGER_LOG_DEBUG)
+ Logger::doLog(LOGGER_LOG_DEBUG, $message);
+ }
+
+
+ /**
+ * Schreiben einer Information in die Logdatei
+ *
+ * @param string message Log-Text
+ */
+ public static function info($message)
+ {
+ if (Logger::$level >= LOGGER_LOG_INFO)
+ Logger::doLog(LOGGER_LOG_INFO, $message);
+ }
+
+
+ /**
+ * Schreiben einer Warnung in die Logdatei
+ *
+ * @param string message Log-Text
+ */
+ public static function warn($message)
+ {
+ if (Logger::$level >= LOGGER_LOG_WARN)
+ Logger::doLog(LOGGER_LOG_WARN, $message);
+ }
+
+
+ /**
+ * Schreiben einer normalen Fehlermeldung in die Logdatei
+ *
+ * @param string message Log-Text
+ */
+ public static function error($message)
+ {
+ if (Logger::$level >= LOGGER_LOG_ERROR)
+ Logger::doLog(LOGGER_LOG_ERROR, $message);
+ }
+
+
+ /**
+ * Schreiben der Meldung in die Logdatei
+ *
+ * @param string facility Schwere dieses Logdatei-Eintrages (z.B. warning)
+ * @param string message Log-Text
+ * @access private
+ */
+ private static function doLog($facility, $message)
+ {
+ if ($facility == LOGGER_LOG_ERROR)
+ $thisLevel = 'ERROR';
+ elseif ($facility == LOGGER_LOG_WARN)
+ $thisLevel = 'WARN';
+ elseif ($facility == LOGGER_LOG_INFO)
+ $thisLevel = 'INFO';
+ elseif ($facility == LOGGER_LOG_DEBUG)
+ $thisLevel = 'DEBUG';
+ elseif ($facility == LOGGER_LOG_TRACE)
+ $thisLevel = 'TRACE';
+
+
+ // Ersetzen von Variablen
+ if (Logger::$nsLookup)
+ $vars['host'] = gethostbyaddr(getenv('REMOTE_ADDR'));
+ else
+ $vars['host'] = getenv('REMOTE_ADDR');
+
+ if (isset(Logger::$messageCallback))
+ $cb = Logger::$messageCallback;
+ $vars += $cb();
+
+ $vars['level'] = str_pad($thisLevel, 5);
+ $vars['agent'] = getenv('HTTP_USER_AGENT');
+ $vars['time'] = date(Logger::$dateFormat);
+ $vars['text'] = $message;
+
+ $text = Logger::$messageFormat;
+
+ // Variablen ersetzen.
+ foreach ($vars as $key => $value) {
+ $text = str_replace('%' . $key, $value,$text);
+ }
+
+ // Mehrzeilige Meldungen werden um 1 Spalte eingerueckt, um sie maschinell
+ // erkennen und auswerten zu koennen.
+ $text = str_replace("\n", "\n ", $text);
+
+ if (isset(Logger::$filename)) {
+
+ if (!is_writable(Logger::$filename)) {
+
+ error_log('logfile ' . Logger::$filename . ' is not writable');
+ error_log($text . "\n");
+ } else {
+ // Schreiben in Logdatei
+ error_log($text . "\n", 3, Logger::$filename);
+ }
+ }
+
+ // ERROR- und WARN-Meldungen immer zusätzlich in das Error-Log schreiben.
+ if (Logger::$level <= LOGGER_LOG_WARN)
+ error_log($text . "\n");
+ }
+}
+
+?>+
\ No newline at end of file
diff --git a/modules/logger/require.php b/modules/logger/require.php
@@ -0,0 +1,5 @@
+<?php
+
+include( __DIR__.'/Logger.class.php');
+
+?>+
\ No newline at end of file
diff --git a/util/Logger.class.php b/util/Logger.class.php
@@ -1,177 +0,0 @@
-<?php
-// OpenRat Content Management System
-// Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
-//
-// This program is free software; you can redistribute it and/or
-// modify it under the terms of the GNU General Public License
-// as published by the Free Software Foundation; either version 2
-// of the License, or (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-
-switch( strtolower($conf['log']['level']) )
-{
- case 'trace':
- define('OR_LOG_LEVEL_TRACE',true );
-
- case 'debug':
- define('OR_LOG_LEVEL_DEBUG',true );
-
- case 'info':
- define('OR_LOG_LEVEL_INFO' ,true );
-
- case 'warn':
- define('OR_LOG_LEVEL_WARN' ,true );
-
- case 'error':
- define('OR_LOG_LEVEL_ERROR',true );
-}
-
-if ( !defined('OR_LOG_LEVEL_TRACE') ) define('OR_LOG_LEVEL_TRACE',false );
-if ( !defined('OR_LOG_LEVEL_DEBUG') ) define('OR_LOG_LEVEL_DEBUG',false );
-if ( !defined('OR_LOG_LEVEL_INFO' ) ) define('OR_LOG_LEVEL_INFO' ,false );
-if ( !defined('OR_LOG_LEVEL_WARN' ) ) define('OR_LOG_LEVEL_WARN' ,false );
-if ( !defined('OR_LOG_LEVEL_ERROR') ) define('OR_LOG_LEVEL_ERROR',false );
-
-
-
-/**
- * Schreiben eines Eintrages in die Logdatei
- *
- * @author $Author$
- * @version $Rev: $
- * @package openrat.services
- */
-class Logger
-{
- /**
- * Schreiben einer Trace-Meldung in die Logdatei
- *
- * @param message Log-Text
- */
- public static function trace( $message )
- {
- if ( OR_LOG_LEVEL_TRACE )
- Logger::doLog( 'trace',$message );
- }
-
-
- /**
- * Schreiben einer Debug-Meldung in die Logdatei
- *
- * @param message Log-Text
- */
- public static function debug( $message )
- {
- if ( OR_LOG_LEVEL_DEBUG )
- Logger::doLog( 'debug',$message );
- }
-
-
- /**
- * Schreiben einer Information in die Logdatei
- *
- * @param message Log-Text
- */
- public static function info( $message )
- {
- if ( OR_LOG_LEVEL_INFO )
- Logger::doLog( 'info',$message );
- }
-
-
- /**
- * Schreiben einer Warnung in die Logdatei
- *
- * @param message Log-Text
- */
- public static function warn( $message )
- {
- if ( OR_LOG_LEVEL_WARN )
- Logger::doLog( 'warn',$message );
- }
-
-
- /**
- * Schreiben einer normalen Fehlermeldung in die Logdatei
- *
- * @param message Log-Text
- */
- public static function error( $message )
- {
- if ( OR_LOG_LEVEL_ERROR )
- Logger::doLog( 'error',$message );
- }
-
-
- /**
- * Schreiben der Meldung in die Logdatei
- *
- * @author $Author$
- * @param facility Schwere dieses Logdatei-Eintrages (z.B. warning)
- * @param message Log-Text
- * @access private
- */
- private static function doLog( $facility,$message )
- {
- global $conf;
- global $SESS;
-
- $filename = $conf['log']['file'];
-
- if ( $filename == '' )
- return;
-
-
- $thisLevel = strtoupper($facility);
-
- $user = Session::getUser();
- if ( is_object($user) )
- $username = $user->name;
- else
- $username = '-';
-
- $text = $conf['log']['format']; // Format der Logdatei lesen
-
- // Ersetzen von Variablen
- if ( $conf['log']['dns_lookup'] )
- $text = str_replace( '%host',gethostbyaddr(getenv('REMOTE_ADDR')),$text );
- else
- $text = str_replace( '%host',getenv('REMOTE_ADDR'),$text );
-
- $action = Session::get('action');
- if ( empty($action) )
- $action = '-';
-
- $action = Session::get('action');
- if ( empty($action) )
- $action = '-';
-
- $text = str_replace( '%user' ,str_pad($username ,8),$text );
- $text = str_replace( '%level' ,str_pad($thisLevel,5),$text );
- $text = str_replace( '%agent' ,getenv('HTTP_USER_AGENT'),$text );
- $text = str_replace( '%action',str_pad($action,12),$text );
- $text = str_replace( '%text' ,$message,$text );
- $text = str_replace( '%time' ,date($conf['log']['date_format']),$text );
- $text = str_replace( "\n" ,"\n ",$text );
- if (! is_writable($filename)) {
-
- error_log("logfile $filename is not writable by the server");
- error_log($text . "\n");
- } else {
-
- // Schreiben in Logdatei
- error_log($text . "\n", 3, $filename);
- }
- }
-}
-
-?>-
\ No newline at end of file
diff --git a/util/config-default.php b/util/config-default.php
@@ -367,10 +367,10 @@ $conf['login']['start']['start_lastchanged_object']=true;
$conf['login']['start']['start_single_project']=true;
$conf['login']['default-database']='db';
$conf['log'] = array();
-$conf['log']['file']= "";
+$conf['log']['file']= null;
$conf['log']['level']= "warn";
$conf['log']['date_format']= "M j H:i:s";
-$conf['log']['dns_lookup']=false;
+$conf['log']['ns_lookup']=false;
$conf['log']['format']= "%time %level %host %user %action %text";
$conf['mail'] = array();
$conf['mail']['enabled']=true;