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