18abstract class Logger extends Log\AbstractLogger
21 LogLevel::EMERGENCY => LOG_EMERG,
22 LogLevel::ALERT => LOG_ALERT,
23 LogLevel::CRITICAL => LOG_CRIT,
24 LogLevel::ERROR => LOG_ERR,
25 LogLevel::WARNING => LOG_WARNING,
26 LogLevel::NOTICE => LOG_NOTICE,
27 LogLevel::INFO => LOG_INFO,
28 LogLevel::DEBUG => LOG_DEBUG,
41 if (!isset($this->context[
'date']))
46 if (!isset($this->context[
'host']))
48 $this->context[
'host'] = $_SERVER[
'HTTP_HOST'] ??
'';
53 return $formatter->format($this->message, $this->context);
62 if (!isset(static::$supportedLevels[
$level]))
64 throw new Log\InvalidArgumentException(
"Log level {$level} is unsupported.");
67 if ($this->level !==
null && static::$supportedLevels[
$level] > $this->level)
95 if (isset(static::$supportedLevels[
$level]))
97 $this->level = static::$supportedLevels[
$level];
132 public static function create(
string $id, $params = [])
134 $loggersConfig = Config\Configuration::getValue(
'loggers');
138 if (isset($loggersConfig[$id]))
140 $config = $loggersConfig[$id];
142 if (isset($config[
'className']))
144 $class = $config[
'className'];
146 $args = $config[
'constructorParams'] ?? [];
147 if ($args instanceof \Closure)
152 $logger =
new $class(...array_values($args));
154 elseif (isset($config[
'constructor']))
156 $closure = $config[
'constructor'];
157 if ($closure instanceof \Closure)
159 $logger = $closure(...array_values($params));
163 if ($logger instanceof
static)
165 if (isset($config[
'level']))
167 $logger->setLevel($config[
'level']);
170 if (isset($config[
'formatter']))
172 $serviceLocator = DI\ServiceLocator::getInstance();
173 if ($serviceLocator->has($config[
'formatter']))
175 $logger->setFormatter($serviceLocator->get($config[
'formatter']));
186 if ($this->formatter ===
null)
static create(string $id, $params=[])
setFormatter(LogFormatterInterface $formatter)
log($level, string|\Stringable $message, array $context=[])
logMessage(string $level, string $message)