]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger/Type/StreamLogger.php
Merge pull request #13246 from nupplaphil/feat/addons
[friendica.git] / src / Core / Logger / Type / StreamLogger.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core\Logger\Type;
23
24 use Friendica\Core\Logger\Capabilities\IHaveCallIntrospections;
25 use Friendica\Core\Logger\Exception\LoggerException;
26 use Friendica\Core\Logger\Exception\LogLevelException;
27 use Friendica\Util\DateTimeFormat;
28 use Psr\Log\LogLevel;
29
30 /**
31  * A Logger instance for logging into a stream (file, stdout, stderr)
32  */
33 class StreamLogger extends AbstractLogger
34 {
35         /**
36          * The minimum loglevel at which this logger will be triggered
37          * @var string
38          */
39         private $logLevel;
40
41         /**
42          * The stream, where the current logger is writing into
43          * @var resource
44          */
45         private $stream;
46
47         /**
48          * The current process ID
49          * @var int
50          */
51         private $pid;
52
53         /**
54          * Translates LogLevel log levels to integer values
55          * @var array
56          */
57         public const levelToInt = [
58                 LogLevel::EMERGENCY => 0,
59                 LogLevel::ALERT     => 1,
60                 LogLevel::CRITICAL  => 2,
61                 LogLevel::ERROR     => 3,
62                 LogLevel::WARNING   => 4,
63                 LogLevel::NOTICE    => 5,
64                 LogLevel::INFO      => 6,
65                 LogLevel::DEBUG     => 7,
66         ];
67
68         /**
69          * {@inheritdoc}
70          * @param string          $level  The minimum loglevel at which this logger will be triggered
71          *
72          * @throws LoggerException
73          */
74         public function __construct(string $channel, IHaveCallIntrospections $introspection, $stream, int $logLevel, int $pid)
75         {
76                 parent::__construct($channel, $introspection);
77
78                 $this->stream   = $stream;
79                 $this->pid      = $pid;
80                 $this->logLevel = $logLevel;
81         }
82
83         public function close()
84         {
85                 if (is_resource($this->stream)) {
86                         fclose($this->stream);
87                 }
88
89                 $this->stream = null;
90         }
91
92         /**
93          * Adds a new entry to the log
94          *
95          * @param mixed  $level
96          * @param string $message
97          * @param array  $context
98          *
99          * @return void
100          *
101          * @throws LoggerException
102          * @throws LogLevelException
103          */
104         protected function addEntry($level, string $message, array $context = [])
105         {
106                 if (!array_key_exists($level, static::levelToInt)) {
107                         throw new LogLevelException(sprintf('The level "%s" is not valid.', $level));
108                 }
109
110                 $logLevel = static::levelToInt[$level];
111
112                 if ($logLevel > $this->logLevel) {
113                         return;
114                 }
115
116                 $formattedLog = $this->formatLog($level, $message, $context);
117                 fwrite($this->stream, $formattedLog);
118         }
119
120         /**
121          * Formats a log record for the syslog output
122          *
123          * @param mixed  $level   The loglevel/priority
124          * @param string $message The message
125          * @param array  $context The context of this call
126          *
127          * @return string the formatted syslog output
128          *
129          * @throws LoggerException
130          */
131         private function formatLog($level, string $message, array $context = []): string
132         {
133                 $record = $this->introspection->getRecord();
134                 $record = array_merge($record, ['uid' => $this->logUid, 'process_id' => $this->pid]);
135
136                 try {
137                         $logMessage = DateTimeFormat::utcNow(DateTimeFormat::ATOM) . ' ';
138                 } catch (\Exception $exception) {
139                         throw new LoggerException('Cannot get current datetime.', $exception);
140                 }
141                 $logMessage .= $this->channel . ' ';
142                 $logMessage .= '[' . strtoupper($level) . ']: ';
143                 $logMessage .= $this->psrInterpolate($message, $context) . ' ';
144                 $logMessage .= $this->jsonEncodeArray($context) . ' - ';
145                 $logMessage .= $this->jsonEncodeArray($record);
146                 $logMessage .= PHP_EOL;
147
148                 return $logMessage;
149         }
150 }