python: logging

While one can use if-else statements, the use of logging and raising errors (raise) can be a better way to handle errors and warnings in complex projects for the following reasons:

  • Informative – You can provide more information by using various levels (debug, info, warning, error, critical)
import logging
logger = logging.getLogger(__name__)

logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")
logger.critical("This is a critical message")
  • Error Output – Log messages can be printed on a console, in a file, and used to trigger notification if critical
import logging
logging.basicConfig(filename='data_proc.log', level=logging.INFO)
logger = logging.getLogger(__name__)

logger.info("this will be stored in data_proc.log file!")
  • Customize Error Message – message can be customized with the logging attributes
import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

logger.warning("check out the additional info")

# output
2024-09-17 10:34:21,597 - __main__ - WARNING - check out the additional info
  • Configure and control logger – logger can be configured so the log size can be controlled
from logging.handlers import RotatingFileHandler

# if data_proc.log reaches maxBytes, it will be archived in 
# data_proc.log1 .. all the way to data_proc.log.5
# When it reaches backupCount, then it starts again from 
# data_proc.log.1 replacing the previous log

handler = RotatingFileHandler('data_proc.log', maxBytes=1000, backupCount=5)
logger.addHandler(handler)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply