Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define channels and Levels in monolog logging in symfony2

Is there any way that i can define my custom levels in monolog in symfony2.

I i do this

$logger->err('An error occurred');

Then in the database i have this added.

The channel is app and level is 500

Is there any way to do this

$logger->log("message",(channel),(level)
$logger->log("Object with is 212 deleted",'DELETE',NORMAL);

So that i can have separate things in database for reporting and viewing

like image 494
Mirage Avatar asked Aug 14 '12 04:08

Mirage


People also ask

What are the different levels of logs we can have in monolog?

Monolog log levels Monolog has the following log levels: DEBUG - detailed debug information. INFO - interesting events. NOTICE - normal but significant events.

What are Monolog channels?

Generally you might need to classify your log files for different services or modules. For this purpose, Monolog allows you to create different channels, where each can log separately to different file and allow you to configure your log per channel.

What is monolog Symfony?

Monolog is the existing standard logging library for PHP. It is most popular in PHP frameworks such as Laravel and Symfony, where it implements a common interface for logging libraries. This article talks about the step-by-step process of using PHP Monolog in your application.

Where are Symfony logs?

By default, Symfony logs are stored in var/log/dev. log and var/log/prod. log within the project directory, depending on the environment, but these defaults can be changed in the Monolog package configuration file found at config/packages/monolog.


1 Answers

With version 2.4 and up (beware, the release cycle of the MonologBundle is not syncronized with symfony anymore) of the MonologBundle, you can now define new channels very simple via configuration, without defining services.

monolog:
    channels: ["my_channel"]

Now simply get the automatically created logger for the new channel in your controller:

$logger = $this->get('monolog.logger.my_channel');
$logger->info('somelogcontent');

The message level is defined trough the use of the appropriate method. Take a look into the LoggerInterface to see all logging methods (which are indeed implemented by monolog). Here some levels to be mentioned:

$logger->info('Info message for interesting things');
$logger->warning('Some application warnings, but the application works');
$logger->error('Error which can influence the application flow/output');

I know old question, but this new feature from the MonologBundle ~2.4 should be mentioned.

like image 111
Emii Khaos Avatar answered Oct 05 '22 11:10

Emii Khaos