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

Usually you get logger service, and logs:

 %kernel.root_dir%/%kernel.environment%.log 

I would like to log messages from SOAP services ONLY :

 %kernel.root_dir%/%kernel.environment%.soap.log 

not for the main log file.

I read a cookbook, but I don’t understand how to set up a monologue.

Any help, hints?

+53
logging symfony monolog
Nov 17 '11 at 14:50
source share
3 answers

MonologBundle records everything using the same handlers for the entire structure. This means that if one of your services needs to connect to various handlers, you must create your own Logger / Handler and enter it into your service.

This could be a config example (in yaml):

 services: my_logger: class: Symfony\Bridge\Monolog\Logger arguments: [soap] calls: - [pushHandler, [@my_handler]] my_handler: class: Monolog\Handler\StreamHandler # 200 = INFO, see Monolog::Logger for the values of log levels arguments: [%kernel.root_dir%/%kernel.environment%.soap.log, 200] soap_service: class: Your\Soap\Client arguments: [@my_logger] 

Hope this clarifies this.

Update: as in Symfony 2.1, you can also configure which channels these handlers will receive so you can do something like this:

 services: soap_service: class: Your\Soap\Client arguments: [@logger] tags: - { name: monolog.logger, channel: soap } 

What creates a new soap channel (i.e., a registrar instance that receives all the handlers), and then configure different handlers for this channel:

 monolog: handlers: main: type: stream path: %kernel.root_dir%/%kernel.environment%.log level: error channels: [!soap] soap: type: stream path: %kernel.root_dir%/%kernel.environment%.soap.log level: info channels: [soap] 

This means that the main processor will receive everything except the soap channel, and the soap processor will receive only the soap channel. You can also remove the channels key on the main handler if you want your main log file to have everything, but also have a copy of only soap logs separately. This brings great flexibility, and as you can see, the channels are an array, so you can list the channels you need or use the !name blacklist notation to exclude some of them and include everything else.

+73
Nov 21 '11 at 11:16
source share
β€” -

I had a similar problem and decided to use the Monolog library directly instead of the Monolog service.

The monologue uses Monolog\Handler\StreamHandler to write to files. The github page has a simple example:

 use Monolog\Logger; use Monolog\Handler\StreamHandler; // create a log channel $log = new Logger('name'); $log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING)); // add records to the log $log->addWarning('Foo'); $log->addError('Bar'); 

You may be able to use the service and just click on the new handler (and pull it out as soon as you finish), otherwise you may accidentally write more than you would like your own journal to do), but I have not tested this, Honestly, it was easier to just use the library directly.

+27
Nov 17 '11 at 15:46
source share

I solved the same problem by creating custom channels in config.yml as described in this link How to write messages to different files .

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

After that, I can access my looger with a dynamically created service named monolog.logger.my_logger

+5
Aug 10 '16 at 14:22
source share



All Articles