Logger In python – First Approach
Besides using the print statements and the debugging tools sometimes (more and more frequently) I’m seeing in the code the logging module. According to the python documentation:
This module defines functions and classes which implement a flexible event logging system for applications and libraries.
The key benefit of having the logging API provided by a standard library module is that all Python modules can participate in logging, so your application log can include your own messages integrated with messages from third-party modules.
https://docs.python.org/3/library/logging.html#module-logging
So you can create a simple logging warning with the following scrip:
import logging
logging.warning("Ey, this is a logging warning")
You’ll see the warning output
WARNING:root:Ey, this is a logging warning
The logging module has different values for the most commons events:
Level | Numeric value |
---|---|
CRITICAL | 50 |
ERROR | 40 |
WARNING | 30 |
INFO | 20 |
DEBUG | 10 |
NOTSET | 0 |
If you are wondering the possibility to add color to your messages you can see this stack overflow question with a lot of comments: How can I color Python logging output?
Sources: