logging

sammysammy
1 min read

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;

LEVELwhen it is used
DEBUGProvide detailed information when diagnosing
INFOConfirmation that things worked well
WARNINGsomething unexpected happened that might affect you in future like low storage but software runs
ERRORserious problem, causing some specific functionanlity in the program to halt
CRITICALmore 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")
0
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.