New in Symfony 3.4: Minimalist PSR-3 logger

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.


> It only defines a single method to log messages: log($level, $message, array $context = []) (where $level is Psr\Log\LogLevel::DEBUG, Psr\Log\LogLevel::INFO, etc.)

That's not true. It implements the whole PSR-3 methods (you forgot to look at the parent class defining all the nice PSR-3 methods as calls to `log()`)
@Christophe thanks for the heads up! I've just updated the blog post.
This minimalist logger has 3 options:

- the minimum level from which the message will be wrote (defaults to SHELL_VERBOSITY if defined, warning otherwise)
- the stream where to send logs (defaults to php://stderr)
- a callable that format the message, for instance in JSON if you want too (defaults with a builtin one generating a traditional text message)

These options should be enough for most usages in clouds and containers.
Login with SensioLabsConnect to post a comment