This example demonstrates how to use Reforge's LoggerFilter to dynamically control log levels for standard Python logging.
- Dynamic log level control through Reforge configuration
- No application restart required to change log levels
- Works with standard Python
loggingmodule - Can target specific loggers by name using context-based rules
-
Install dependencies:
poetry install --no-root
-
Set your Reforge SDK key (or use local-only mode as in the example):
export REFORGE_SDK_KEY=your-sdk-key-here -
Create a log level config in your Reforge dashboard:
- Config key:
log-levels.default - Type:
LOG_LEVEL_V2 - Default value:
INFO(or your preferred level)
- Config key:
poetry run python standard_logging_example.pyThe example will log messages at all levels (DEBUG, INFO, WARNING, ERROR) every second.
While the example is running, you can:
- Change the log level in your Reforge dashboard
- See the output change in real-time without restarting the application
- Use context-based rules to set different levels for different loggers
For example, you could create rules like:
- Set
DEBUGlevel forreforge.python.*loggers - Set
ERRORlevel for all other loggers - Set
INFOlevel during business hours,DEBUGlevel at night
The LoggerFilter integrates with Python's standard logging by:
- Implementing the
logging.Filterinterface - Querying Reforge config for the log level on each log message
- Returning
True(allow) orFalse(block) based on the configured level - Using a context containing the logger name for targeted rules
You can customize the logger name lookup by subclassing LoggerFilter:
class CustomLoggerFilter(LoggerFilter):
def logger_name(self, record: logging.LogRecord) -> str:
# Custom logic to derive logger name
return record.name.replace("myapp", "mycompany")