summaryrefslogtreecommitdiffstats
path: root/lib/SimpleSAML/Logger/LoggingHandlerFile.php
diff options
context:
space:
mode:
authorJaime Perez <jaime.perez@uninett.no>2014-06-17 17:04:07 +0200
committerJaime Perez <jaime.perez@uninett.no>2014-06-17 17:04:07 +0200
commit6b8b5b255613ff88af5aa0eafa48d8e0f16d9155 (patch)
tree2f78e4258dd4774cfb9f8d283071425ec1659b12 /lib/SimpleSAML/Logger/LoggingHandlerFile.php
parent0e2f2ec441b25dfa3d5136e400b324dc54310f0b (diff)
downloadsimplesamlphp-6b8b5b255613ff88af5aa0eafa48d8e0f16d9155.zip
simplesamlphp-6b8b5b255613ff88af5aa0eafa48d8e0f16d9155.tar.gz
simplesamlphp-6b8b5b255613ff88af5aa0eafa48d8e0f16d9155.tar.bz2
Reformat the logging classes. Extract the LoggingHandler to its own file. Add proper documentation. Fix typos.
Diffstat (limited to 'lib/SimpleSAML/Logger/LoggingHandlerFile.php')
-rw-r--r--lib/SimpleSAML/Logger/LoggingHandlerFile.php104
1 files changed, 62 insertions, 42 deletions
diff --git a/lib/SimpleSAML/Logger/LoggingHandlerFile.php b/lib/SimpleSAML/Logger/LoggingHandlerFile.php
index cb5e2b1..9ed795e 100644
--- a/lib/SimpleSAML/Logger/LoggingHandlerFile.php
+++ b/lib/SimpleSAML/Logger/LoggingHandlerFile.php
@@ -9,60 +9,82 @@
* @version $ID$
*/
-class SimpleSAML_Logger_LoggingHandlerFile implements SimpleSAML_Logger_LoggingHandler {
-
- /**
- * This array contains the mappings from syslog loglevel to names. Copied
- * more or less directly from SimpleSAML_Logger_LoggingHandlerErrorLog.
- */
- private static $levelNames = array(
- SimpleSAML_Logger::EMERG => 'EMERGENCY',
- SimpleSAML_Logger::ALERT => 'ALERT',
- SimpleSAML_Logger::CRIT => 'CRITICAL',
- SimpleSAML_Logger::ERR => 'ERROR',
- SimpleSAML_Logger::WARNING => 'WARNING',
- SimpleSAML_Logger::NOTICE => 'NOTICE',
- SimpleSAML_Logger::INFO => 'INFO',
- SimpleSAML_Logger::DEBUG => 'DEBUG',
- );
-
- private $logFile = null;
- private $processname = null;
+class SimpleSAML_Logger_LoggingHandlerFile implements SimpleSAML_Logger_LoggingHandler
+{
+ /**
+ * This array contains the mappings from syslog loglevel to names. Copied
+ * more or less directly from SimpleSAML_Logger_LoggingHandlerErrorLog.
+ */
+ private static $levelNames = array(
+ SimpleSAML_Logger::EMERG => 'EMERGENCY',
+ SimpleSAML_Logger::ALERT => 'ALERT',
+ SimpleSAML_Logger::CRIT => 'CRITICAL',
+ SimpleSAML_Logger::ERR => 'ERROR',
+ SimpleSAML_Logger::WARNING => 'WARNING',
+ SimpleSAML_Logger::NOTICE => 'NOTICE',
+ SimpleSAML_Logger::INFO => 'INFO',
+ SimpleSAML_Logger::DEBUG => 'DEBUG',
+ );
+ private $logFile = NULL;
+ private $processname = NULL;
private $format;
- function __construct() {
+
+ /**
+ * Build a new logging handler based on files.
+ */
+ public function __construct()
+ {
$config = SimpleSAML_Configuration::getInstance();
assert($config instanceof SimpleSAML_Configuration);
- /* 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');
-
+ // 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');
+
if (@file_exists($this->logFile)) {
- if (!@is_writeable($this->logFile)) throw new Exception("Could not write to logfile: ".$this->logFile);
- }
- else
- {
- if (!@touch($this->logFile)) throw new Exception("Could not create logfile: ".$this->logFile." Loggingdir is not writeable for the webserver user.");
+ if (!@is_writeable($this->logFile)) {
+ throw new Exception("Could not write to logfile: " . $this->logFile);
+ }
+ } else {
+ if (!@touch($this->logFile)) {
+ throw new Exception(
+ "Could not create logfile: " . $this->logFile .
+ " Loggingdir is not writeable for the webserver user."
+ );
+ }
}
- SimpleSAML_Utilities::initTimezone();
+ SimpleSAML_Utilities::initTimezone();
}
- function setLogFormat($format) {
+ /**
+ * Set the format desired for the logs.
+ *
+ * @param string $format The format used for logs.
+ */
+ public function setLogFormat($format)
+ {
$this->format = $format;
}
- function log_internal($level, $string) {
- if ($this->logFile != null) {
-
- // Set human-readable log level. Copied from SimpleSAML_Logger_LoggingHandlerErrorLog.
- if(array_key_exists($level, self::$levelNames))
- $levelName = self::$levelNames[$level];
- else
- $levelName = sprintf('UNKNOWN%d', $level);
+ /**
+ * Log a message to the log file.
+ *
+ * @param int $level The log level.
+ * @param string $string The formatted message to log.
+ */
+ public function log($level, $string)
+ {
+ if ($this->logFile != NULL) {
+ // set human-readable log level. Copied from SimpleSAML_Logger_LoggingHandlerErrorLog.
+ $levelName = sprintf('UNKNOWN%d', $level);
+ if (array_key_exists($level, self::$levelNames)) {
+ $levelName = self::$levelNames[$level];
+ }
$formats = array('%process', '%level');
$replacements = array($this->processname, $levelName);
@@ -79,9 +101,7 @@ class SimpleSAML_Logger_LoggingHandlerFile implements SimpleSAML_Logger_LoggingH
}
$string = str_replace($formats, $replacements, $string);
- file_put_contents($this->logFile, $string . PHP_EOL, FILE_APPEND);
+ file_put_contents($this->logFile, $string.PHP_EOL, FILE_APPEND);
}
}
}
-
-?> \ No newline at end of file