Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Logger From Dictionary

In this lesson, you’ll see how to do what you did in the last lesson, but this time, but with a dictionary approach. The most straightforward way to do this is with a YAML file for your configuration. Here’s the same configuration in a YAML format for the dictionary approach:

YAML
version: 1
formatters:
  simple:
    format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
  console:
    class: logging.StreamHandler
    level: DEBUG
    formatter: simple
    stream: ext://sys.stdout
loggers:
  sampleLogger:
    level: DEBUG
    handlers: [console]
    propagate: no
root:
  level: DEBUG
  handlers: [console]

Here’s an example that shows how to load config from a yaml file:

Python
import logging
import logging.config
import yaml

with open('config.yaml', 'r') as f:
    config = yaml.safe_load(f.read())
    logging.config.dictConfig(config)

logger = logging.getLogger(__name__)

logger.debug('This is a debug message')

Here’s what you’d get:

Shell
2018-07-13 14:05:03,766 - __main__ - DEBUG - This is a debug message

00:00 Let’s do the same thing, but with a dictionary approach. The easiest way to do this is by using a YAML file for our configuration. This works in a similar way as in the last video. First, we define a new formatter called simple, and we will supply it this formatting string right here.

00:21 Then we create a new handler called console, which will use the StreamHandler class and the stdout stream, a level of DEBUG, and the simple formatter we just defined.

00:33 Then we create a logger called sampleLogger with a level of DEBUG, handlers of [console], and no for propagation. Finally, we define the root logger with a level of DEBUG and the same console handler.

00:50 This file right here is saved as config.yaml.

00:55 I’ll jump into a new Python file and start coding the mechanism for creating our logger from this YAML file. I need to import three modules this time, logging, logging.config, and yaml.

01:09 If you don’t have yaml, you can grab it by running pip install PyYAML in a command line. Now, I can open this file in the standard Python way, with open('config.yaml') in read mode ('r') as f:. I’ll read the lines with config = yaml.safe_load(f.read()).

01:37 And now I’ll configure the logger with logging.config.dictConfig(), passing in the config information. I’ll create a new logger by typing logger = logging.getLogger(__name__).

01:54 I’ll use this logger by writing logger.debug(), passing in 'This is a debug message'.

02:03 And as usual, I will right-click here and choose Run Code, and we see that we get the exact same log output as in the last video.

Avatar image for Olya

Olya on April 27, 2021

Austin hello,

I have reviewed your course and have attention to implement assertion for logger, could it be used for existed logs?

Thank you in advance!

Olha

Become a Member to join the conversation.