diff options
author | Joas Schilling <coding@schilljs.com> | 2022-01-19 16:15:14 +0100 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2022-01-19 16:15:14 +0100 |
commit | 4c41abd8169e78aa35bd6ea9bd9eda6d76817085 (patch) | |
tree | 44274debf7d77eeff07353dfa3a0e6b5e418f716 | |
parent | a7eefa293e4735ab9f249f9d12ced3e70823da35 (diff) | |
download | nextcloud-server-4c41abd8169e78aa35bd6ea9bd9eda6d76817085.tar.gz nextcloud-server-4c41abd8169e78aa35bd6ea9bd9eda6d76817085.zip |
Allow to get custom loggers for syslog, errorlog and systemdlog too
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | lib/private/Log/Errorlog.php | 9 | ||||
-rw-r--r-- | lib/private/Log/LogFactory.php | 20 | ||||
-rw-r--r-- | lib/private/Log/Syslog.php | 7 | ||||
-rw-r--r-- | lib/private/Log/Systemdlog.php | 7 | ||||
-rw-r--r-- | lib/public/Log/ILogFactory.php | 6 |
5 files changed, 40 insertions, 9 deletions
diff --git a/lib/private/Log/Errorlog.php b/lib/private/Log/Errorlog.php index ebcb73be4ce..d27759d7050 100644 --- a/lib/private/Log/Errorlog.php +++ b/lib/private/Log/Errorlog.php @@ -29,6 +29,13 @@ use OCP\Log\IWriter; class Errorlog implements IWriter { + /** @var string */ + protected $tag; + + public function __construct(string $tag = 'owncloud') { + $this->tag = $tag; + } + /** * write a message in the log * @param string $app @@ -36,6 +43,6 @@ class Errorlog implements IWriter { * @param int $level */ public function write(string $app, $message, int $level) { - error_log('[owncloud]['.$app.']['.$level.'] '.$message); + error_log('[' . $this->tag . ']['.$app.']['.$level.'] '.$message); } } diff --git a/lib/private/Log/LogFactory.php b/lib/private/Log/LogFactory.php index f0f804cd51c..807ff501e39 100644 --- a/lib/private/Log/LogFactory.php +++ b/lib/private/Log/LogFactory.php @@ -70,8 +70,24 @@ class LogFactory implements ILogFactory { return new Log($log, $this->systemConfig); } - public function getCustomPsrLogger(string $path): LoggerInterface { - $log = $this->buildLogFile($path); + protected function createNewLogger(string $type, string $tag, string $path): IWriter { + switch (strtolower($type)) { + case 'errorlog': + return new Errorlog($tag); + case 'syslog': + return new Syslog($this->systemConfig, $tag); + case 'systemd': + return new Systemdlog($this->systemConfig, $tag); + case 'file': + case 'owncloud': + case 'nextcloud': + default: + return $this->buildLogFile($path); + } + } + + public function getCustomPsrLogger(string $path, string $type = 'file', string $tag = 'Nextcloud'): LoggerInterface { + $log = $this->createNewLogger($type, $tag, $path); return new PsrLoggerAdapter( new Log($log, $this->systemConfig) ); diff --git a/lib/private/Log/Syslog.php b/lib/private/Log/Syslog.php index 7c3d1a54b78..8140b4ec77c 100644 --- a/lib/private/Log/Syslog.php +++ b/lib/private/Log/Syslog.php @@ -38,9 +38,12 @@ class Syslog extends LogDetails implements IWriter { ILogger::FATAL => LOG_CRIT, ]; - public function __construct(SystemConfig $config) { + public function __construct(SystemConfig $config, ?string $tag = null) { parent::__construct($config); - openlog($config->getValue('syslog_tag', 'Nextcloud'), LOG_PID | LOG_CONS, LOG_USER); + if ($tag === null) { + $tag = $config->getValue('syslog_tag', 'Nextcloud'); + } + openlog($tag, LOG_PID | LOG_CONS, LOG_USER); } public function __destruct() { diff --git a/lib/private/Log/Systemdlog.php b/lib/private/Log/Systemdlog.php index a01826c0b05..00f242e3718 100644 --- a/lib/private/Log/Systemdlog.php +++ b/lib/private/Log/Systemdlog.php @@ -56,14 +56,17 @@ class Systemdlog extends LogDetails implements IWriter { protected $syslogId; - public function __construct(SystemConfig $config) { + public function __construct(SystemConfig $config, ?string $tag = null) { parent::__construct($config); if (!function_exists('sd_journal_send')) { throw new HintException( 'PHP extension php-systemd is not available.', 'Please install and enable PHP extension systemd if you wish to log to the Systemd journal.'); } - $this->syslogId = $config->getValue('syslog_tag', 'Nextcloud'); + if ($tag === null) { + $tag = $config->getValue('syslog_tag', 'Nextcloud'); + } + $this->syslogId = $tag; } /** diff --git a/lib/public/Log/ILogFactory.php b/lib/public/Log/ILogFactory.php index 54ed761a8e7..6bbcf93bca8 100644 --- a/lib/public/Log/ILogFactory.php +++ b/lib/public/Log/ILogFactory.php @@ -51,8 +51,10 @@ interface ILogFactory { /** * @param string $path + * @param string $type + * @param string $tag * @return LoggerInterface - * @since 22.0.0 + * @since 22.0.0 - Parameters $type and $tag were added in 24.0.0 */ - public function getCustomPsrLogger(string $path): LoggerInterface; + public function getCustomPsrLogger(string $path, string $type = 'file', string $tag = 'Nextcloud'): LoggerInterface; } |