diff --git a/config-templates/config.php b/config-templates/config.php index 21263d2c796004290200529e646fab724a2abce7..b82efd048079dd213cdea5529036be180629313f 100644 --- a/config-templates/config.php +++ b/config-templates/config.php @@ -125,6 +125,34 @@ $config = array ( 'logging.level' => SimpleSAML_Logger::NOTICE, 'logging.handler' => 'syslog', + /* + * Specify the format of the logs. Its use varies depending on the log handler used (for instance, you cannot + * control here how dates are displayed when using the syslog or errorlog handlers), but in general the options + * are: + * + * - %date{<format>}: the date and time, with its format specified inside the brackets. See the PHP documentation + * of the strftime() function for more information on the format. If the brackets are omitted, the standard + * format is applied. This can be useful if you just want to control the placement of the date, but don't care + * about the format. + * + * - %process: the name of the SimpleSAMLphp process. Remember you can configure this in the 'logging.processname' + * option below. + * + * - %level: the log level (name or number depending on the handler used). + * + * - %stat: if the log entry is intended for statistical purposes, it will print the string 'STAT ' (bear in mind + * the trailing space). + * + * - %trackid: the track ID, an identifier that allows you to track a single session. + * + * - %srcip: the IP address of the client. If you are behind a proxy, make sure to modify the + * $_SERVER['REMOTE_ADDR'] variable on your code accordingly to the X-Forwarded-For header. + * + * - %msg: the message to be logged. + * + */ + //'logging.format' => '%date{%b %d %H:%M:%S} %process %level %stat[%trackid] %msg', + /* * Choose which facility should be used when logging with syslog. * diff --git a/lib/SimpleSAML/Logger.php b/lib/SimpleSAML/Logger.php index 8a094f573cdf77c9a9bbbe9d913f4a1b34929d5b..9ea59825ab2c138f5dc3688c685c32c0432129f4 100644 --- a/lib/SimpleSAML/Logger.php +++ b/lib/SimpleSAML/Logger.php @@ -11,6 +11,8 @@ interface SimpleSAML_Logger_LoggingHandler { function log_internal($level,$string); + + function setLogFormat($format); } class SimpleSAML_Logger { @@ -42,6 +44,35 @@ class SimpleSAML_Logger { */ private static $trackid = null; + /** + * This variable holds the format used to log any message. Its use varies depending on the log handler used (for + * instance, you cannot control here how dates are displayed when using syslog or errorlog handlers), but in + * general the options are: + * + * - %date{<format>}: the date and time, with its format specified inside the brackets. See the PHP documentation + * of the strftime() function for more information on the format. If the brackets are omitted, the standard + * format is applied. This can be useful if you just want to control the placement of the date, but don't care + * about the format. + * + * - %process: the name of the SimpleSAMLphp process. Remember you can configure this in the 'logging.processname' + * option. + * + * - %level: the log level (name or number depending on the handler used). + * + * - %stat: if the log entry is intended for statistical purposes, it will print the string 'STAT ' (bear in mind + * the trailing space). + * + * - %trackid: the track ID, an identifier that allows you to track a single session. + * + * - %srcip: the IP address of the client. If you are behind a proxy, make sure to modify the + * $_SERVER['REMOTE_ADDR'] variable on your code accordingly to the X-Forwarded-For header. + * + * - %msg: the message to be logged. + * + * @var string The format of the log line. + */ + private static $format = '%date{%b %d %H:%M:%S} %process %level %stat[%trackid] %msg'; + /* * LOG_ERR No statistics, only errors * LOG_WARNING No statistics, only warnings/errors @@ -141,6 +172,10 @@ class SimpleSAML_Logger { } else { throw new Exception('Invalid value for the [logging.handler] configuration option. Unknown handler: ' . $handler); } + + self::$format = $config->getString('logging.format', self::$format); + $sh->setLogFormat(self::$format); + /* Set the session handler. */ self::$loggingHandler = $sh; } @@ -188,9 +223,18 @@ class SimpleSAML_Logger { if (self::$logLevel >= $level || $statsLog) { if (is_array($string)) $string = implode(",",$string); - $string = '['.self::getTrackId().'] '.$string; - if ($statsLog) $string = 'STAT '.$string; - self::$loggingHandler->log_internal($level,$string); + + $formats = array('%trackid', '%msg', '%srcip', '%stat'); + $replacements = array(self::getTrackId(), $string, $_SERVER['REMOTE_ADDR']); + + $stat = ''; + if ($statsLog) { + $stat = 'STAT '; + } + array_push($replacements, $stat); + + $string = str_replace($formats, $replacements, self::$format); + self::$loggingHandler->log_internal($level, $string); } } diff --git a/lib/SimpleSAML/Logger/LoggingHandlerErrorLog.php b/lib/SimpleSAML/Logger/LoggingHandlerErrorLog.php index 0a687ee878bc4f5ed701ba74933cbb0b78ebfbdc..cd9404d7faba3aa9ad702d39fa74f2b5ea54ae9d 100644 --- a/lib/SimpleSAML/Logger/LoggingHandlerErrorLog.php +++ b/lib/SimpleSAML/Logger/LoggingHandlerErrorLog.php @@ -26,18 +26,29 @@ class SimpleSAML_Logger_LoggingHandlerErrorLog implements SimpleSAML_Logger_Logg ); - function log_internal($level, $string) { - $config = SimpleSAML_Configuration::getInstance(); - assert($config instanceof SimpleSAML_Configuration); - $processname = $config->getString('logging.processname','simpleSAMLphp'); - + function setLogFormat($format) { + $this->format = $format; + } + + + function log_internal($level, $string) { + $config = SimpleSAML_Configuration::getInstance(); + assert($config instanceof SimpleSAML_Configuration); + $processname = $config->getString('logging.processname','simpleSAMLphp'); + if(array_key_exists($level, self::$levelNames)) { $levelName = self::$levelNames[$level]; } else { $levelName = sprintf('UNKNOWN%d', $level); } - error_log($processname.' - '.$levelName . ': ' . $string); + $formats = array('%process', '%level'); + $replacements = array($processname, $levelName); + $string = str_replace($formats, $replacements, $string); + $string = preg_replace('/%\w+(\{[^\}]+\})?/', '', $string); + $string = trim($string); + + error_log($string); } } diff --git a/lib/SimpleSAML/Logger/LoggingHandlerFile.php b/lib/SimpleSAML/Logger/LoggingHandlerFile.php index 43ed5fb16409ddee8507792ebed4d196d7ddcb54..cb5e2b113dd3947e2297e8a5e1f212d8abc3d767 100644 --- a/lib/SimpleSAML/Logger/LoggingHandlerFile.php +++ b/lib/SimpleSAML/Logger/LoggingHandlerFile.php @@ -4,7 +4,7 @@ * A class for logging * * @author Lasse Birnbaum Jensen, SDU. - * @author Andreas Åkre Solberg, UNINETT AS. <andreas.solberg@uninett.no> + * @author Andreas Ã…kre Solberg, UNINETT AS. <andreas.solberg@uninett.no> * @package simpleSAMLphp * @version $ID$ */ @@ -26,8 +26,9 @@ class SimpleSAML_Logger_LoggingHandlerFile implements SimpleSAML_Logger_LoggingH SimpleSAML_Logger::DEBUG => 'DEBUG', ); - private $logFile = null; + private $logFile = null; private $processname = null; + private $format; function __construct() { $config = SimpleSAML_Configuration::getInstance(); @@ -35,7 +36,7 @@ class SimpleSAML_Logger_LoggingHandlerFile implements SimpleSAML_Logger_LoggingH /* Get the metadata handler option from the configuration. */ $this->logFile = $config->getPathValue('loggingdir', 'log/').$config->getString('logging.logfile', 'simplesamlphp.log'); - $this->processname = $config->getString('logging.processname','simpleSAMLphp'); + $this->processname = $config->getString('logging.processname','simpleSAMLphp'); if (@file_exists($this->logFile)) { if (!@is_writeable($this->logFile)) throw new Exception("Could not write to logfile: ".$this->logFile); @@ -48,6 +49,12 @@ class SimpleSAML_Logger_LoggingHandlerFile implements SimpleSAML_Logger_LoggingH SimpleSAML_Utilities::initTimezone(); } + + function setLogFormat($format) { + $this->format = $format; + } + + function log_internal($level, $string) { if ($this->logFile != null) { @@ -56,9 +63,23 @@ class SimpleSAML_Logger_LoggingHandlerFile implements SimpleSAML_Logger_LoggingH $levelName = self::$levelNames[$level]; else $levelName = sprintf('UNKNOWN%d', $level); - - $line = sprintf("%s %s %s %s\n", strftime("%b %d %H:%M:%S"), $this->processname, $levelName, $string); - file_put_contents($this->logFile, $line, FILE_APPEND); + + $formats = array('%process', '%level'); + $replacements = array($this->processname, $levelName); + + $matches = array(); + if (preg_match('/%date(?:\{([^\}]+)\})?/', $this->format, $matches)) { + $format = "%b %d %H:%M:%S"; + if (isset($matches[1])) { + $format = $matches[1]; + } + + array_push($formats, $matches[0]); + array_push($replacements, strftime($format)); + } + + $string = str_replace($formats, $replacements, $string); + file_put_contents($this->logFile, $string . PHP_EOL, FILE_APPEND); } } } diff --git a/lib/SimpleSAML/Logger/LoggingHandlerSyslog.php b/lib/SimpleSAML/Logger/LoggingHandlerSyslog.php index 6c7ddfadd371af9c567706ee81d9c43ae0d7fbec..58fc183498520b619a8d7c3654641a8e763210a9 100644 --- a/lib/SimpleSAML/Logger/LoggingHandlerSyslog.php +++ b/lib/SimpleSAML/Logger/LoggingHandlerSyslog.php @@ -12,7 +12,7 @@ class SimpleSAML_Logger_LoggingHandlerSyslog implements SimpleSAML_Logger_LoggingHandler { private $isWindows = false; - + function __construct() { $config = SimpleSAML_Configuration::getInstance(); assert($config instanceof SimpleSAML_Configuration); @@ -31,6 +31,12 @@ class SimpleSAML_Logger_LoggingHandlerSyslog implements SimpleSAML_Logger_Loggin openlog($processname, LOG_PID, $facility); } + + function setLogFormat($format) { + $this->format = $format; + } + + function log_internal($level,$string) { /* * Changing log level to supported levels if OS is Windows @@ -41,7 +47,14 @@ class SimpleSAML_Logger_LoggingHandlerSyslog implements SimpleSAML_Logger_Loggin else $level = LOG_INFO; } - syslog($level,$level.' '.$string); + + $formats = array('%process', '%level'); + $replacements = array('', $level); + $string = str_replace($formats, $replacements, $string); + $string = preg_replace('/%\w+(\{[^\}]+\})?/', '', $string); + $string = trim($string); + + syslog($level, $string); } } ?> \ No newline at end of file