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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Logger From Dictionary

Give Feedback

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:

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

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

import logging
import logging.config
import yaml

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

logger = logging.getLogger(__name__)

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

Here’s what you’d get:

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(

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.

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!


Become a Member to join the conversation.