AuditLogger.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\AdminAudit;
  7. use OCP\IConfig;
  8. use OCP\Log\ILogFactory;
  9. use Psr\Log\LoggerInterface;
  10. /**
  11. * Logger that logs in the audit log file instead of the normal log file
  12. */
  13. class AuditLogger implements IAuditLogger {
  14. private LoggerInterface $parentLogger;
  15. public function __construct(ILogFactory $logFactory, IConfig $config) {
  16. $auditType = $config->getSystemValueString('log_type_audit', 'file');
  17. $defaultTag = $config->getSystemValueString('syslog_tag', 'Nextcloud');
  18. $auditTag = $config->getSystemValueString('syslog_tag_audit', $defaultTag);
  19. $logFile = $config->getSystemValueString('logfile_audit', '');
  20. if ($auditType === 'file' && !$logFile) {
  21. $default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log';
  22. // Legacy way was appconfig, now it's paralleled with the normal log config
  23. $logFile = $config->getAppValue('admin_audit', 'logfile', $default);
  24. }
  25. $this->parentLogger = $logFactory->getCustomPsrLogger($logFile, $auditType, $auditTag);
  26. }
  27. public function emergency($message, array $context = []): void {
  28. $this->parentLogger->emergency($message, $context);
  29. }
  30. public function alert($message, array $context = []): void {
  31. $this->parentLogger->alert($message, $context);
  32. }
  33. public function critical($message, array $context = []): void {
  34. $this->parentLogger->critical($message, $context);
  35. }
  36. public function error($message, array $context = []): void {
  37. $this->parentLogger->error($message, $context);
  38. }
  39. public function warning($message, array $context = []): void {
  40. $this->parentLogger->warning($message, $context);
  41. }
  42. public function notice($message, array $context = []): void {
  43. $this->parentLogger->notice($message, $context);
  44. }
  45. public function info($message, array $context = []): void {
  46. $this->parentLogger->info($message, $context);
  47. }
  48. public function debug($message, array $context = []): void {
  49. $this->parentLogger->debug($message, $context);
  50. }
  51. public function log($level, $message, array $context = []): void {
  52. $this->parentLogger->log($level, $message, $context);
  53. }
  54. }