Allow to get custom loggers for syslog, errorlog and systemdlog too

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2022-01-19 16:15:14 +01:00 committed by Vincent Petry (Rebase PR Action)
parent f08ac1efcf
commit 67a8d3f736
5 changed files with 40 additions and 9 deletions

View file

@ -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);
}
}

View file

@ -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)
);

View file

@ -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() {

View file

@ -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;
}
/**

View file

@ -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;
}