Logging can be beneficial if set up correctly. It can help developers to debug if something goes wrong in production.
1️⃣ Here, we are gonna use the logging package in Python.
import logging
logging.basicConfig(level=logging.WARNING)
# 5 levels of severity
logging.debug('Debug message')
logging.info('Info message')
logging.warning('Warning message')
logging.error('Error message')
logging.critical('Critical message')
with basicConfig
, we can set the base logging level that we wanna see in our logs. For example, if level is set to WARNING
, our logs will display messages for warning, error & critical
.
2️⃣ A more advanced example of logging config:
logging.basicConfig(
level=logging.WARNING,
format="%(asctime)s %(levelname)s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
filename="logs.log",
)
One last note: NEVER PRINT SENSITIVE DATA INTO YOUR LOGS