Kévin Dunglas
Contributed by Kévin Dunglas in #24300

Symfony Flex proposes a whole new philosophy when developing Symfony 3.3 or newer applications. The main difference with respect to the old Symfony Standard Edition is that applications now must explicitly install everything they need, instead of having to remove what they don't need.

In practice, this means that you need to install the console recipe (composer require cli) before running any command, the Twig recipe (composer require twig) before rendering a single template, etc. This also means that you don't have a logger unless you install Monolog via the logging recipe (composer require log).

However, given that logging is essential for all applications, in Symfony 3.4 we introduced a new and minimalist PSR-3 based logger which is always available in all Symfony applications without having to install any package.

The new logger is available as the logger service and, if you use autowiring, via the injection of Psr\Log\LoggerInterface. The logger has been designed to integrate seamlessly with containerization (Docker) and orchestration (Kubernetes) tools, as well as with most modern cloud providers. When using those, logs will be automatically collected, aggregated and stored. In addition:

  • It writes contents by default to php://stderr;
  • It defines the same seven log levels as Monolog, but it only logs warning messages or higher by default;
  • It defines a simple method to log messages: log($level, $message, array $context = []) (where $level is Psr\Log\LogLevel::DEBUG, Psr\Log\LogLevel::INFO, etc.) and the traditional shortcut methods (debug(), info(), warning(), etc.);
  • Log messages are displayed in the console when running the integrated PHP web server (bin/console server:start or Flex's make serve).

This new logger is minimalist by nature and it can be considered "feature complete". We'll never add more features or methods or configuration options to it. If you need more options and features, you must use Monolog instead.

Published in #Living on the edge