1
0

AuditLogger.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2022 Carl Schwan <carl@carlschwan.eu>
  4. *
  5. * @author Carl Schwan <carl@carlschwan.eu>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\AdminAudit;
  24. use OCP\IConfig;
  25. use OCP\Log\ILogFactory;
  26. use Psr\Log\LoggerInterface;
  27. /**
  28. * Logger that logs in the audit log file instead of the normal log file
  29. */
  30. class AuditLogger implements IAuditLogger {
  31. private LoggerInterface $parentLogger;
  32. public function __construct(ILogFactory $logFactory, IConfig $config) {
  33. $auditType = $config->getSystemValueString('log_type_audit', 'file');
  34. $defaultTag = $config->getSystemValueString('syslog_tag', 'Nextcloud');
  35. $auditTag = $config->getSystemValueString('syslog_tag_audit', $defaultTag);
  36. $logFile = $config->getSystemValueString('logfile_audit', '');
  37. if ($auditType === 'file' && !$logFile) {
  38. $default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log';
  39. // Legacy way was appconfig, now it's paralleled with the normal log config
  40. $logFile = $config->getAppValue('admin_audit', 'logfile', $default);
  41. }
  42. $this->parentLogger = $logFactory->getCustomPsrLogger($logFile, $auditType, $auditTag);
  43. }
  44. public function emergency($message, array $context = []): void {
  45. $this->parentLogger->emergency($message, $context);
  46. }
  47. public function alert($message, array $context = []): void {
  48. $this->parentLogger->alert($message, $context);
  49. }
  50. public function critical($message, array $context = []): void {
  51. $this->parentLogger->critical($message, $context);
  52. }
  53. public function error($message, array $context = []): void {
  54. $this->parentLogger->error($message, $context);
  55. }
  56. public function warning($message, array $context = []): void {
  57. $this->parentLogger->warning($message, $context);
  58. }
  59. public function notice($message, array $context = []): void {
  60. $this->parentLogger->notice($message, $context);
  61. }
  62. public function info($message, array $context = []): void {
  63. $this->parentLogger->info($message, $context);
  64. }
  65. public function debug($message, array $context = []): void {
  66. $this->parentLogger->debug($message, $context);
  67. }
  68. public function log($level, $message, array $context = []): void {
  69. $this->parentLogger->log($level, $message, $context);
  70. }
  71. }