Custom monologue registration channel in symfony2 command

In this article, a cookbook article, we see how to use a custom channel in a service. But how can I use a custom input channel in a team?

I created a symfony2 command to execute something. I would like to use a monologue to record things done by my team.

Actually, I want to write the log for my command to a file other than the application logs.

+15
php symfony
Oct 10 2018-11-15T00:
source share
5 answers

A similar question was asked and answered here:

How to write logs from one service to a separate file?

thank

+4
Nov 21 '11 at 12:54
source share

Any user command that extends ContainerAwareCommand has access to the Symfony service container. You can define a service that registers with a custom channel in your configuration.

 <services> <service id="console.logger" parent="monolog.logger_prototype"> <argument index="0">mychannel</argument> </service> </services> 

You can access your service from a team as follows

 $logger = $this->getContainer()->get('console.logger'); 

This registrar will register with the channel as "mychannel".

FYI The default log service logs for the application channel. This can be seen in the file Symfony/Bundle/MonologBundle/Resources/config/monolog.xml . This is also the place where the default logger service is defined.

 <services> <service id="monolog.logger" parent="monolog.logger_prototype" public="false"> <argument index="0">app</argument> </service> <service id="logger" alias="monolog.logger" /> <service id="monolog.logger_prototype" class="%monolog.logger.class%" abstract="true"> <argument /><!-- Channel --> </service> </services> 
+6
Jan 23 '13 at 6:26
source share

Try this, use the library directly

 use Monolog\Logger; use Monolog\Handler\StreamHandler; // ... // create a log channel $log = new Logger('name'); $log->pushHandler(new StreamHandler('path of log file', Logger::WARNING)); //add the erros to log file try { //do something } catch(Exception $e) { $log->addError($e->getMessage()); } 

Perhaps this may solve your problem, add this to your batch file.

+4
Jul 19 '13 at 9:00 a.m.
source share

In most cases, your team extends ContainerAwareCommand (as shown here: http://symfony.com/doc/current/cookbook/console.html#creating-a-basic-command ).

This means that your team has access to the Symfony service container - which is a large package of all services inside Symfony (i.e. useful objects). In your case, you will need the logger service, which you can get by extracting it from the container:

 $logger = $this->getContainer()->get('logger'); 

(link: http://symfony.com/doc/current/cookbook/console.html#getting-services-from-the-service-container )

Now you can use the registrar as usual. If you need any other services, just check the php app/console container:debug command, which lists all the services in the container.

Good luck

0
Oct 10 '11 at 3:30 p.m.
source share

For Symfony 3.3, because of autowire services, when you want to enter a separate file, follow this approach:

For example, suppose we need to register 2 rotating files:

  • exception.log : for exceptions thrown by the application.
  • dataFetch.log : for api calls, tha retrieves application data.

On services.yml put:

 monolog: channels: ['dataFetch', 'exception'] handlers: dataFetch: type: 'rotating_file' level: info type: stream path: 'dataFetch.log' max_files: 7 channels: dataFetch exception: type: 'rotating_file' level: error type: stream path: 'exception.log' max_files: 7 channels: exception 

Now for the exception logger you can insert the monolog.logger.exception declaration monolog.logger.exception , and for dataFetch you can inject monolog.logger.dataFetch injection.

0
Nov 17 '17 at 10:24
source share



All Articles