Systemdlog.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018, Johannes Ernst
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Johannes Ernst <jernst@indiecomputing.com>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Log;
  27. use OC\SystemConfig;
  28. use OCP\HintException;
  29. use OCP\ILogger;
  30. use OCP\Log\IWriter;
  31. // The following fields are understood by systemd/journald, see
  32. // man systemd.journal-fields. All are optional:
  33. // MESSAGE=
  34. // The human-readable message string for this entry.
  35. // MESSAGE_ID=
  36. // A 128-bit message identifier ID
  37. // PRIORITY=
  38. // A priority value between 0 ("emerg") and 7 ("debug")
  39. // CODE_FILE=, CODE_LINE=, CODE_FUNC=
  40. // The code location generating this message, if known
  41. // ERRNO=
  42. // The low-level Unix error number causing this entry, if any.
  43. // SYSLOG_FACILITY=, SYSLOG_IDENTIFIER=, SYSLOG_PID=
  44. // Syslog compatibility fields
  45. class Systemdlog extends LogDetails implements IWriter {
  46. protected array $levels = [
  47. ILogger::DEBUG => 7,
  48. ILogger::INFO => 6,
  49. ILogger::WARN => 4,
  50. ILogger::ERROR => 3,
  51. ILogger::FATAL => 2,
  52. ];
  53. protected string $syslogId;
  54. public function __construct(
  55. SystemConfig $config,
  56. ?string $tag = null,
  57. ) {
  58. parent::__construct($config);
  59. if (!function_exists('sd_journal_send')) {
  60. throw new HintException(
  61. 'PHP extension php-systemd is not available.',
  62. 'Please install and enable PHP extension systemd if you wish to log to the Systemd journal.');
  63. }
  64. if ($tag === null) {
  65. $tag = $config->getValue('syslog_tag', 'Nextcloud');
  66. }
  67. $this->syslogId = $tag;
  68. }
  69. /**
  70. * Write a message to the log.
  71. * @param string|array $message
  72. */
  73. public function write(string $app, $message, int $level): void {
  74. $journal_level = $this->levels[$level];
  75. sd_journal_send('PRIORITY='.$journal_level,
  76. 'SYSLOG_IDENTIFIER='.$this->syslogId,
  77. 'MESSAGE=' . $this->logDetailsAsJSON($app, $message, $level));
  78. }
  79. }