Syslog.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Log;
  8. use OC\SystemConfig;
  9. use OCP\ILogger;
  10. use OCP\Log\IWriter;
  11. class Syslog extends LogDetails implements IWriter {
  12. protected array $levels = [
  13. ILogger::DEBUG => LOG_DEBUG,
  14. ILogger::INFO => LOG_INFO,
  15. ILogger::WARN => LOG_WARNING,
  16. ILogger::ERROR => LOG_ERR,
  17. ILogger::FATAL => LOG_CRIT,
  18. ];
  19. public function __construct(
  20. SystemConfig $config,
  21. ?string $tag = null,
  22. ) {
  23. parent::__construct($config);
  24. if ($tag === null) {
  25. $tag = $config->getValue('syslog_tag', 'Nextcloud');
  26. }
  27. openlog($tag, LOG_PID | LOG_CONS, LOG_USER);
  28. }
  29. public function __destruct() {
  30. closelog();
  31. }
  32. /**
  33. * write a message in the log
  34. * @param string|array $message
  35. */
  36. public function write(string $app, $message, int $level): void {
  37. $syslog_level = $this->levels[$level];
  38. syslog($syslog_level, $this->logDetailsAsJSON($app, $message, $level));
  39. }
  40. }