Ankur Agarwal
Ankur Agarwal

Reputation: 24768

Logging with Python - how to change the display name of custom level

This is what I have:

>>> MYLEVEL=15
>>> import logging
>>> lo=logging.Logger("abc")
>>> lo.setLevel(MYLEVEL)
>>> 
>>> ch=logging.StreamHandler()
>>> ch.setLevel(MYLEVEL)
>>> 
>>> ff = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
>>> 
>>> ch.setFormatter(ff)
>>> lo.addHandler(ch)
>>> lo.log(MYLEVEL, "abcdef")
2013-10-07 21:24:25,652 - abc - Level 15 - abcdef # How would I replace "Level 15" here by MYLEVEL

Question:

How would I replace "Level 15" in the last line of o/p with MYLEVEL?

Upvotes: 0

Views: 2783

Answers (1)

falsetru
falsetru

Reputation: 369224

Use logging.addLevelName:

>>> import logging
>>> MYLEVEL = 15
>>> logging.addLevelName(MYLEVEL, 'MYLEVEL')
>>> logging.basicConfig(level=0, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
>>> logging.log(MYLEVEL, "ABCDEF")
2013-10-08 13:33:26,950 - root - MYLEVEL - ABCDEF

Upvotes: 2

Related Questions