3 namespace Friendica\Util\Logger;
5 use Friendica\Util\Introspection;
6 use Friendica\Util\Strings;
7 use Psr\Log\LoggerInterface;
11 * This class contains all necessary dependencies and calls for Friendica
12 * Every new Logger should extend this class and define, how addEntry() works
14 * Additional information for each Logger, who extends this class:
17 * - Channel of the current call (i.e. index, worker, daemon, ...)
19 abstract class AbstractLogger implements LoggerInterface
22 * The output channel of this logger
28 * The Introspection for the current call
31 protected $introspection;
34 * The UID of the current call
40 * Adds a new entry to the log
43 * @param string $message
44 * @param array $context
48 abstract protected function addEntry($level, $message, $context = []);
51 * @param string $channel The output channel
52 * @param Introspection $introspection The introspection of the current call
56 public function __construct($channel, Introspection $introspection)
58 $this->channel = $channel;
59 $this->introspection = $introspection;
60 $this->logUid = Strings::getRandomHex(6);
64 * Simple interpolation of PSR-3 compliant replacements ( variables between '{' and '}' )
65 * @see https://www.php-fig.org/psr/psr-3/#12-message
67 * @param string $message
68 * @param array $context
70 * @return string the interpolated message
72 protected function psrInterpolate($message, array $context = array())
75 foreach ($context as $key => $value) {
76 // check that the value can be casted to string
77 if (!is_array($value) && (!is_object($value) || method_exists($value, '__toString'))) {
78 $replace['{' . $key . '}'] = $value;
79 } elseif (is_array($value)) {
80 $replace['{' . $key . '}'] = @json_encode($value);
84 return strtr($message, $replace);
90 public function emergency($message, array $context = array())
92 $this->addEntry(LogLevel::EMERGENCY, (string) $message, $context);
98 public function alert($message, array $context = array())
100 $this->addEntry(LogLevel::ALERT, (string) $message, $context);
106 public function critical($message, array $context = array())
108 $this->addEntry(LogLevel::CRITICAL, (string) $message, $context);
114 public function error($message, array $context = array())
116 $this->addEntry(LogLevel::ERROR, (string) $message, $context);
122 public function warning($message, array $context = array())
124 $this->addEntry(LogLevel::WARNING, (string) $message, $context);
130 public function notice($message, array $context = array())
132 $this->addEntry(LogLevel::NOTICE, (string) $message, $context);
138 public function info($message, array $context = array())
140 $this->addEntry(LogLevel::INFO, (string) $message, $context);
146 public function debug($message, array $context = array())
148 $this->addEntry(LogLevel::DEBUG, (string) $message, $context);
154 public function log($level, $message, array $context = array())
156 $this->addEntry($level, (string) $message, $context);