Syslog.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 $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(SystemConfig $config, ?string $tag = null) {
  39. parent::__construct($config);
  40. if ($tag === null) {
  41. $tag = $config->getValue('syslog_tag', 'Nextcloud');
  42. }
  43. openlog($tag, LOG_PID | LOG_CONS, LOG_USER);
  44. }
  45. public function __destruct() {
  46. closelog();
  47. }
  48. /**
  49. * write a message in the log
  50. * @param string $app
  51. * @param string|array $message
  52. * @param int $level
  53. */
  54. public function write(string $app, $message, int $level) {
  55. $syslog_level = $this->levels[$level];
  56. syslog($syslog_level, $this->logDetailsAsJSON($app, $message, $level));
  57. }
  58. }