Systemdlog.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 $levels = [
  47. ILogger::DEBUG => 7,
  48. ILogger::INFO => 6,
  49. ILogger::WARN => 4,
  50. ILogger::ERROR => 3,
  51. ILogger::FATAL => 2,
  52. ];
  53. protected $syslogId;
  54. public function __construct(SystemConfig $config, ?string $tag = null) {
  55. parent::__construct($config);
  56. if (!function_exists('sd_journal_send')) {
  57. throw new HintException(
  58. 'PHP extension php-systemd is not available.',
  59. 'Please install and enable PHP extension systemd if you wish to log to the Systemd journal.');
  60. }
  61. if ($tag === null) {
  62. $tag = $config->getValue('syslog_tag', 'Nextcloud');
  63. }
  64. $this->syslogId = $tag;
  65. }
  66. /**
  67. * Write a message to the log.
  68. * @param string $app
  69. * @param string|array $message
  70. * @param int $level
  71. */
  72. public function write(string $app, $message, int $level) {
  73. $journal_level = $this->levels[$level];
  74. sd_journal_send('PRIORITY='.$journal_level,
  75. 'SYSLOG_IDENTIFIER='.$this->syslogId,
  76. 'MESSAGE=' . $this->logDetailsAsJSON($app, $message, $level));
  77. }
  78. }