1
0

AuditLogger.php 2.2 KB

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