3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Core\Logger\Type;
24 use Friendica\Core\Logger\Exception\LoggerArgumentException;
25 use Friendica\Core\Logger\Exception\LoggerException;
26 use Friendica\Core\Logger\Exception\LogLevelException;
27 use Friendica\Util\DateTimeFormat;
28 use Friendica\Util\FileSystem;
29 use Friendica\Util\Introspection;
33 * A Logger instance for logging into a stream (file, stdout, stderr)
35 class StreamLogger extends AbstractLogger
38 * The minimum loglevel at which this logger will be triggered
44 * The file URL of the stream (if needed)
50 * The stream, where the current logger is writing into
56 * The current process ID
67 * Translates LogLevel log levels to integer values
70 private $levelToInt = [
71 LogLevel::EMERGENCY => 0,
73 LogLevel::CRITICAL => 2,
75 LogLevel::WARNING => 4,
76 LogLevel::NOTICE => 5,
83 * @param string|resource $stream The stream to write with this logger (either a file or a stream, i.e. stdout)
84 * @param string $level The minimum loglevel at which this logger will be triggered
86 * @throws LoggerArgumentException
87 * @throws LogLevelException
89 public function __construct($channel, $stream, Introspection $introspection, FileSystem $fileSystem, string $level = LogLevel::DEBUG)
91 $this->fileSystem = $fileSystem;
93 parent::__construct($channel, $introspection);
95 if (is_resource($stream)) {
96 $this->stream = $stream;
97 } elseif (is_string($stream)) {
100 throw new LoggerArgumentException('A stream must either be a resource or a string.');
103 $this->pid = getmypid();
104 if (array_key_exists($level, $this->levelToInt)) {
105 $this->logLevel = $this->levelToInt[$level];
107 throw new LogLevelException(sprintf('The level "%s" is not valid.', $level));
110 $this->checkStream();
113 public function close()
115 if ($this->url && is_resource($this->stream)) {
116 fclose($this->stream);
119 $this->stream = null;
123 * Adds a new entry to the log
125 * @param mixed $level
126 * @param string $message
127 * @param array $context
131 * @throws LoggerException
132 * @throws LogLevelException
134 protected function addEntry($level, string $message, array $context = [])
136 if (!array_key_exists($level, $this->levelToInt)) {
137 throw new LogLevelException(sprintf('The level "%s" is not valid.', $level));
140 $logLevel = $this->levelToInt[$level];
142 if ($logLevel > $this->logLevel) {
146 $this->checkStream();
148 $formattedLog = $this->formatLog($level, $message, $context);
149 fwrite($this->stream, $formattedLog);
153 * Formats a log record for the syslog output
155 * @param mixed $level The loglevel/priority
156 * @param string $message The message
157 * @param array $context The context of this call
159 * @return string the formatted syslog output
161 * @throws LoggerException
163 private function formatLog($level, string $message, array $context = []): string
165 $record = $this->introspection->getRecord();
166 $record = array_merge($record, ['uid' => $this->logUid, 'process_id' => $this->pid]);
169 $logMessage = DateTimeFormat::utcNow(DateTimeFormat::ATOM) . ' ';
170 } catch (\Exception $exception) {
171 throw new LoggerException('Cannot get current datetime.', $exception);
173 $logMessage .= $this->channel . ' ';
174 $logMessage .= '[' . strtoupper($level) . ']: ';
175 $logMessage .= $this->psrInterpolate($message, $context) . ' ';
176 $logMessage .= $this->jsonEncodeArray($context) . ' - ';
177 $logMessage .= $this->jsonEncodeArray($record);
178 $logMessage .= PHP_EOL;
184 * Checks the current stream
186 * @throws LoggerException
187 * @throws LoggerArgumentException
189 private function checkStream()
191 if (is_resource($this->stream)) {
195 if (empty($this->url)) {
196 throw new LoggerArgumentException('Missing stream URL.');
200 $this->stream = $this->fileSystem->createStream($this->url);
201 } catch (\UnexpectedValueException $exception) {
202 throw new LoggerException('Cannot create stream.', $exception);