Sending Logs To GrayLog From A Symfony Application Through RabbitMQ
Message queues are an indispensable part of a system which is under high load. They protect the applications from the destructive impact of the load and help to scale up the applications easily also.
If a system consists of many application instances and/or some devices it means that you have many log producers too. So you should scale out the logging system as you scaled the application. Otherwise, the logging mechanism might be effected from the load that comes from outside, be irresponsible and makes down the entire system easily.
You can scale logging systems using message queues too. Today I’m gonna try to explain how to configure RabbitMQ and Graylog for sending logs to Graylog from a Symfony application through RabbitMQ.
Installing Composer Dependencies:
Install the RabbitMQBundle to access the RabbitMQ instance from the application.
$ composer require php-amqplib/rabbitmq-bundle
Define the RabbitMQ connection into the
Define the credentials for the RabbitMQ connection into
parameters.yml that described in config.yml
Define a new service for a RabbitMQ channel to be used by monolog.
Define a monolog handler in the Symfony configuration file like the following. For the demostration, we will use
config_dev.yml You can define handlers specifically for each environment.
Now the application is ready to send the logs to RabbitMQ. Let’s set up the RabbitMQ server instance for receiving messages.
In the example, I preferred to create a specific virtual server in order to separate RabbitMQ specific dependencies related application and log. You can ignore this step if you plan to use a separate RabbitMQ instance.
$ rabbitmqctl add_vhost logging
$ rabbitmqctl set_permissions -p logging guest “.*” “.*” “.*”
Create a new exchange on RabbitMQ with the name you defined in the configuration as a producer.
$ ./bin/console rabbitmq:setup-fabric
Now the RabbitMQ server is ready to receive logs from the application.
Setup A Log Producer For The Demo
You can simply create a Symfony command like the following, in order to create some log entries on the message queue.
namespace AppBundle\Command;use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Output\OutputInterface;class LoggingTestCommand extends ContainerAwareCommand
protected function configure()
->setDescription('Tester command for sending log over');
}protected function execute(InputInterface $input, OutputInterface $output)
/** @var LoggerInterface $logger */
$logger = $this->getContainer()->get('logger');
$output->writeln('Test messages are sending to message queue now. Please press CTRL+C to break this process.');
$logger->error('Some error occurred while doing sometihng.', [
'some_value' => 'ABC',
'another_value' => 1532,
Call the following command to create some logs on the RabbitMQ server.
$ ./bin/console logging:test
As you can see from the following picture; the application is started sending logs to the RabbitMQ server.
Sign in to your Graylog as a administrator and go to
Inputs section from
Choose the “GELF AMQP” from the dropdown list on the head of the page and click to
Launch new input button (The green button near the dropdown list).
Fill the inputs in the opened pop-up with the following values:
Gobal: (Checked) (This means all nodes in your graylog cluster will use this input as a source)
Broker virtual host: logging
Broker hostname: (Your rabbitmq host address)
Allow throttling this input: (checked) (This options usally used when you readed logs from file or message queue.)
Bind to exchange: (Checked) (This means graylog will bind the testapp-logs queue to your exchange automatically.)
Routing Key: #
As you can see the logs are started coming from RabbitMQ to Graylog.
You can find this example and all the Graylog/RabbitMQ setup from the following repository.
monologamqpexample - Example application for sending logs from symfony applications to graylog through AMQP
Thanks for reading!