1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2016 SURFnet. |
4
|
|
|
* |
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
8
|
|
|
* License, or (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU Affero General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace fkooman\RemoteStorage; |
20
|
|
|
|
21
|
|
|
use Psr\Log\AbstractLogger; |
22
|
|
|
use Psr\Log\LogLevel; |
23
|
|
|
use RuntimeException; |
24
|
|
|
|
25
|
|
|
class Logger extends AbstractLogger |
26
|
|
|
{ |
27
|
|
|
public function __construct($ident) |
28
|
|
|
{ |
29
|
|
|
if (false === openlog($ident, LOG_PERROR | LOG_ODELAY, LOG_USER)) { |
30
|
|
|
throw new RuntimeException('unable to open syslog'); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function __destruct() |
35
|
|
|
{ |
36
|
|
|
if (false === closelog()) { |
37
|
|
|
throw new RuntimeException('unable to close syslog'); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Logs with an arbitrary level. |
43
|
|
|
* |
44
|
|
|
* @param mixed $level |
45
|
|
|
* @param string $message |
46
|
|
|
* @param array $context |
47
|
|
|
*/ |
48
|
|
|
public function log($level, $message, array $context = []) |
49
|
|
|
{ |
50
|
|
|
// convert level to syslog level |
51
|
|
|
$syslogPriority = self::levelToPriority($level); |
52
|
|
|
|
53
|
|
|
syslog( |
54
|
|
|
$syslogPriority, |
55
|
|
|
$message |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private static function levelToPriority($level) |
60
|
|
|
{ |
61
|
|
|
switch ($level) { |
62
|
|
|
case LogLevel::EMERGENCY: |
63
|
|
|
return LOG_EMERG; |
64
|
|
|
case LogLevel::ALERT: |
65
|
|
|
return LOG_ALERT; |
66
|
|
|
case LogLevel::CRITICAL: |
67
|
|
|
return LOG_CRIT; |
68
|
|
|
case LogLevel::ERROR: |
69
|
|
|
return LOG_ERR; |
70
|
|
|
case LogLevel::WARNING: |
71
|
|
|
return LOG_WARNING; |
72
|
|
|
case LogLevel::NOTICE: |
73
|
|
|
return LOG_NOTICE; |
74
|
|
|
case LogLevel::INFO: |
75
|
|
|
return LOG_INFO; |
76
|
|
|
case LogLevel::DEBUG: |
77
|
|
|
return LOG_DEBUG; |
78
|
|
|
default: |
79
|
|
|
throw new RuntimeException('unknown log level'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|