]> git.mxchange.org Git - friendica.git/blob - src/Util/Logger/SyslogLogger.php
Adding SyslogLogger
[friendica.git] / src / Util / Logger / SyslogLogger.php
1 <?php
2
3 namespace Friendica\Util\Logger;
4
5 use Friendica\Network\HTTPException\InternalServerErrorException;
6 use Psr\Log\InvalidArgumentException;
7 use Psr\Log\LoggerInterface;
8 use Psr\Log\LogLevel;
9
10 /**
11  * A Logger instance for syslogging (fast, but simple)
12  * @see http://php.net/manual/en/function.syslog.php
13  */
14 class SyslogLogger implements LoggerInterface
15 {
16         /**
17          * Translates LogLevel log levels to syslog log priorities.
18          */
19         private $logLevels = [
20                 LogLevel::DEBUG     => LOG_DEBUG,
21                 LogLevel::INFO      => LOG_INFO,
22                 LogLevel::NOTICE    => LOG_NOTICE,
23                 LogLevel::WARNING   => LOG_WARNING,
24                 LogLevel::ERROR     => LOG_ERR,
25                 LogLevel::CRITICAL  => LOG_CRIT,
26                 LogLevel::ALERT     => LOG_ALERT,
27                 LogLevel::EMERGENCY => LOG_EMERG,
28         ];
29
30         /**
31          * The standard ident of the syslog (added to each message)
32          * @var string
33          */
34         private $ident;
35
36         /**
37          * Indicates what logging options will be used when generating a log message
38          * @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
39          *
40          * @var int
41          */
42         private $logOpts;
43
44         /**
45          * Used to specify what type of program is logging the message
46          * @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
47          *
48          * @var int
49          */
50         private $logFacility;
51
52         /**
53          * The minimum loglevel at which this logger will be triggered
54          * @var int
55          */
56         private $logLevel;
57
58         /**
59          * The Introspector for the current call
60          * @var Introspection
61          */
62         private $introspection;
63
64         /**
65          * @param string $channel     The channel (Syslog ident)
66          * @param string $level    The minimum loglevel at which this logger will be triggered
67          * @param int    $logOpts     Indicates what logging options will be used when generating a log message
68          * @param int    $logFacility Used to specify what type of program is logging the message
69          *
70          * @throws InternalServerErrorException if the loglevel isn't valid
71          */
72         public function __construct($channel, Introspection $introspection, $level = LogLevel::NOTICE, $logOpts = LOG_PID, $logFacility = LOG_USER)
73         {
74                 $this->ident = $channel;
75                 $this->logOpts = $logOpts;
76                 $this->logFacility = $logFacility;
77                 $this->logLevel = $this->mapLevelToPriority($level);
78                 $this->introspection = $introspection;
79         }
80
81         /**
82          * Maps the LogLevel (@see LogLevel ) to a SysLog priority (@see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters )
83          *
84          * @param string $level A LogLevel
85          *
86          * @return int The SysLog priority
87          *
88          * @throws \Psr\Log\InvalidArgumentException If the loglevel isn't valid
89          */
90         public function mapLevelToPriority($level)
91         {
92                 if (!array_key_exists($level, $this->logLevels)) {
93                         throw new InvalidArgumentException('LogLevel \'' . $level . '\' isn\'t valid.');
94                 }
95
96                 return $this->logLevels[$level];
97         }
98
99         /**
100          * Writes a message to the syslog
101          *
102          * @param int    $priority The Priority ( @see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters )
103          * @param string $message The message of the log
104          * @throws InternalServerErrorException if syslog cannot be used
105          */
106         private function write($priority, $message)
107         {
108                 if (!openlog($this->ident, $this->logOpts, $this->logFacility)) {
109                         throw new InternalServerErrorException('Can\'t open syslog for ident "' . $this->ident . '" and facility "' . $this->logFacility . '""');
110                 }
111
112                 syslog($priority, $message);
113         }
114
115         public function close()
116         {
117                 closelog();
118         }
119
120         private function formatLog($level, $message, $context = [])
121         {
122                 $logMessage  = '';
123
124                 $logMessage .= $this->ident . ' ';
125                 $logMessage .= '[' . $level . ']: ';
126                 $logMessage .= $message . ' ';
127                 $logMessage .= json_encode($context) . ' - ';
128                 $logMessage .= json_encode($this->introspection->getRecord());
129
130                 return $logMessage;
131         }
132
133         private function addEntry($level, $message, $context = [])
134         {
135                 if ($level >= $this->logLevel) {
136                         return;
137                 }
138
139                 $formattedLog = $this->formatLog($level, $message, $context);
140                 $this->write($level, $formattedLog);
141         }
142
143         /**
144          * {@inheritdoc}
145          */
146         public function emergency($message, array $context = array())
147         {
148                 $this->addEntry(LOG_EMERG, $message, $context);
149         }
150
151         /**
152          * {@inheritdoc}
153          */
154         public function alert($message, array $context = array())
155         {
156                 $this->addEntry(LOG_ALERT, $message, $context);
157         }
158
159         /**
160          * {@inheritdoc}
161          */
162         public function critical($message, array $context = array())
163         {
164                 $this->addEntry(LOG_CRIT, $message, $context);
165         }
166
167         /**
168          * {@inheritdoc}
169          */
170         public function error($message, array $context = array())
171         {
172                 $this->addEntry(LOG_ERR, $message, $context);
173         }
174
175         /**
176          * {@inheritdoc}
177          */
178         public function warning($message, array $context = array())
179         {
180                 $this->addEntry(LOG_WARNING, $message, $context);
181         }
182
183         /**
184          * {@inheritdoc}
185          */
186         public function notice($message, array $context = array())
187         {
188                 $this->addEntry(LOG_NOTICE, $message, $context);
189         }
190
191         /**
192          * {@inheritdoc}
193          */
194         public function info($message, array $context = array())
195         {
196                 $this->addEntry(LOG_INFO, $message, $context);
197         }
198
199         /**
200          * {@inheritdoc}
201          */
202         public function debug($message, array $context = array())
203         {
204                 $this->addEntry(LOG_DEBUG, $message, $context);
205         }
206
207         /**
208          * {@inheritdoc}
209          */
210         public function log($level, $message, array $context = array())
211         {
212                 $logLevel = $this->mapLevelToPriority($level);
213                 $this->addEntry($logLevel, $message, $context);
214         }
215 }