3 * @copyright Copyright (C) 2010-2023, 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\Capability\IHaveCallIntrospections;
25 use Friendica\Core\Logger\Exception\LoggerException;
26 use Friendica\Core\Logger\Exception\LogLevelException;
27 use Friendica\Util\DateTimeFormat;
31 * A Logger instance for logging into a stream (file, stdout, stderr)
33 class StreamLogger extends AbstractLogger
35 const NAME = 'stream';
38 * The minimum loglevel at which this logger will be triggered
44 * The stream, where the current logger is writing into
50 * The current process ID
56 * Translates LogLevel log levels to integer values
59 public const levelToInt = [
60 LogLevel::EMERGENCY => 0,
62 LogLevel::CRITICAL => 2,
64 LogLevel::WARNING => 4,
65 LogLevel::NOTICE => 5,
72 * @param string $level The minimum loglevel at which this logger will be triggered
74 * @throws LoggerException
76 public function __construct(string $channel, IHaveCallIntrospections $introspection, $stream, int $logLevel, int $pid)
78 parent::__construct($channel, $introspection);
80 $this->stream = $stream;
82 $this->logLevel = $logLevel;
85 public function close()
87 if (is_resource($this->stream)) {
88 fclose($this->stream);
95 * Adds a new entry to the log
98 * @param string $message
99 * @param array $context
103 * @throws LoggerException
104 * @throws LogLevelException
106 protected function addEntry($level, string $message, array $context = [])
108 if (!array_key_exists($level, static::levelToInt)) {
109 throw new LogLevelException(sprintf('The level "%s" is not valid.', $level));
112 $logLevel = static::levelToInt[$level];
114 if ($logLevel > $this->logLevel) {
118 $formattedLog = $this->formatLog($level, $message, $context);
119 fwrite($this->stream, $formattedLog);
123 * Formats a log record for the syslog output
125 * @param mixed $level The loglevel/priority
126 * @param string $message The message
127 * @param array $context The context of this call
129 * @return string the formatted syslog output
131 * @throws LoggerException
133 private function formatLog($level, string $message, array $context = []): string
135 $record = $this->introspection->getRecord();
136 $record = array_merge($record, ['uid' => $this->logUid, 'process_id' => $this->pid]);
139 $logMessage = DateTimeFormat::utcNow(DateTimeFormat::ATOM) . ' ';
140 } catch (\Exception $exception) {
141 throw new LoggerException('Cannot get current datetime.', $exception);
143 $logMessage .= $this->channel . ' ';
144 $logMessage .= '[' . strtoupper($level) . ']: ';
145 $logMessage .= $this->psrInterpolate($message, $context) . ' ';
146 $logMessage .= $this->jsonEncodeArray($context) . ' - ';
147 $logMessage .= $this->jsonEncodeArray($record);
148 $logMessage .= PHP_EOL;