WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Python · Advanced · question 45 of 100

How do you use Python’s logging module for application logging? Explain different logging levels and handlers.?

📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

Python’s logging module provides a flexible and customizable way to log messages from your application. It allows you to define loggers, handlers, and formatters to customize the way your logs are recorded, stored, and displayed.

Here’s a brief overview of the main components of the logging module:

The logging module provides several logging levels to categorize your log messages according to their severity or importance. The most commonly used logging levels are:

Here’s an example of how to use the logging module to log messages in your application:

    import logging
    
    # Create a logger with the name 'myapp'
    logger = logging.getLogger('myapp')
    logger.setLevel(logging.DEBUG)
    
    # Create a file handler and set its level to INFO
    fh = logging.FileHandler('myapp.log')
    fh.setLevel(logging.INFO)
    
    # Create a console handler and set its level to DEBUG
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    
    # Create a formatter and set it on the handlers
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    
    # Add the handlers to the logger
    logger.addHandler(fh)
    logger.addHandler(ch)
    
    # Log some messages at different levels
    logger.debug('Debugging message')
    logger.info('Informational message')
    logger.warning('Warning message')
    logger.error('Error message')
    logger.critical('Critical error message')

In this example, we create a logger with the name ’myapp’ and set its logging level to DEBUG. We then create a file handler and a console handler, each with its own logging level, and set a formatter on both handlers. Finally, we add the handlers to the logger.

We can then log messages at different levels using the logger object, and these messages will be handled according to the logger’s settings.

The logging module also provides several built-in handlers, such as RotatingFileHandler, TimedRotatingFileHandler, SMTPHandler, and SysLogHandler, that can be used to send logs to different destinations. You can also define your own custom handlers if necessary.

Overall, the logging module provides a powerful and flexible way to log messages in your Python application, and its customization options make it suitable for a wide range of use cases.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Python interview — then scores it.
📞 Practice Python — free 15 min
📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

All 100 Python questions · All topics