Logging Infrastructure in Python
Posted By : Rohitesh Rawat | 29-Jun-2018
We use 'print' to print a statement but it just prints to the console.
Logging allows us to instruct the program to emit information while the system is still running.
This information can be useful to us.
>>>Benefits of logging infrastructure are:
1- Multi-threading support.
2- Categorization via different levels of logging.
3-Flexibility and
4- Separation of the how from the what.
>>>Logging levels
Some of the logging levels are:
1- DEBUG: it is used when we are debugging something.
When want to write some detailed instructions on an issue that only you want to see when debugging and no other person should see that.
2- INFO: it confirms that all the things are working as expected.
3- WARNING: it indicates that something unexpected happened or may happen in the future, but the software is still working as expected.
4- ERROR: it indicates a serious problem.
The software may not able to perform some action.
>>>Logging Format: it changes the format of log messages that are displayed in the console.
import logging
logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p',level=logging.DEBUG)
logging.warning("warning message")
logging.info("info message")
logging.error("error message")
>>>Output will be something like this:
06/03/2018 11:12:32 PM: WARNING: warning message
06/03/2018 11:12:32 PM: INFO: info message
06/03/2018 11:12:32 PM: ERROR: error message
>>>Logger Console Example:
import logging
class LoggerConsole():
def testLog(self):
# create logger
logger = logging.getLogger(LoggerDemoConsole.__name__)
logger.setLevel(logging.INFO)
# create console handler and set level to info
consoleHandler = logging.StreamHandler()
consoleHandler.setLevel(logging.INFO)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s: %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p')
# add formatter to console handler
consoleHandler.setFormatter(formatter)
# add console handler to logger
logger.addHandler(consoleHandler)
# logging messages
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
demo = LoggerConsole()
demo.testLog()
>>>Output will be something like this:
06/03/2018 11:12:32 PM -sample_log - INFO: info message
06/03/2018 11:12:32 PM -sample_log - WARNING: warning message
06/03/2018 11:12:32 PM -sample_log - ERROR: error message
06/03/2018 11:12:32 PM -sample_log - CRITICAL: critical message
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rohitesh Rawat
Rohitesh is an expert in Agile methodologies, specializing in Scrum. He possesses a wide range of skills, including proficiency in Jira, MongoDB, planning, scoping, process creation and management, and QA. Over the years, he has led the successful delivery of several offshore projects, including Konfer, Virgin Media, HP1T, and Transleqo. Rohitesh holds certifications as a Certified Scrum Master (CSM) and Project Management Professional (PMP) and has a comprehensive understanding of the entire Project Life Cycle (PLC).