Graylog2 Log Management For Symfony2 Configuration

Connecting logging through Graylog2 to projects on Symfony2

Graylog is a non standard solution for logging with the ability to set up alerts for certain events, as well as real-time viewing of filtered events. It’s written in RoR, fast, reliable and has informative graphs.

For Symfony2 with Graylog2 bundle we need

Installing the Library mlehner/gelf-php

Add to composer.json a package mlehner / gelf-php:

"mlehner/gelf-php": "v1.0"

And update the vendors:

$ php composer.phar update

The library is set!

Customize logs output on Graylog server in Symfony

In the config of needed environment (prod, dev, test) we write:

 	graylog.hostname: localhost
 	graylog.port: 12201 #standart port

For different environments, you can configure different hosts addresses. For example, for the development and testing (config_dev) localhost will fit, for Productions (config_prod) – centralized host for logging.

In a basic config «config.yml» announce two new services:

In config.yml:

     	class: Gelf\MessagePublisher
     	arguments: [%graylog.hostname%, %graylog.port%]

     	class: Monolog\Handler\GelfHandler
     	arguments: [@gelf.publisher]

The first one «gelf.publisher» is a simple «sender» of messages to the Graylog server, and the second one «monolog.gelf_handler» is a monologue handler to which the previous service as ([@ gelf.publisher]) parameter is transferred.

In other words we create a kind of service for the monologue of events subscription.

In the configuration of the monologue (config.yml) we should specify it:

         	type: service
         	id: monolog.gelf_handler
         	level: debug

We can set the «level» of messages starting from which the log will come. In short, here is a list of messages of levels in ascending order: INHERIT -> DEBUG -> INFO -> WARN -> ERROR -> FATAL. Since we have the level of «DEBUG», we receive all types of messages except for «INHERIT».

Done! You can check the log records on Graylog server.

Adding Extra Information To The Log

Due to the popularity of high-end servers, as well as cloud services, it often happens that one and the same server contains multiple virtual hosts. In this case we will not be able to filter the messages in Graylog for individual sites, messages will be given like this:

log without processor

To filter messages for different virtual hosts, we need to add extra information to the log, for example HTTP_HOST. To add extra information you should write your service-Processor for monolog, or use a ready-made from Monolog / Processor. Creating of your own Processor is described here

We are ready to connect WebProcessor from Monolog. (Vendor / Monolog / Processor / WebProcessor).

Announcing the new service in config.yml:

             class: Monolog\Processor\WebProcessor
                 - { name: monolog.processor, method: __invoke }

Open GrayLog and see extra information in the messages:

log with graylog processor

Now you can create a separate «streams» in Graylog for each virtual host, and assign separate levels of notifications for different hosts.

This way we hooked up Symfony to Graylog with login of detailed information, by setting only Graylog2 configuration and Graylog2 settings without having to write additional code.