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\Util\DateTimeFormat;
27 use Friendica\Util\FileSystem;
28 use Friendica\Util\Introspection;
32 * A Logger instance for logging into a stream (file, stdout, stderr)
34 class StreamLogger extends AbstractLogger
37 * The minimum loglevel at which this logger will be triggered
43 * The file URL of the stream (if needed)
49 * The stream, where the current logger is writing into
55 * The current process ID
66 * Translates LogLevel log levels to integer values
69 private $levelToInt = [
70 LogLevel::EMERGENCY => 0,
72 LogLevel::CRITICAL => 2,
74 LogLevel::WARNING => 4,
75 LogLevel::NOTICE => 5,
82 * @param string|resource $stream The stream to write with this logger (either a file or a stream, i.e. stdout)
83 * @param string $level The minimum loglevel at which this logger will be triggered
85 * @throws LoggerArgumentException
87 public function __construct($channel, $stream, Introspection $introspection, FileSystem $fileSystem, string $level = LogLevel::DEBUG)
89 $this->fileSystem = $fileSystem;
91 parent::__construct($channel, $introspection);
93 if (is_resource($stream)) {
94 $this->stream = $stream;
95 } elseif (is_string($stream)) {
98 throw new LoggerArgumentException('A stream must either be a resource or a string.');
101 $this->pid = getmypid();
102 if (array_key_exists($level, $this->levelToInt)) {
103 $this->logLevel = $this->levelToInt[$level];
105 throw new LoggerArgumentException(sprintf('The level "%s" is not valid.', $level));
108 $this->checkStream();
111 public function close()
113 if ($this->url && is_resource($this->stream)) {
114 fclose($this->stream);
117 $this->stream = null;
121 * Adds a new entry to the log
123 * @param mixed $level
124 * @param string $message
125 * @param array $context
129 * @throws LoggerException
130 * @throws LoggerArgumentException
132 protected function addEntry($level, string $message, array $context = [])
134 if (!array_key_exists($level, $this->levelToInt)) {
135 throw new LoggerArgumentException(sprintf('The level "%s" is not valid.', $level));
138 $logLevel = $this->levelToInt[$level];
140 if ($logLevel > $this->logLevel) {
144 $this->checkStream();
146 $formattedLog = $this->formatLog($level, $message, $context);
147 fwrite($this->stream, $formattedLog);
151 * Formats a log record for the syslog output
153 * @param mixed $level The loglevel/priority
154 * @param string $message The message
155 * @param array $context The context of this call
157 * @return string the formatted syslog output
159 * @throws LoggerException
161 private function formatLog($level, string $message, array $context = []): string
163 $record = $this->introspection->getRecord();
164 $record = array_merge($record, ['uid' => $this->logUid, 'process_id' => $this->pid]);
167 $logMessage = DateTimeFormat::utcNow(DateTimeFormat::ATOM) . ' ';
168 } catch (\Exception $exception) {
169 throw new LoggerException('Cannot get current datetime.', $exception);
171 $logMessage .= $this->channel . ' ';
172 $logMessage .= '[' . strtoupper($level) . ']: ';
173 $logMessage .= $this->psrInterpolate($message, $context) . ' ';
174 $logMessage .= $this->jsonEncodeArray($context) . ' - ';
175 $logMessage .= $this->jsonEncodeArray($record);
176 $logMessage .= PHP_EOL;
182 * Checks the current stream
184 * @throws LoggerException
185 * @throws LoggerArgumentException
187 private function checkStream()
189 if (is_resource($this->stream)) {
193 if (empty($this->url)) {
194 throw new LoggerArgumentException('Missing stream URL.');
198 $this->stream = $this->fileSystem->createStream($this->url);
199 } catch (\UnexpectedValueException $exception) {
200 throw new LoggerException('Cannot create stream.', $exception);