2018-06-06 16:40:06 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2018-06-06 16:40:06 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-06-06 16:40:06 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Log;
|
|
|
|
|
|
2019-07-16 04:15:00 -04:00
|
|
|
use OC\SystemConfig;
|
2021-06-29 19:20:33 -04:00
|
|
|
use OCP\HintException;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\ILogger;
|
2018-06-06 16:40:06 -04:00
|
|
|
use OCP\Log\IWriter;
|
|
|
|
|
|
|
|
|
|
// The following fields are understood by systemd/journald, see
|
|
|
|
|
// man systemd.journal-fields. All are optional:
|
|
|
|
|
// MESSAGE=
|
|
|
|
|
// The human-readable message string for this entry.
|
|
|
|
|
// MESSAGE_ID=
|
|
|
|
|
// A 128-bit message identifier ID
|
|
|
|
|
// PRIORITY=
|
|
|
|
|
// A priority value between 0 ("emerg") and 7 ("debug")
|
|
|
|
|
// CODE_FILE=, CODE_LINE=, CODE_FUNC=
|
|
|
|
|
// The code location generating this message, if known
|
|
|
|
|
// ERRNO=
|
|
|
|
|
// The low-level Unix error number causing this entry, if any.
|
|
|
|
|
// SYSLOG_FACILITY=, SYSLOG_IDENTIFIER=, SYSLOG_PID=
|
|
|
|
|
// Syslog compatibility fields
|
|
|
|
|
|
2019-07-16 04:15:00 -04:00
|
|
|
class Systemdlog extends LogDetails implements IWriter {
|
2023-06-28 01:53:43 -04:00
|
|
|
protected array $levels = [
|
2018-06-06 16:40:06 -04:00
|
|
|
ILogger::DEBUG => 7,
|
|
|
|
|
ILogger::INFO => 6,
|
|
|
|
|
ILogger::WARN => 4,
|
|
|
|
|
ILogger::ERROR => 3,
|
|
|
|
|
ILogger::FATAL => 2,
|
|
|
|
|
];
|
|
|
|
|
|
2023-06-28 01:53:43 -04:00
|
|
|
protected string $syslogId;
|
2018-06-18 14:29:31 -04:00
|
|
|
|
2023-06-28 01:53:43 -04:00
|
|
|
public function __construct(
|
|
|
|
|
SystemConfig $config,
|
|
|
|
|
?string $tag = null,
|
|
|
|
|
) {
|
2019-07-16 04:15:00 -04:00
|
|
|
parent::__construct($config);
|
2020-04-10 08:19:56 -04:00
|
|
|
if (!function_exists('sd_journal_send')) {
|
2018-06-18 14:29:31 -04:00
|
|
|
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.');
|
|
|
|
|
}
|
2022-01-19 10:15:14 -05:00
|
|
|
if ($tag === null) {
|
|
|
|
|
$tag = $config->getValue('syslog_tag', 'Nextcloud');
|
|
|
|
|
}
|
|
|
|
|
$this->syslogId = $tag;
|
2018-06-06 16:40:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-06-18 14:29:31 -04:00
|
|
|
* Write a message to the log.
|
2022-12-05 13:54:37 -05:00
|
|
|
* @param string|array $message
|
2018-06-06 16:40:06 -04:00
|
|
|
*/
|
2023-06-28 01:53:43 -04:00
|
|
|
public function write(string $app, $message, int $level): void {
|
2018-06-06 16:40:06 -04:00
|
|
|
$journal_level = $this->levels[$level];
|
2024-09-19 05:10:31 -04:00
|
|
|
sd_journal_send('PRIORITY=' . $journal_level,
|
|
|
|
|
'SYSLOG_IDENTIFIER=' . $this->syslogId,
|
2023-01-20 05:45:08 -05:00
|
|
|
'MESSAGE=' . $this->logDetailsAsJSON($app, $message, $level));
|
2018-06-06 16:40:06 -04:00
|
|
|
}
|
|
|
|
|
}
|