summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJaime Perez <jaime.perez@uninett.no>2014-06-17 16:18:08 +0200
committerJaime Perez <jaime.perez@uninett.no>2014-06-17 16:18:08 +0200
commit0e2f2ec441b25dfa3d5136e400b324dc54310f0b (patch)
tree393a7106b71cb00f5c36023f2a10ceec0a52f9ad
parent246f901962c22811e3adef88b9b9aab6ea4412f9 (diff)
downloadsimplesamlphp-0e2f2ec441b25dfa3d5136e400b324dc54310f0b.zip
simplesamlphp-0e2f2ec441b25dfa3d5136e400b324dc54310f0b.tar.gz
simplesamlphp-0e2f2ec441b25dfa3d5136e400b324dc54310f0b.tar.bz2
Add a new 'logging.format' option to control the formatting of the logs.
-rw-r--r--config-templates/config.php28
-rw-r--r--lib/SimpleSAML/Logger.php50
-rw-r--r--lib/SimpleSAML/Logger/LoggingHandlerErrorLog.php23
-rw-r--r--lib/SimpleSAML/Logger/LoggingHandlerFile.php33
-rw-r--r--lib/SimpleSAML/Logger/LoggingHandlerSyslog.php17
5 files changed, 134 insertions, 17 deletions
diff --git a/config-templates/config.php b/config-templates/config.php
index 21263d2..b82efd0 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 8a094f5..9ea5982 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 0a687ee..cd9404d 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 43ed5fb..cb5e2b1 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 6c7ddfa..58fc183 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