1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace OC\Log;
- use OC\SystemConfig;
- use OCP\HintException;
- use OCP\ILogger;
- use OCP\Log\IWriter;
- class Systemdlog extends LogDetails implements IWriter {
- protected array $levels = [
- ILogger::DEBUG => 7,
- ILogger::INFO => 6,
- ILogger::WARN => 4,
- ILogger::ERROR => 3,
- ILogger::FATAL => 2,
- ];
- protected string $syslogId;
- 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.');
- }
- if ($tag === null) {
- $tag = $config->getValue('syslog_tag', 'Nextcloud');
- }
- $this->syslogId = $tag;
- }
-
- public function write(string $app, $message, int $level): void {
- $journal_level = $this->levels[$level];
- sd_journal_send('PRIORITY='.$journal_level,
- 'SYSLOG_IDENTIFIER='.$this->syslogId,
- 'MESSAGE=' . $this->logDetailsAsJSON($app, $message, $level));
- }
- }
|