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\Util\Strings;
27 use Psr\Log\LoggerInterface;
31 * This class contains all necessary dependencies and calls for Friendica
32 * Every new Logger should extend this class and define, how addEntry() works
34 * Additional information for each Logger, who extends this class:
37 * - Channel of the current call (i.e. index, worker, daemon, ...)
39 abstract class AbstractLogger implements LoggerInterface
44 * The output channel of this logger
50 * The Introspection for the current call
51 * @var IHaveCallIntrospections
53 protected $introspection;
56 * The UID of the current call
62 * Adds a new entry to the log
65 * @param string $message
66 * @param array $context
70 abstract protected function addEntry($level, string $message, array $context = []);
73 * @param string $channel The output channel
74 * @param IHaveCallIntrospections $introspection The introspection of the current call
76 * @throws LoggerException
78 public function __construct(string $channel, IHaveCallIntrospections $introspection)
80 $this->channel = $channel;
81 $this->introspection = $introspection;
84 $this->logUid = Strings::getRandomHex(6);
85 } catch (\Exception $exception) {
86 throw new LoggerException('Cannot generate log Id', $exception);
91 * Simple interpolation of PSR-3 compliant replacements ( variables between '{' and '}' )
93 * @see https://www.php-fig.org/psr/psr-3/#12-message
95 * @param string $message
96 * @param array $context
98 * @return string the interpolated message
100 protected function psrInterpolate(string $message, array $context = []): string
103 foreach ($context as $key => $value) {
104 // check that the value can be casted to string
105 if (!is_array($value) && (!is_object($value) || method_exists($value, '__toString'))) {
106 $replace['{' . $key . '}'] = $value;
107 } elseif (is_array($value)) {
108 $replace['{' . $key . '}'] = @json_encode($value);
112 return strtr($message, $replace);
116 * JSON Encodes a complete array including objects with "__toString()" methods
118 * @param array $input an Input Array to encode
120 * @return false|string The json encoded output of the array
122 protected function jsonEncodeArray(array $input)
126 foreach ($input as $key => $value) {
127 if (is_object($value) && method_exists($value, '__toString')) {
128 $output[$key] = $value->__toString();
130 $output[$key] = $value;
134 return @json_encode($output, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
140 public function emergency($message, array $context = [])
142 $this->addEntry(LogLevel::EMERGENCY, (string) $message, $context);
148 public function alert($message, array $context = [])
150 $this->addEntry(LogLevel::ALERT, (string) $message, $context);
156 public function critical($message, array $context = [])
158 $this->addEntry(LogLevel::CRITICAL, (string) $message, $context);
164 public function error($message, array $context = [])
166 $this->addEntry(LogLevel::ERROR, (string) $message, $context);
172 public function warning($message, array $context = [])
174 $this->addEntry(LogLevel::WARNING, (string) $message, $context);
180 public function notice($message, array $context = [])
182 $this->addEntry(LogLevel::NOTICE, (string) $message, $context);
188 public function info($message, array $context = [])
190 $this->addEntry(LogLevel::INFO, (string) $message, $context);
196 public function debug($message, array $context = [])
198 $this->addEntry(LogLevel::DEBUG, (string) $message, $context);
204 public function log($level, $message, array $context = [])
206 $this->addEntry($level, (string) $message, $context);