Colab

setup

  1. import the logging module
  2. call .basicConfig()
  3. setup autoreload to help reloading .py files from disk
### useful: please run this
import logging
logging.basicConfig()

%load_ext autoreload
%autoreload 2

Basic

  1. import
  2. Create a new python file named ex1.py which should:
    1. Import the logging module
    2. create a logger instance named __name__
    3. Create a function called say_something() that logs a ‘warning’ message with the text: "This a warning message!".
  3. use the file
    1. import ex1
    2. call ex1.say_something()
### useful: run your ex1.py
import ex1
ex1.say_something()

Log level

  1. Create a new python file named ‘ex2.py‘ which should:
    1. Import the logging module
    2. create a logger instance named __name__
    3. set the logger’s level to logging.DEBUG
    4. create a function called say_something() which logs an ‘info’ message with the text: “This an informative message!”.
  2. use this file
    1. import ex2
    2. call ex2.say_something()
### useful: run your ex2.py
import ex2
ex2.say_something()

Configuration

  1. setup
    1. change the level of ex2’s logger to DEBUG
    2. call ex2.say_something() - this should write the usual output

  2. Review this basic YAML configuration, and see that you understand how it will:
    1. set a simple format that is the same as the default format
    2. set a handler that logs to console
    3. connect the root logger to the console handler, with level WARNING
    4. modify the level for the ex2 logger to ERROR
      version: 1
      disable_existing_loggers: False
      formatters:
        simple:
          format: '%(levelname)s:%(name)s:%(message)s'
      handlers:
        console:
          class: logging.StreamHandler
          formatter: simple
          stream: ext://sys.stderr
      loggers:
        ex2:
          level: ERROR
      root:
        level: WARNING
        handlers: [console]
      



  3. load this configuration
    1. import the yaml module
    2. use yaml.load() to read this configuration into a dict object
    3. import logging.config module
    4. use logging.config.dictConfig() function to load the configuration from your dict


  4. execute ex2.say_something() again. this time there should not be any output. why?

Format #1

Copy the YAML configuration from the previous question, and change the formatter so that output from calling ex1.say_something() will look like this 2019-12-26 03:07:04,560 | WARNING | ex1 | this is a warning message

hints:

  • Read the LogRecord documentaion, which shows the attributes available for formatting

Format #2

Copy the YAML configuration from the previous question, and change the formatter so that output from calling ex1.say_something() will look like this:

2019-12-26 03:10:19,852 :: WARNING :: Module ex1 :: Line No 6 :: this is a warning message

file handler

  • Use YAML configuration to
    1. set the levels of both ex1 and ex2 loggers to DEBUG
    2. setup file logging for ex2 logger so that it writes to ex2.log
  • test this by calling
    ex1.say_something()
    ex2.say_something()
    

    and reading ex2.log