diff options
author | Jaime Perez <jaime.perez@uninett.no> | 2014-06-17 16:18:08 +0200 |
---|---|---|
committer | Jaime Perez <jaime.perez@uninett.no> | 2014-06-17 16:18:08 +0200 |
commit | 0e2f2ec441b25dfa3d5136e400b324dc54310f0b (patch) | |
tree | 393a7106b71cb00f5c36023f2a10ceec0a52f9ad /lib/SimpleSAML/Logger/LoggingHandlerFile.php | |
parent | 246f901962c22811e3adef88b9b9aab6ea4412f9 (diff) | |
download | simplesamlphp-0e2f2ec441b25dfa3d5136e400b324dc54310f0b.zip simplesamlphp-0e2f2ec441b25dfa3d5136e400b324dc54310f0b.tar.gz simplesamlphp-0e2f2ec441b25dfa3d5136e400b324dc54310f0b.tar.bz2 |
Add a new 'logging.format' option to control the formatting of the logs.
Diffstat (limited to 'lib/SimpleSAML/Logger/LoggingHandlerFile.php')
-rw-r--r-- | lib/SimpleSAML/Logger/LoggingHandlerFile.php | 33 |
1 files changed, 27 insertions, 6 deletions
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); } } } |