Monolog is a great package available as a Symfony component. It is very powerful yet very easy to setup and use. You can log almost everything to everywhere. Configuring Monolog in Symfony involves setting up log channels, handlers, and formatters.

 

Basic Configuration
In your config/packages/dev/monolog.yaml file, you can define a basic configuration for Monolog

monolog:
    channels: ['app']
    handlers:
        app:
            type: stream
            path: '%kernel.logs_dir%/%kernel.environment%.log'
            level: debug

This configuration creates a log channel named app and configures a stream handler that writes logs to a file. The log file is stored in the logs directory with the name corresponding to the current environment.

 

Customizing Log Formats
You can customize the log format by defining a formatter for your handler. For example, to include the log level, date, and message in the log output, modify the configuration as follows

monolog:
    handlers:
        app:
            type: stream
            path: '%kernel.logs_dir%/%kernel.environment%.log'
            level: debug
            formatter: 'Monolog\Formatter\LineFormatter'
            formatter_options:
                format: "%datetime% [%level_name%] %message%\n"

Here, we set the formatter option to Monolog\Formatter\LineFormatter and provide the format option to specify the desired log format.

 

Adding Additional Handlers
You can configure multiple handlers for different log channels. For example, to add a separate log file for error messages, you can define an additional handler

monolog:
    channels: ['app', 'error']
    handlers:
        app:
            # ...
        error:
            type: stream
            path: '%kernel.logs_dir%/error.log'
            level: error

In this configuration, we added a new log channel named error and defined a separate stream handler that logs only error-level messages to a different log file.

 

Using Different Types of Handlers
Monolog provides various types of handlers, such as stream, rotating_file, fingers_crossed, syslog, etc. Here's an example that uses a rotating file handler

monolog:
    handlers:
        app:
            type: rotating_file
            path: '%kernel.logs_dir%/app.log'
            level: debug
            max_files: 30

This configuration sets up a rotating file handler that creates a new log file when the current file reaches a certain size or the maximum number of files is exceeded.

 

Most commonly used log channels in Symfony Monolog

- app: The app channel is the default channel used by Symfony. It captures logs generated by the core framework components, such as routing, controllers, services, etc.
- request: The request channel is often used to log information related to HTTP requests. This can include request and response headers, request parameters, and other relevant details.
- security: The security channel is used for logging security-related events, such as authentication failures, authorization errors, and other security-related activities.
- doctrine: The doctrine channel is used for logging database-related events and queries. It can be helpful for debugging database interactions, identifying performance issues, or tracking database errors.
- event: The event channel is commonly used for logging application-specific events or custom event dispatching. It allows you to log specific events and their relevant data for monitoring and debugging purposes.
- cache: The cache channel is used for logging cache-related events, such as cache hits, misses, and cache-related errors. It helps in understanding cache usage and performance.
- console: The console channel is used for logging messages generated during command-line operations. It captures console output, command execution information, and error messages.
- email: The email channel is used for logging email-related events, such as sending or receiving emails. It can be helpful in debugging email sending issues or tracking email-related errors.

These are some commonly used log channels in Symfony Monolog, but you can create additional channels based on your application's specific needs. Each channel allows you to define its own handlers, formatters, and log levels to control how logs are processed and stored.

For more information on this topic visit Symfony Documentation: Monolog