8

I am working on a project with python and I need to help about logging part. My logger have two handler(syslog and file log) and it send logs to both of them. Sometimes I need to send logs only one of them. How can I choose handler to be used? Thanks for help...

global my_Sysloghandler
global my_logger, my_log_handler

my_Sysloghandler=logging.handlers.SysLogHandler()
my_log_handler= logging.FileHandler('/var/log/{0}.log'.format(__project__))
my_log_handler.setFormatter(logging.Formatter('%(asctime)s %(message)s'))                                                                              
my_log_handler.setLevel(level)
my_logger= logging.getLogger('my_logger')
my_logger.addHandler(my_log_handler)
my_logger.addHandler(my_Sysloghandler)

my_logger.debug('This log is sent to both handler but I want to send it only my_logger')
1
  • Your code extract is incomplete. What is the value of level? Set the level of my_log_handler to DEBUG and the level of my_Sysloghandler to INFO and as a result everything logged as DEBUG will ONLY be logged to your my_log_handler. Commented Dec 10, 2011 at 8:23

1 Answer 1

7

I think your choices are:

  • Prioritize one over the other (i.e. SysLogHandler gets INFO messages or above, while FileHandler gets DEBUG or above)
  • Use two different logger instances.

FWIW, the regular logging docs can be tough to read through. Instead, take a look at the Logging HOWTO and Logging Cookbook for something more easily digestible.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.