Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logging multiple files using the same logger

This is my scenario: I want to log my_module's activity. This needs to be done, depending on the method executed (let's say, INPUT and OUTPUT), to two different files.

So I have two Handlers, each one point to a different file (my_in_.log & my_out_.log), with the same log level. I would like to know if I can use the same logger to achieve this or I have to define two loggers. My config is:

[loggers]
keys=root, my_log

[handlers]
keys=my_in_hand, my_out_hand

[formatters]
keys=generic_form


...


[logger_my_log]
level=NOTSET
handlers=my_in_hand, my_out_hand
qualname=ws_log

[handler_my_in_hand]
class=handlers.TimeRotatingFileHandler
level=NOTSET
formatter=generic_form
args=('my_in_.log', 'h', 1, 0, None, False, True)

[handler_my_out_hand]
class=handlers.TimeRotatingFileHandler
level=NOTSET
formatter=generic_form
args=('my_out_.log', 'h', 1, 0, None, False, True)

Do I have to define a logger per handler/destination (because I want to log different information in different files)? Is there a way to indicate to the logger which handler will do this? I mean, I have two handlers for one logger, then choose only one handler to log one method.

like image 587
Alberto Megía Avatar asked Mar 04 '13 10:03

Alberto Megía


People also ask

How do I create a multiple logging level in Python?

You can set a different logging level for each logging handler but it seems you will have to set the logger's level to the "lowest". In the example below I set the logger to DEBUG, the stream handler to INFO and the TimedRotatingFileHandler to DEBUG. So the file has DEBUG entries and the stream outputs only INFO.

Is Python logging using log4j?

log4j is a popular logging package written in Java. log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.


1 Answers

You should instantiate an Handler for each destination you want to send your log to, then add the 2 handlers to your logger. The following should work (didn't test it though):

logger = logging.getLogger()
handler1 = logging.TimedRotatingFileHandler()
handler2 = logging.TimedRotatingFileHandler()
logger.addHandler(handler1)
logger.addHandler(handler2)

Of course add all your configuration and formatting options you may need. Basically it is just to show you that when you instantiate the logging handler you can add it to the logger. From that moment on, your log records will be emitted to every handler added to the logger.

like image 81
drekyn Avatar answered Sep 29 '22 13:09

drekyn