1

The default Numeric values of logging levels is :

enter image description here

I need to change this values and set the DEBUG value to 20 and INFO to 10

Please let me know how to do it.

At present this is my code :

log =logging.getLogger(__name__) log.setLevel(logging.DEBUG) formatter =logging.Formatter('[%(module)s](%(lineno)d) [%(levelname)s] : %(message)s') file_handler = logging.FileHandler('debug.log', mode='w') file_handler.setFormatter(formatter) log.addHandler(file_handler) stream_handler = logging.StreamHandler() stream_handler.setFormatter(formatter) log.addHandler(stream_handler) log.warning('This is a warning') log.error('This is an error') log.info('This is an INFO') log.debug('This is an DEBUG') log.critical('This is an CRITICAL') 
3
  • Why do you need to do this? Commented Aug 16, 2020 at 5:49
  • I need to put only debug messages in a file not other messages. In the present scenario, in case I want to do it, I will have to set the level to "DEBUG" for file handler. If I do that INFO messages also go in the debug.log Commented Aug 16, 2020 at 5:51
  • Okay so what you are really asking is how to set up a handler which is only for DEBUG messages, rather than everything DEBUG and higher. I'm afraid I don't know the answer, but it is not self-evident that trying to tamper with the symbolic constants will be the way to implement this, so I suggest that you edit the question to reflect what you are actually trying to achieve (and have just said in the comments), and leave it to people to suggest the most appropriate implementation. Commented Aug 16, 2020 at 5:57

2 Answers 2

1

You can use a filter on the handler instead which can select messages with a specific level.

See logging with filters

The body of the filter method on the Filter subclass in this case would be:

return record.getLevel() == logging.DEBUG 
Sign up to request clarification or add additional context in comments.

Comments

0

This is how I solved

I created a filter like this :

class debugFilter(logging.Filter): def filter(self, record): if (record.levelno == 10): return True else: return False 

and main code :

file_handler = logging.FileHandler('debug.log', mode='w') file_handler.addFilter(debugFilter()) file_handler.setFormatter(formatter) log.addHandler(file_handler) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.