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\Capabilities\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
36 * The minimum loglevel at which this logger will be triggered
42 * The stream, where the current logger is writing into
48 * The current process ID
54 * Translates LogLevel log levels to integer values
57 public const levelToInt = [
58 LogLevel::EMERGENCY => 0,
60 LogLevel::CRITICAL => 2,
62 LogLevel::WARNING => 4,
63 LogLevel::NOTICE => 5,
70 * @param string $level The minimum loglevel at which this logger will be triggered
72 * @throws LoggerException
74 public function __construct(string $channel, IHaveCallIntrospections $introspection, $stream, int $logLevel, int $pid)
76 parent::__construct($channel, $introspection);
78 $this->stream = $stream;
80 $this->logLevel = $logLevel;
83 public function close()
85 if (is_resource($this->stream)) {
86 fclose($this->stream);
93 * Adds a new entry to the log
96 * @param string $message
97 * @param array $context
101 * @throws LoggerException
102 * @throws LogLevelException
104 protected function addEntry($level, string $message, array $context = [])
106 if (!array_key_exists($level, static::levelToInt)) {
107 throw new LogLevelException(sprintf('The level "%s" is not valid.', $level));
110 $logLevel = static::levelToInt[$level];
112 if ($logLevel > $this->logLevel) {
116 $formattedLog = $this->formatLog($level, $message, $context);
117 fwrite($this->stream, $formattedLog);
121 * Formats a log record for the syslog output
123 * @param mixed $level The loglevel/priority
124 * @param string $message The message
125 * @param array $context The context of this call
127 * @return string the formatted syslog output
129 * @throws LoggerException
131 private function formatLog($level, string $message, array $context = []): string
133 $record = $this->introspection->getRecord();
134 $record = array_merge($record, ['uid' => $this->logUid, 'process_id' => $this->pid]);
137 $logMessage = DateTimeFormat::utcNow(DateTimeFormat::ATOM) . ' ';
138 } catch (\Exception $exception) {
139 throw new LoggerException('Cannot get current datetime.', $exception);
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;