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 Config File

Give Feedback

You can configure logging using the module and class functions or by creating a config file or a dictionary and loading it using fileConfig() or dictConfig() respectively. These are useful in case you want to change your logging configuration in a running application.

Here’s an example file configuration:







format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

In the above file, there are two loggers, one handler, and one formatter. After their names are defined, they are configured by adding the words logger, handler, and formatter before their names separated by an underscore.

00:00 Creating custom loggers within our code is great, but what if we want to save our configuration for use later on or share it with someone else? That’s where config files come into play.

00:13 We can create a config file and have our program create the logger based on that file. We can do that with the fileConfig() and dictConfig() functions.

00:24 Let’s see how that works. Here in VS Code, I have a basic config file written for a logger. This might look a little bit cryptic, but let’s walk through it to understand what it’s doing. First, we’re defining two loggers, one named root and the other named sampleLogger. Remember, root is the built-in logger, so this config file will configure both a custom logger and the built-in one.

00:52 Then, we define a new handler named consoleHandler and a formatter called sampleFormatter Now, the next two sections modify our loggers.

01:04 We can tell because the name starts with logger and then an underscore (_), and then the name of the logger. For the root logger, we assign it a level of DEBUG and a handler, consoleHandler. For the sampleLogger, we give it a level of DEBUG, the same consoleHandler as before, a qualname (qualified name) of sampleLogger, and a propagate value of 0, or False—something that doesn’t apply to this basic config file here.

01:35 The next section configures the consoleHandler. We give it a class of StreamHandler, which will allow it to write to stdout, a level of DEBUG, a formatter of sampleFormatter, and args of (sys.stdout,), short for standard out.

01:53 Finally, we modify the sampleFormatter by defining the string used to represent the log format, just like we’ve been doing. This file right here is saved as file.conf, or as I say, “conf”.

02:10 Now I’m going to move into a blank Python file, and we’re going to write a few lines of code that will configure a new logger from this file. First, I’ll import two modules, logging and logging.config.

02:26 Then, we need to read from the file. This can be done by calling the fileConfig() function within the logging.config module, passing in the filename of 'file.conf'. And I’ll set disable_existing_loggers to False. This way, if other loggers are being used, they won’t be disabled, which normally happens by default.

02:53 Now, we need to get the logger specified in the file. I’ll type logger = logging.getLogger() and I’ll pass in __name__.

03:04 And I will use this logger with logger.debug('This is a debug message').

03:12 I’ll right-click and run this program, and we see in the console on the right, we get the time and the date, the logger name of __main__, the level, and the debug message.

Become a Member to join the conversation.