output = $output ?? new ConsoleOutput(); } public function emergency($message, array $context = []): void { $this->log(LogLevel::EMERGENCY, $message, $context); } public function alert($message, array $context = []): void { $this->log(LogLevel::ALERT, $message, $context); } public function critical($message, array $context = []): void { $this->log(LogLevel::CRITICAL, $message, $context); } public function error($message, array $context = []): void { $this->log(LogLevel::ERROR, $message, $context); } public function warning($message, array $context = []): void { $this->log(LogLevel::WARNING, $message, $context); } public function notice($message, array $context = []): void { $this->log(LogLevel::NOTICE, $message, $context); } public function info($message, array $context = []): void { $this->log(LogLevel::INFO, $message, $context); } public function debug($message, array $context = []): void { $this->log(LogLevel::DEBUG, $message, $context); } public function log($level, $message, array $context = []): void { $config = $this->config->get(StdoutLoggerInterface::class, ['log_level' => []]); // Check if the log level is allowed if (! in_array($level, $config['log_level'], true)) { return; } $tags = array_intersect_key($context, array_flip($this->tags)); $context = array_diff_key($context, $tags); // Handle objects that are not Stringable foreach ($context as $key => $value) { if (is_object($value) && ! $value instanceof Stringable) { $context[$key] = ' ' . $value::class; } } $search = array_map(fn ($key) => sprintf('{%s}', $key), array_keys($context)); $message = str_replace($search, $context, $this->getMessage((string) $message, $level, $tags)); $this->output->writeln($message); } protected function getMessage(string $message, string $level = LogLevel::INFO, array $tags = []) { $tag = match ($level) { LogLevel::EMERGENCY, LogLevel::ALERT, LogLevel::CRITICAL => 'error', LogLevel::ERROR => 'fg=red', LogLevel::WARNING, LogLevel::NOTICE => 'comment', default => 'info', }; $template = sprintf('<%s>[%s]', $tag, strtoupper($level)); $implodedTags = ''; foreach ($tags as $value) { $implodedTags .= (' [' . $value . ']'); } return sprintf($template . $implodedTags . ' %s', $message); } }