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\LoggerException;
25 use Friendica\Util\Introspection;
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
42 * The output channel of this logger
48 * The Introspection for the current call
51 protected $introspection;
54 * The UID of the current call
60 * Adds a new entry to the log
63 * @param string $message
64 * @param array $context
68 abstract protected function addEntry($level, string $message, array $context = []);
71 * @param string $channel The output channel
72 * @param Introspection $introspection The introspection of the current call
74 * @throws LoggerException
76 public function __construct(string $channel, Introspection $introspection)
78 $this->channel = $channel;
79 $this->introspection = $introspection;
82 $this->logUid = Strings::getRandomHex(6);
83 } catch (\Exception $exception) {
84 throw new LoggerException('Cannot generate log Id', $exception);
89 * Simple interpolation of PSR-3 compliant replacements ( variables between '{' and '}' )
91 * @see https://www.php-fig.org/psr/psr-3/#12-message
93 * @param string $message
94 * @param array $context
96 * @return string the interpolated message
98 protected function psrInterpolate(string $message, array $context = []): string
101 foreach ($context as $key => $value) {
102 // check that the value can be casted to string
103 if (!is_array($value) && (!is_object($value) || method_exists($value, '__toString'))) {
104 $replace['{' . $key . '}'] = $value;
105 } elseif (is_array($value)) {
106 $replace['{' . $key . '}'] = @json_encode($value);
110 return strtr($message, $replace);
114 * JSON Encodes a complete array including objects with "__toString()" methods
116 * @param array $input an Input Array to encode
118 * @return false|string The json encoded output of the array
120 protected function jsonEncodeArray(array $input)
124 foreach ($input as $key => $value) {
125 if (is_object($value) && method_exists($value, '__toString')) {
126 $output[$key] = $value->__toString();
128 $output[$key] = $value;
132 return @json_encode($output);
138 public function emergency($message, array $context = [])
140 $this->addEntry(LogLevel::EMERGENCY, (string) $message, $context);
146 public function alert($message, array $context = [])
148 $this->addEntry(LogLevel::ALERT, (string) $message, $context);
154 public function critical($message, array $context = [])
156 $this->addEntry(LogLevel::CRITICAL, (string) $message, $context);
162 public function error($message, array $context = [])
164 $this->addEntry(LogLevel::ERROR, (string) $message, $context);
170 public function warning($message, array $context = [])
172 $this->addEntry(LogLevel::WARNING, (string) $message, $context);
178 public function notice($message, array $context = [])
180 $this->addEntry(LogLevel::NOTICE, (string) $message, $context);
186 public function info($message, array $context = [])
188 $this->addEntry(LogLevel::INFO, (string) $message, $context);
194 public function debug($message, array $context = [])
196 $this->addEntry(LogLevel::DEBUG, (string) $message, $context);
202 public function log($level, $message, array $context = [])
204 $this->addEntry($level, (string) $message, $context);