ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
FileLoggingHandler.php
Go to the documentation of this file.
1<?php
2
3namespace SimpleSAML\Logger;
4
6
15{
16
22 protected $logFile = null;
23
28 private static $levelNames = array(
29 Logger::EMERG => 'EMERGENCY',
30 Logger::ALERT => 'ALERT',
31 Logger::CRIT => 'CRITICAL',
32 Logger::ERR => 'ERROR',
33 Logger::WARNING => 'WARNING',
34 Logger::NOTICE => 'NOTICE',
35 Logger::INFO => 'INFO',
36 Logger::DEBUG => 'DEBUG',
37 );
38 protected $processname = null;
39 protected $format;
40
41
46 {
47 // get the metadata handler option from the configuration
48 $this->logFile = $config->getPathValue('loggingdir', 'log/').
49 $config->getString('logging.logfile', 'simplesamlphp.log');
50 $this->processname = $config->getString('logging.processname', 'SimpleSAMLphp');
51
52 if (@file_exists($this->logFile)) {
53 if (!@is_writeable($this->logFile)) {
54 throw new \Exception("Could not write to logfile: ".$this->logFile);
55 }
56 } else {
57 if (!@touch($this->logFile)) {
58 throw new \Exception(
59 "Could not create logfile: ".$this->logFile.
60 " The logging directory is not writable for the web server user."
61 );
62 }
63 }
64
66 }
67
68
74 public function setLogFormat($format)
75 {
76 $this->format = $format;
77 }
78
79
86 public function log($level, $string)
87 {
88 if (!is_null($this->logFile)) {
89 // set human-readable log level. Copied from SimpleSAML\Logger\ErrorLogLoggingHandler.
90 $levelName = sprintf('UNKNOWN%d', $level);
91 if (array_key_exists($level, self::$levelNames)) {
92 $levelName = self::$levelNames[$level];
93 }
94
95 $formats = array('%process', '%level');
96 $replacements = array($this->processname, $levelName);
97
98 $matches = array();
99 if (preg_match('/%date(?:\{([^\}]+)\})?/', $this->format, $matches)) {
100 $format = "%b %d %H:%M:%S";
101 if (isset($matches[1])) {
102 $format = $matches[1];
103 }
104
105 array_push($formats, $matches[0]);
106 array_push($replacements, strftime($format));
107 }
108
109 $string = str_replace($formats, $replacements, $string);
110 file_put_contents($this->logFile, $string.PHP_EOL, FILE_APPEND);
111 }
112 }
113}
An exception for terminatinating execution or to throw for unit testing.
setLogFormat($format)
Set the format desired for the logs.
static $levelNames
This array contains the mappings from syslog log levels to names.
log($level, $string)
Log a message to the log file.
__construct(\SimpleSAML_Configuration $config)
Build a new logging handler based on files.
static initTimezone()
Initialize the timezone.
Definition: Time.php:51
$formats
Definition: date.php:77
$config
Definition: bootstrap.php:15