Syslog.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  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, version 3,
  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\ILogger;
  29. use OCP\Log\IWriter;
  30. class Syslog extends LogDetails implements IWriter {
  31. protected array $levels = [
  32. ILogger::DEBUG => LOG_DEBUG,
  33. ILogger::INFO => LOG_INFO,
  34. ILogger::WARN => LOG_WARNING,
  35. ILogger::ERROR => LOG_ERR,
  36. ILogger::FATAL => LOG_CRIT,
  37. ];
  38. public function __construct(
  39. SystemConfig $config,
  40. ?string $tag = null,
  41. ) {
  42. parent::__construct($config);
  43. if ($tag === null) {
  44. $tag = $config->getValue('syslog_tag', 'Nextcloud');
  45. }
  46. openlog($tag, LOG_PID | LOG_CONS, LOG_USER);
  47. }
  48. public function __destruct() {
  49. closelog();
  50. }
  51. /**
  52. * write a message in the log
  53. * @param string|array $message
  54. */
  55. public function write(string $app, $message, int $level): void {
  56. $syslog_level = $this->levels[$level];
  57. syslog($syslog_level, $this->logDetailsAsJSON($app, $message, $level));
  58. }
  59. }