logging


logging means tracking events when a software runs we add logs to show an event occurred
there are several levels of logging depending on severity include;
LEVEL | when it is used |
DEBUG | Provide detailed information when diagnosing |
INFO | Confirmation that things worked well |
WARNING | something unexpected happened that might affect you in future like low storage but software runs |
ERROR | serious problem, causing some specific functionanlity in the program to halt |
CRITICAL | more serious error, causing the entire program to crash totally |
logging methods are named after levels of severity mentioned above including logging.debug(), logging.info(), logging.warning(), logging.error()
logging is a build-in module in python.
import logging
logging.warning("watch out!")
logging.info("I told you")
logging to file
import logging
logging.basicConfig(filename="my_log", level=logging.DEBUG,
format="%(asctime)s: %(levelname)s: %(message)s")
you can access logging functionality by creating a logger
import logging
logger = getLogger(__name__) # creating a logger to access logging methods
logging.basicConfig(filename="my_log", level=logging.DEBUG,
format="%(asctime)s: %(levelname)s: %(message)s")
logger.debug("this message should appear on console")
logger.info("this message should appear on console")
logger.warning("even this message should appear on console")
Subscribe to my newsletter
Read articles from sammy directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

sammy
sammy
I am an aspiring software engineer.By writing this article.I am commit myself accountable to provide valuable content to you reader oftenly every weekend about amazing programming concepts and technologies.To help you in your learning journey also.