openrat-cms

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

commit 2ddcfcae1787f8739cfbba99fdea1cfcb69ed99a
parent 6478cfd63db4284b585fd198fb7263364d992b58
Author: Jan Dankert <develop@jandankert.de>
Date:   Thu, 19 Nov 2020 22:43:33 +0100

New: Configure separate logging endpoints (file, syslog, stdout, stderr), so docker container may write directly to stdout.

Diffstat:
MDockerfile | 5+++--
Mmodules/cms/Dispatcher.class.php | 23+++++++++++++++++------
Mmodules/logger/Logger.class.php | 31++++++++++++++++++++++---------
3 files changed, 42 insertions(+), 17 deletions(-)

diff --git a/Dockerfile b/Dockerfile @@ -69,8 +69,9 @@ database:\n\ suffix : _or\n\ \n\ log:\n\ - file : "log/cms.log"\n\ - level : "${env:CMS_LOG_LEVEL}"\n\ + file : ""\n\ + level: "${env:CMS_LOG_LEVEL}"\n\ + stdout: true\n\ \n\ production: ${env:CMS_PRODUCTION}\n\ \n\ diff --git a/modules/cms/Dispatcher.class.php b/modules/cms/Dispatcher.class.php @@ -194,14 +194,25 @@ class Dispatcher { $logConfig = Configuration::subset('log'); - $logFile = $logConfig->get('file',''); + Logger::$messageFormat = $logConfig->get('format',['time','level','host','text']); - // Wenn Logfile relativ angegeben wurde, dann muss dies relativ zum Root der Anwendung sein. - if ( $logFile && FileUtils::isRelativePath($logFile) ) - $logFile = __DIR__ . '/../../' . $logFile; + Logger::$logto = 0; // initially disable all logging endpoints. + + $logFile = $logConfig->get('file',''); + if ( $logFile ) { + // Write to a logfile + if ( FileUtils::isRelativePath($logFile) ) + $logFile = __DIR__ . '/../../' . $logFile; // prepend relativ path to app root + Logger::$filename = $logFile; + Logger::$logto = Logger::$logto |= Logger::LOG_TO_FILE; + } + if ( $logConfig->is('syslog')) // write to syslog + Logger::$logto = Logger::$logto |= Logger::LOG_TO_ERROR_LOG; + if ( $logConfig->is('stdout')) // write to standard out + Logger::$logto = Logger::$logto |= Logger::LOG_TO_STDOUT; + if ( $logConfig->is('stderr')) // write to standard error + Logger::$logto = Logger::$logto |= Logger::LOG_TO_STDERR; - Logger::$messageFormat = $logConfig->get('format',['time','level','host','text']); - Logger::$filename = $logFile; Logger::$dateFormat = $logConfig->get('date_format','r'); Logger::$nsLookup = $logConfig->is('ns_lookup',false); diff --git a/modules/logger/Logger.class.php b/modules/logger/Logger.class.php @@ -22,9 +22,15 @@ class Logger const OUTPUT_PLAIN = 1; const OUTPUT_JSON = 2; + const LOG_TO_FILE = 1; + const LOG_TO_STDOUT = 2; + const LOG_TO_STDERR = 4; + const LOG_TO_ERROR_LOG = 8; + public static $level = self::LEVEL_ERROR; public static $filename = null; + public static $logto = self::LOG_TO_ERROR_LOG; public static $messageFormat = ['time','level','host','text']; public static $dateFormat = 'r'; @@ -181,26 +187,33 @@ class Logger break; } - if ( Logger::$filename ) { + $text .= "\n"; + + if ( Logger::$logto & self::LOG_TO_FILE ) { // Is the file writable? // Exception: Streams (like php://stdout) are never 'writable' :/ - if ( is_writable( Logger::$filename ) || strpos(Logger::$filename,'://')!==FALSE ) { + if ( Logger::$filename && is_writable( Logger::$filename ) ) { // Writing to the logfile - // It's not a good idea to user error_log here because it failed on symlinked files. - $result = file_put_contents( Logger::$filename,$text . "\n", FILE_APPEND ); + $result = file_put_contents( Logger::$filename,$text, FILE_APPEND ); if ( $result === FALSE ) - error_log('logfile ' . Logger::$filename . ' is not available for writing'); + error_log('could not write to logfile ' . Logger::$filename ); } else { error_log('logfile ' . Logger::$filename . ' is not writable'); - error_log($text . "\n"); } } - // Errors and warnings are written to the common error log. - if (Logger::$level <= self::LEVEL_WARN) - error_log($text . "\n"); + if ( Logger::$logto & self::LOG_TO_ERROR_LOG ) { + error_log( $text ); + } + if ( Logger::$logto & self::LOG_TO_STDERR ) { + file_put_contents( 'php://stderr', $text, FILE_APPEND ); + } + if ( Logger::$logto & self::LOG_TO_STDOUT ) { + file_put_contents( 'php://stdout', $text, FILE_APPEND ); + } + } } \ No newline at end of file