Skip to content

Commit 272f4cd

Browse files
authored
Merge pull request #30763 from nextcloud/feature/noid/allow-to-log-audit-to-syslog
2 parents 562c573 + 497c584 commit 272f4cd

File tree

7 files changed

+77
-17
lines changed

7 files changed

+77
-17
lines changed

apps/admin_audit/lib/AppInfo/Application.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,19 @@ public function boot(IBootContext $context): void {
9696
}
9797

9898
private function getLogger(IConfig $config,
99-
LoggerInterface $logger,
10099
ILogFactory $logFactory): LoggerInterface {
101-
$default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log';
102-
$logFile = $config->getAppValue('admin_audit', 'logfile', $default);
103-
104-
if ($logFile === null) {
105-
return $logger;
100+
$auditType = $config->getSystemValueString('log_type_audit', 'file');
101+
$defaultTag = $config->getSystemValueString('syslog_tag', 'Nextcloud');
102+
$auditTag = $config->getSystemValueString('syslog_tag_audit', $defaultTag);
103+
$logFile = $config->getSystemValueString('logfile_audit', '');
104+
105+
if ($auditType === 'file' && !$logFile) {
106+
$default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log';
107+
// Legacy way was appconfig, now it's paralleled with the normal log config
108+
$logFile = $config->getAppValue('admin_audit', 'logfile', $default);
106109
}
107-
return $logFactory->getCustomPsrLogger($logFile);
110+
111+
return $logFactory->getCustomPsrLogger($logFile, $auditType, $auditTag);
108112
}
109113

110114
/**

config/config.sample.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,13 @@
846846
*/
847847
'log_type' => 'file',
848848

849+
/**
850+
* This parameter determines where the audit logs are sent. See ``log_type`` for more information.
851+
*
852+
* Defaults to ``file``
853+
*/
854+
'log_type_audit' => 'file',
855+
849856
/**
850857
* Name of the file to which the Nextcloud logs are written if parameter
851858
* ``log_type`` is set to ``file``.
@@ -855,7 +862,15 @@
855862
'logfile' => '/var/log/nextcloud.log',
856863

857864
/**
858-
* Log file mode for the Nextcloud loggin type in octal notation.
865+
* Name of the file to which the audit logs are written if parameter
866+
* ``log_type`` is set to ``file``.
867+
*
868+
* Defaults to ``[datadirectory]/audit.log``
869+
*/
870+
'logfile_audit' => '/var/log/audit.log',
871+
872+
/**
873+
* Log file mode for the Nextcloud logging type in octal notation.
859874
*
860875
* Defaults to 0640 (writeable by user, readable by group).
861876
*/
@@ -879,6 +894,16 @@
879894
*/
880895
'syslog_tag' => 'Nextcloud',
881896

897+
/**
898+
* If you maintain different instances and aggregate the logs, you may want
899+
* to distinguish between them. ``syslog_tag_audit`` can be set per instance
900+
* with a unique id. Only available if ``log_type`` is set to ``syslog`` or
901+
* ``systemd``.
902+
*
903+
* The default value is the value of ``syslog_tag``.
904+
*/
905+
'syslog_tag_audit' => 'Nextcloud',
906+
882907
/**
883908
* Log condition for log level increase based on conditions. Once one of these
884909
* conditions is met, the required log level is set to debug. This allows to

lib/private/Log/Errorlog.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,20 @@
2929

3030
class Errorlog implements IWriter {
3131

32+
/** @var string */
33+
protected $tag;
34+
35+
public function __construct(string $tag = 'owncloud') {
36+
$this->tag = $tag;
37+
}
38+
3239
/**
3340
* write a message in the log
3441
* @param string $app
3542
* @param string $message
3643
* @param int $level
3744
*/
3845
public function write(string $app, $message, int $level) {
39-
error_log('[owncloud]['.$app.']['.$level.'] '.$message);
46+
error_log('[' . $this->tag . ']['.$app.']['.$level.'] '.$message);
4047
}
4148
}

lib/private/Log/LogFactory.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,24 @@ public function getCustomLogger(string $path):ILogger {
7070
return new Log($log, $this->systemConfig);
7171
}
7272

73-
public function getCustomPsrLogger(string $path): LoggerInterface {
74-
$log = $this->buildLogFile($path);
73+
protected function createNewLogger(string $type, string $tag, string $path): IWriter {
74+
switch (strtolower($type)) {
75+
case 'errorlog':
76+
return new Errorlog($tag);
77+
case 'syslog':
78+
return new Syslog($this->systemConfig, $tag);
79+
case 'systemd':
80+
return new Systemdlog($this->systemConfig, $tag);
81+
case 'file':
82+
case 'owncloud':
83+
case 'nextcloud':
84+
default:
85+
return $this->buildLogFile($path);
86+
}
87+
}
88+
89+
public function getCustomPsrLogger(string $path, string $type = 'file', string $tag = 'Nextcloud'): LoggerInterface {
90+
$log = $this->createNewLogger($type, $tag, $path);
7591
return new PsrLoggerAdapter(
7692
new Log($log, $this->systemConfig)
7793
);

lib/private/Log/Syslog.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ class Syslog extends LogDetails implements IWriter {
3838
ILogger::FATAL => LOG_CRIT,
3939
];
4040

41-
public function __construct(SystemConfig $config) {
41+
public function __construct(SystemConfig $config, ?string $tag = null) {
4242
parent::__construct($config);
43-
openlog($config->getValue('syslog_tag', 'Nextcloud'), LOG_PID | LOG_CONS, LOG_USER);
43+
if ($tag === null) {
44+
$tag = $config->getValue('syslog_tag', 'Nextcloud');
45+
}
46+
openlog($tag, LOG_PID | LOG_CONS, LOG_USER);
4447
}
4548

4649
public function __destruct() {

lib/private/Log/Systemdlog.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,17 @@ class Systemdlog extends LogDetails implements IWriter {
5656

5757
protected $syslogId;
5858

59-
public function __construct(SystemConfig $config) {
59+
public function __construct(SystemConfig $config, ?string $tag = null) {
6060
parent::__construct($config);
6161
if (!function_exists('sd_journal_send')) {
6262
throw new HintException(
6363
'PHP extension php-systemd is not available.',
6464
'Please install and enable PHP extension systemd if you wish to log to the Systemd journal.');
6565
}
66-
$this->syslogId = $config->getValue('syslog_tag', 'Nextcloud');
66+
if ($tag === null) {
67+
$tag = $config->getValue('syslog_tag', 'Nextcloud');
68+
}
69+
$this->syslogId = $tag;
6770
}
6871

6972
/**

lib/public/Log/ILogFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ public function getCustomLogger(string $path): ILogger;
5151

5252
/**
5353
* @param string $path
54+
* @param string $type
55+
* @param string $tag
5456
* @return LoggerInterface
55-
* @since 22.0.0
57+
* @since 22.0.0 - Parameters $type and $tag were added in 24.0.0
5658
*/
57-
public function getCustomPsrLogger(string $path): LoggerInterface;
59+
public function getCustomPsrLogger(string $path, string $type = 'file', string $tag = 'Nextcloud'): LoggerInterface;
5860
}

0 commit comments

Comments
 (0)