We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Django Logging Demystified with Lee Trout - DjangoCon US 2022
Django logging demystified: learn how to harness the power of structured logging, where, when, and how; from log record construction to log file rotation, and beyond.
- Structured logging is key to demystifying Django logging. It’s about adding explicit context to log messages, making them easier to understand.
-
Python’s
logging
module is essential to understanding Django logging. It’s a complex system with many components, including loggers, handlers, filters, and formatters. - Loggers are hierarchical, with the root logger at the top. Each logger can have a level, filters, and handlers.
- Handlers control where log messages are sent, such as a file, console, or email.
- Filters determine whether log messages are processed by handlers.
- Formatters control the format of log messages.
- Levels (CRITICAL, ERROR, WARNING, INFO, DEBUG) determine the severity of log messages.
- Effective logging requires understanding how all these components interact.
- Structured logging can be achieved using typed dictionaries, data classes, or serialized log records.
- Django comes with a default logging configuration, but it’s not inheriting settings from other apps.
-
The
make_record
helper can be used to create a log record. - Loggers can be bound to a specific context, making it easier to log related events.
- Rotation and filtering are important concepts in logging configuration.
-
The
logging
module’sextra
keyword can be used to add custom data to log records. - Logging is essential for testing and debugging, as it allows you to inspect and understand system behavior.
- Effective logging also requires a philosophy of logging, including considering logging as a form of asynchronous debugging.
- Log records can be used to generate metrics and tracing information.
- Structured logging enables more advanced features like log aggregation and anomaly detection.
-
Python’s
logging
module has many built-in features and tools to help with logging configuration. -
The
logging.config
module can be used to configure logging programmatically. - Rotating log files is an important consideration in logging configuration.
-
The
logging.handlers
module provides various handlers, such asRotatingFileHandler
andSMTPHandler
. - Django’s server formatter can be customized to include additional information in log messages.
- Effective logging requires understanding of how logging interacts with other components, such as the ORM and signals.
- Structured logging can be used to generate log entries in a specific format.
- PyLint can be used to lint log messages and enforce logging best practices.
- APM (Application Performance Monitoring) tools can be used to instrument and observe application behavior.
- Structured logging can be used to generate metrics and tracing information.
- Effective logging requires a culture of logging, including making it a priority and considering it a form of testing.