You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.6 KiB

3 years ago
  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\HintException;
  28. use OC\SystemConfig;
  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) {
  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. $this->syslogId = $config->getValue('syslog_tag', 'Nextcloud');
  62. }
  63. /**
  64. * Write a message to the log.
  65. * @param string $app
  66. * @param string $message
  67. * @param int $level
  68. */
  69. public function write(string $app, $message, int $level) {
  70. $journal_level = $this->levels[$level];
  71. sd_journal_send('PRIORITY='.$journal_level,
  72. 'SYSLOG_IDENTIFIER='.$this->syslogId,
  73. 'MESSAGE=' . $this->logDetailsAsJSON($app, $message, $level));
  74. }
  75. }