Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow to get custom loggers for syslog, errorlog and systemdlog too
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen authored and PVince81 committed Feb 16, 2022
commit 67a8d3f736cb500c2a285d4e9627030bd4e1a1e7
9 changes: 8 additions & 1 deletion lib/private/Log/Errorlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,20 @@

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
* @param string $message
* @param int $level
*/
public function write(string $app, $message, int $level) {
error_log('[owncloud]['.$app.']['.$level.'] '.$message);
error_log('[' . $this->tag . ']['.$app.']['.$level.'] '.$message);
}
}
20 changes: 18 additions & 2 deletions lib/private/Log/LogFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,24 @@ public function getCustomLogger(string $path):ILogger {
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)
);
Expand Down
7 changes: 5 additions & 2 deletions lib/private/Log/Syslog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 5 additions & 2 deletions lib/private/Log/Systemdlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions lib/public/Log/ILogFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ public function getCustomLogger(string $path): ILogger;

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