Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

logging - How to write logs from one service into separate file?

Normally you just get logger service, and logs go to:

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

I would like to log messages form SOAP services ONLY to:

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

not to main logfile.

I've read the cookbook, but I don't understand how to configure monolog.

Any help, clues?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The MonologBundle logs everything using the same handlers for the whole framework. That means if one of your services needs to log to different handlers, you should create your own Logger/Handler and inject that in your service.

This could be an example config (in yaml):

services:
    my_logger:
        class: SymfonyBridgeMonologLogger
        arguments: [soap]
        calls:
            - [pushHandler, [@my_handler]]

    my_handler:
        class: MonologHandlerStreamHandler
        # 200 = INFO, see Monolog::Logger for the values of log levels
        arguments: [%kernel.root_dir%/%kernel.environment%.soap.log, 200]

    soap_service:
        class: YourSoapClient
        arguments: [@my_logger]

I hope this clarifies it.

Update: as of symfony 2.1, you can also configure which channels receive which handlers, so you could alternatively do something like this:

services:
     soap_service:
         class: YourSoapClient
         arguments: [@logger]
         tags:
             - { name: monolog.logger, channel: soap }

Which creates a new soap channel (i.e. logger instance receiving all handlers), then to 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 the main handler will receive everything but the soap channel, and the soap handler will receive only the soap channel. You could 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 the soap logs separately. This brings a lot of flexibility, and as you see the channels is an array so you can list channels you want, or use the blacklist !name notation to exclude some and include everything else.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...