Skip to content
Snippets Groups Projects
Commit 185eeb71 authored by Tim van Dijen's avatar Tim van Dijen
Browse files

Rewrite FileLoggingHandler using symfony/filesystem

parent 004d7021
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,9 @@ namespace SimpleSAML\Logger; ...@@ -7,6 +7,9 @@ namespace SimpleSAML\Logger;
use SimpleSAML\Configuration; use SimpleSAML\Configuration;
use SimpleSAML\Logger; use SimpleSAML\Logger;
use SimpleSAML\Utils; use SimpleSAML\Utils;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\Exception\CannotWriteFileException;
use Symfony\Component\HttpFoundation\File\File;
/** /**
* A logging handler that dumps logs to files. * A logging handler that dumps logs to files.
...@@ -45,6 +48,11 @@ class FileLoggingHandler implements LoggingHandlerInterface ...@@ -45,6 +48,11 @@ class FileLoggingHandler implements LoggingHandlerInterface
/** @var string */ /** @var string */
protected string $format = "%b %d %H:%M:%S"; protected string $format = "%b %d %H:%M:%S";
/**
* @var \Symfony\Component\Filesystem\Filesystem;
*/
protected Filesystem $fileSystem;
/** /**
* Build a new logging handler based on files. * Build a new logging handler based on files.
...@@ -52,6 +60,8 @@ class FileLoggingHandler implements LoggingHandlerInterface ...@@ -52,6 +60,8 @@ class FileLoggingHandler implements LoggingHandlerInterface
*/ */
public function __construct(Configuration $config) public function __construct(Configuration $config)
{ {
$this->fileSystem = new Filesystem();
// get the metadata handler option from the configuration // get the metadata handler option from the configuration
$this->logFile = $config->getPathValue('loggingdir', 'log/') . $this->logFile = $config->getPathValue('loggingdir', 'log/') .
$config->getOptionalString('logging.logfile', 'simplesamlphp.log'); $config->getOptionalString('logging.logfile', 'simplesamlphp.log');
...@@ -63,17 +73,19 @@ class FileLoggingHandler implements LoggingHandlerInterface ...@@ -63,17 +73,19 @@ class FileLoggingHandler implements LoggingHandlerInterface
$config->getOptionalString('logging.processname', 'SimpleSAMLphp') $config->getOptionalString('logging.processname', 'SimpleSAMLphp')
); );
if (@file_exists($this->logFile)) { $file = new File($this->logFile);
if (!@is_writeable($this->logFile)) { // Suppress E_WARNING if not exists
throw new \Exception("Could not write to logfile: " . $this->logFile); if (@$this->fileSystem->exists($this->logFile)) {
} if (!$file->isWritable()) {
} else { throw new CannotWriteFileException(
if (!@touch($this->logFile)) { sprintf("Could not write to logfile: %s", $this->logFile),
throw new \Exception(
"Could not create logfile: " . $this->logFile .
" The logging directory is not writable for the web server user."
); );
} }
} elseif (!$this->fileSystem->touch($this->logFile)) {
throw new CannotWriteFileException(sprintf(
"The logging directory is not writable for the web server user. Could not create logfile: %s",
$this->logFile,
));
} }
$timeUtils = new Utils\Time(); $timeUtils = new Utils\Time();
...@@ -121,8 +133,11 @@ class FileLoggingHandler implements LoggingHandlerInterface ...@@ -121,8 +133,11 @@ class FileLoggingHandler implements LoggingHandlerInterface
array_push($replacements, date($format)); array_push($replacements, date($format));
} }
$string = str_replace($formats, $replacements, $string); $this->fileSystem->appendToFile(
file_put_contents($this->logFile, $string . \PHP_EOL, FILE_APPEND); $this->logFile,
str_replace($formats, $replacements, $string) . \PHP_EOL,
false,
);
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment