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