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\Util\Logger;
24 use Friendica\Util\Introspection;
25 use Friendica\Util\Strings;
26 use Psr\Log\LoggerInterface;
30 * This class contains all necessary dependencies and calls for Friendica
31 * Every new Logger should extend this class and define, how addEntry() works
33 * Additional information for each Logger, who extends this class:
36 * - Channel of the current call (i.e. index, worker, daemon, ...)
38 abstract class AbstractLogger implements LoggerInterface
41 * The output channel of this logger
47 * The Introspection for the current call
50 protected $introspection;
53 * The UID of the current call
59 * Adds a new entry to the log
62 * @param string $message
63 * @param array $context
67 abstract protected function addEntry($level, $message, $context = []);
70 * @param string $channel The output channel
71 * @param Introspection $introspection The introspection of the current call
75 public function __construct($channel, Introspection $introspection)
77 $this->channel = $channel;
78 $this->introspection = $introspection;
79 $this->logUid = Strings::getRandomHex(6);
83 * Simple interpolation of PSR-3 compliant replacements ( variables between '{' and '}' )
84 * @see https://www.php-fig.org/psr/psr-3/#12-message
86 * @param string $message
87 * @param array $context
89 * @return string the interpolated message
91 protected function psrInterpolate($message, array $context = array())
94 foreach ($context as $key => $value) {
95 // check that the value can be casted to string
96 if (!is_array($value) && (!is_object($value) || method_exists($value, '__toString'))) {
97 $replace['{' . $key . '}'] = $value;
98 } elseif (is_array($value)) {
99 $replace['{' . $key . '}'] = @json_encode($value);
103 return strtr($message, $replace);
107 * JSON Encodes an complete array including objects with "__toString()" methods
109 * @param array $input an Input Array to encode
111 * @return false|string The json encoded output of the array
113 protected function jsonEncodeArray(array $input)
117 foreach ($input as $key => $value) {
118 if (is_object($value) && method_exists($value, '__toString')) {
119 $output[$key] = $value->__toString();
121 $output[$key] = $value;
125 return @json_encode($output);
131 public function emergency($message, array $context = array())
133 $this->addEntry(LogLevel::EMERGENCY, (string) $message, $context);
139 public function alert($message, array $context = array())
141 $this->addEntry(LogLevel::ALERT, (string) $message, $context);
147 public function critical($message, array $context = array())
149 $this->addEntry(LogLevel::CRITICAL, (string) $message, $context);
155 public function error($message, array $context = array())
157 $this->addEntry(LogLevel::ERROR, (string) $message, $context);
163 public function warning($message, array $context = array())
165 $this->addEntry(LogLevel::WARNING, (string) $message, $context);
171 public function notice($message, array $context = array())
173 $this->addEntry(LogLevel::NOTICE, (string) $message, $context);
179 public function info($message, array $context = array())
181 $this->addEntry(LogLevel::INFO, (string) $message, $context);
187 public function debug($message, array $context = array())
189 $this->addEntry(LogLevel::DEBUG, (string) $message, $context);
195 public function log($level, $message, array $context = array())
197 $this->addEntry($level, (string) $message, $context);