AuditLogger.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. /** @var LoggerInterface */
  32. private $parentLogger;
  33. public function __construct(ILogFactory $logFactory, IConfig $config) {
  34. $auditType = $config->getSystemValueString('log_type_audit', 'file');
  35. $defaultTag = $config->getSystemValueString('syslog_tag', 'Nextcloud');
  36. $auditTag = $config->getSystemValueString('syslog_tag_audit', $defaultTag);
  37. $logFile = $config->getSystemValueString('logfile_audit', '');
  38. if ($auditType === 'file' && !$logFile) {
  39. $default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log';
  40. // Legacy way was appconfig, now it's paralleled with the normal log config
  41. $logFile = $config->getAppValue('admin_audit', 'logfile', $default);
  42. }
  43. $this->parentLogger = $logFactory->getCustomPsrLogger($logFile, $auditType, $auditTag);
  44. }
  45. public function emergency($message, array $context = array()) {
  46. $this->parentLogger->emergency($message, $context);
  47. }
  48. public function alert($message, array $context = array()) {
  49. $this->parentLogger->alert($message, $context);
  50. }
  51. public function critical($message, array $context = array()) {
  52. $this->parentLogger->critical($message, $context);
  53. }
  54. public function error($message, array $context = array()) {
  55. $this->parentLogger->error($message, $context);
  56. }
  57. public function warning($message, array $context = array()) {
  58. $this->parentLogger->warning($message, $context);
  59. }
  60. public function notice($message, array $context = array()) {
  61. $this->parentLogger->notice($message, $context);
  62. }
  63. public function info($message, array $context = array()) {
  64. $this->parentLogger->info($message, $context);
  65. }
  66. public function debug($message, array $context = array()) {
  67. $this->parentLogger->debug($message, $context);
  68. }
  69. public function log($level, $message, array $context = array()) {
  70. $this->parentLogger->log($level, $message, $context);
  71. }
  72. }