[Python-ideas] Logging Levels

Mike Miller python-ideas at mgmiller.net
Tue Nov 28 14:25:35 EST 2017


Hi,

I use the logging module extensively for even the simplest scripts, one of the 
reasons there's been less difficulty moving to Python 3 at work.  One of the 
"nano features" I've often added to its config is the addition of a custom log 
level.  Never mentioned it before because of its triviality and minor use cases, 
but just realized that I've been doing it five years now and happy with it, so 
why not?

NOTE (~35)

It is called the "note" level and used when one needs to express something 
important, yet positive, and have it be output by default.  The standard levels 
don't support this currently, you often have to potentially scare the end user 
with a warning or higher to have a message emitted.

Typically I use it to return important information that was asked for 
specifically and retrieved successfully, e.g.:

     log.note('Your token is: %s', token)
     log.note(f'⏵ {item.id} {item.name}')

There are other examples.  Sphinx, has the concept of note admonitions for docs 
in addition to warning and danger.  Bootstrap has note banners for web/apps. 
There is something important to express or highlight, but nothing to worry about.


FATAL (alias of CRITICAL)

Can't find it now, but believe the docs in the past stated that CRITICAL was 
meant for *unrecoverable* errors.  I've never had a project where I didn't shut 
down immediately after such an occurrence.   Therefore I find "FATAL" a more 
accurate description of what happened.  Log4j and other industry loggers use 
this level name as well.

There is also an aesthetic reason to prefer "fatal".  It is shorter and so 
aligns better with other level names for readability, e.g.:

     console_format = '  %(levelname)-7.7s %(message)s'

Tried but never found a good abbreviation for critical, unfortunately.  The 
other option is to add length to align the field.  Most messages use the shorter 
level names (debug, info) so extra length results in wasted space that is very 
rarely needed.

Hopefully someone else finds these useful.  Neither depends on the other.

-Mike


More information about the Python-ideas mailing list