diff --git a/helfi_api_base.services.yml b/helfi_api_base.services.yml index 7afcd4c5..38fa3459 100644 --- a/helfi_api_base.services.yml +++ b/helfi_api_base.services.yml @@ -73,10 +73,15 @@ services: tags: - { name: event_subscriber } + helfi_api_base.logger_filesystem: + class: Symfony\Component\Filesystem\Filesystem + public: false + helfi_api_base.logger_json: class: Drupal\helfi_api_base\Logger\JsonLog arguments: - '@logger.log_message_parser' + - '@helfi_api_base.logger_filesystem' - '%helfi_api_base.json_logger_path%' - '%helfi_api_base.logger_enabled%' tags: diff --git a/src/Logger/JsonLog.php b/src/Logger/JsonLog.php index 23992776..667d67df 100644 --- a/src/Logger/JsonLog.php +++ b/src/Logger/JsonLog.php @@ -9,6 +9,7 @@ use Drupal\Core\Logger\RfcLogLevel; use Drupal\Core\StringTranslation\TranslatableMarkup; use Psr\Log\LoggerInterface; +use Symfony\Component\Filesystem\Filesystem; /** * This class allows logging to stdout and stderr. @@ -22,28 +23,27 @@ final class JsonLog implements LoggerInterface { * * @param \Drupal\Core\Logger\LogMessageParserInterface $parser * The parser to use when extracting message variables. + * @param \Symfony\Component\Filesystem\Filesystem $filesystem + * The file system. * @param string $stream * The output path. * @param bool $loggerEnabled * Whether logger is enabled or not. */ public function __construct( - private LogMessageParserInterface $parser, - private string $stream, - private bool $loggerEnabled = TRUE + private readonly LogMessageParserInterface $parser, + private readonly Filesystem $filesystem, + private readonly string $stream, + private readonly bool $loggerEnabled = TRUE ) { } /** - * Outputs the given entry. - * - * @param array $entry - * The log entry. + * Outputs the messages. */ - private function output(array $entry) : void { - $stream = fopen($this->stream, 'a'); - fwrite($stream, json_encode(['message' => $entry]) . PHP_EOL); - fclose($stream); + private function output(array $message) : void { + $this->filesystem + ->appendToFile($this->stream, json_encode(['message' => $message]) . PHP_EOL); } /**