3 * @copyright Copyright (C) 2020, Friendica
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\Util\Logger;
24 use Friendica\Util\DateTimeFormat;
25 use Friendica\Util\FileSystem;
26 use Friendica\Util\Introspection;
30 * A Logger instance for logging into a stream (file, stdout, stderr)
32 class StreamLogger extends AbstractLogger
35 * The minimum loglevel at which this logger will be triggered
41 * The file URL of the stream (if needed)
47 * The stream, where the current logger is writing into
53 * The current process ID
64 * Translates LogLevel log levels to integer values
67 private $levelToInt = [
68 LogLevel::EMERGENCY => 0,
70 LogLevel::CRITICAL => 2,
72 LogLevel::WARNING => 4,
73 LogLevel::NOTICE => 5,
80 * @param string|resource $stream The stream to write with this logger (either a file or a stream, i.e. stdout)
81 * @param string $level The minimum loglevel at which this logger will be triggered
85 public function __construct($channel, $stream, Introspection $introspection, FileSystem $fileSystem, $level = LogLevel::DEBUG)
87 $this->fileSystem = $fileSystem;
89 parent::__construct($channel, $introspection);
91 if (is_resource($stream)) {
92 $this->stream = $stream;
93 } elseif (is_string($stream)) {
96 throw new \InvalidArgumentException('A stream must either be a resource or a string.');
99 $this->pid = getmypid();
100 if (array_key_exists($level, $this->levelToInt)) {
101 $this->logLevel = $this->levelToInt[$level];
103 throw new \InvalidArgumentException(sprintf('The level "%s" is not valid.', $level));
106 $this->checkStream();
109 public function close()
111 if ($this->url && is_resource($this->stream)) {
112 fclose($this->stream);
115 $this->stream = null;
119 * Adds a new entry to the log
122 * @param string $message
123 * @param array $context
127 protected function addEntry($level, $message, $context = [])
129 if (!array_key_exists($level, $this->levelToInt)) {
130 throw new \InvalidArgumentException(sprintf('The level "%s" is not valid.', $level));
133 $logLevel = $this->levelToInt[$level];
135 if ($logLevel > $this->logLevel) {
139 $this->checkStream();
141 $formattedLog = $this->formatLog($level, $message, $context);
142 fwrite($this->stream, $formattedLog);
146 * Formats a log record for the syslog output
148 * @param int $level The loglevel/priority
149 * @param string $message The message
150 * @param array $context The context of this call
152 * @return string the formatted syslog output
154 private function formatLog($level, $message, $context = [])
156 $record = $this->introspection->getRecord();
157 $record = array_merge($record, ['uid' => $this->logUid, 'process_id' => $this->pid]);
160 $logMessage .= DateTimeFormat::utcNow(DateTimeFormat::ATOM) . ' ';
161 $logMessage .= $this->channel . ' ';
162 $logMessage .= '[' . strtoupper($level) . ']: ';
163 $logMessage .= $this->psrInterpolate($message, $context) . ' ';
164 $logMessage .= @json_encode($context) . ' - ';
165 $logMessage .= @json_encode($record);
166 $logMessage .= PHP_EOL;
171 private function checkStream()
173 if (is_resource($this->stream)) {
177 if (empty($this->url)) {
178 throw new \LogicException('Missing stream URL.');
181 $this->stream = $this->fileSystem->createStream($this->url);