Python Logging - Tutorial

Share

Summary

An introduction to Python's built-in logging module, covering log levels, configuration, writing logs to files, formatting messages, logging exceptions, and creating custom loggers with handlers.

Highlights

Introduction and Log Levels00:00:02

The video begins by contrasting print debugging with persistent logging. It introduces Python's logging module and explains the five log levels: debug, info, warning, error, and critical, noting that by default only warning and above are output to the console.

Basic Configuration and File Logging00:01:32

The tutorial demonstrates how to import the logging module and use basicConfig to set the log level and direct output to a file using filename and filemode arguments, explaining that this configuration only runs once at the start of the program.

Custom Formatting00:05:47

The presenter shows how to change the format of logged messages using the format argument, adding attributes like ASC time for human-readable timestamps, levelname, and message to the log output.

Logging Variables and Exceptions00:07:35

This section covers how to log variable values using f-strings and how to log stack traces for exceptions using either exe info equals true within an error log or the dedicated exception method.

Custom Loggers and Handlers00:10:14

The final part explains creating custom loggers with getLogger to separate logs into different files, and configuring them using FileHandler and Formatter objects for more granular control over logging in larger projects.

Recently Summarized Articles

Loading...
Summarize another YouTube video →