1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2018, Johannes Ernst |
4
|
|
|
* |
5
|
|
|
* @author Johannes Ernst <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This code is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
11
|
|
|
* as published by the Free Software Foundation. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace OC\Log; |
24
|
|
|
|
25
|
|
|
use OC\HintException; |
26
|
|
|
use OCP\ILogger; |
27
|
|
|
use OCP\IConfig; |
28
|
|
|
use OCP\Log\IWriter; |
29
|
|
|
|
30
|
|
|
// The following fields are understood by systemd/journald, see |
31
|
|
|
// man systemd.journal-fields. All are optional: |
32
|
|
|
// MESSAGE= |
33
|
|
|
// The human-readable message string for this entry. |
34
|
|
|
// MESSAGE_ID= |
35
|
|
|
// A 128-bit message identifier ID |
36
|
|
|
// PRIORITY= |
37
|
|
|
// A priority value between 0 ("emerg") and 7 ("debug") |
38
|
|
|
// CODE_FILE=, CODE_LINE=, CODE_FUNC= |
39
|
|
|
// The code location generating this message, if known |
40
|
|
|
// ERRNO= |
41
|
|
|
// The low-level Unix error number causing this entry, if any. |
42
|
|
|
// SYSLOG_FACILITY=, SYSLOG_IDENTIFIER=, SYSLOG_PID= |
43
|
|
|
// Syslog compatibility fields |
44
|
|
|
|
45
|
|
|
class Systemdlog implements IWriter { |
46
|
|
|
protected $levels = [ |
47
|
|
|
ILogger::DEBUG => 7, |
48
|
|
|
ILogger::INFO => 6, |
49
|
|
|
ILogger::WARN => 4, |
50
|
|
|
ILogger::ERROR => 3, |
51
|
|
|
ILogger::FATAL => 2, |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
protected $syslogId; |
55
|
|
|
|
56
|
|
|
public function __construct(IConfig $config) { |
57
|
|
|
if(!function_exists('sd_journal_send')) { |
58
|
|
|
throw new HintException( |
59
|
|
|
'PHP extension php-systemd is not available.', |
60
|
|
|
'Please install and enable PHP extension systemd if you wish to log to the Systemd journal.'); |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
$this->syslogId = $config->getSystemValue('syslog_tag', 'Nextcloud'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Write a message to the log. |
68
|
|
|
* @param string $app |
69
|
|
|
* @param string $message |
70
|
|
|
* @param int $level |
71
|
|
|
*/ |
72
|
|
|
public function write(string $app, $message, int $level) { |
73
|
|
|
$journal_level = $this->levels[$level]; |
74
|
|
|
sd_journal_send('PRIORITY='.$journal_level, |
75
|
|
|
'SYSLOG_IDENTIFIER='.$this->syslogId, |
76
|
|
|
'MESSAGE={'.$app.'} '.$message); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|