Logging levels in Python have numeric values, by which log entries are assigned an order of importance. Some commonly used levels are also assigned human-readable names.
| Level | Numeric Value |
|---|---|
| CRITICAL | 50 |
| ERROR | 40 |
| WARNING | 30 |
| INFO | 20 |
| DEBUG | 10 |
| NOTSET | 0 |
Looking at this table, it seems this naming is done in order of error severity. This matches, for example, Windows severity levels (Critical, Error, Warning, Information, Verbose)
However logging as a concept does not seem to be limited to error logging. The description of the logging tag on Stackoverflow itself, for example, is:
Computer data logging is the process of recording events in a computer program or computer system, usually with a certain scope, in order to provide an audit trail that can be used to understand the activity of the system and to diagnose problems.
Errors, while important, are not the only important events. When using logging for a complicated and long-running process, successful outcomes can also be important events to know about and record in the log. This leads me to wonder why there is no logging.SUCCESS level that is assigned a value above, say, mere warnings (logging.WARNING).
Specifically, I have a long-running process where I would not switch on Debug or Info logs in production, but knowing that task identifier x was successfully completed has more value than knowing that subprocess y had a minor recoverable error.
What is the pythonic way to log an interim successful event that is an important milestone in code execution?
(Or is logging.INFO itself the correct way to log success?)
logging built-in module motivation is described in PEP 282, which regarding levels after enumerating them says:
Although the above levels are strongly recommended, the logging system should not be prescriptive. Users may define their own levels, as well as the textual representation of any levels. User defined levels must, however, obey the constraints that they are all positive integers and that they increase in order of increasing severity. User-defined logging levels are supported through two module-level functions:
def getLevelName(lvl):
"""Return the text for level 'lvl'."""
...
def addLevelName(lvl, lvlName):
"""
Add the level 'lvl' with associated text 'levelName', or
set the textual representation of existing level 'lvl' to be
'lvlName'."""
...
Therefore I suggest firstly to carefully consider if one of already defined can not be used. If it can not then use logging.addLevelName function for creating custom level.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With