Waffle-Switched Logging

Smarter logging provides granular control over logging output at run-time using Waffle switches. This allows developers to enable or disable logging for specific components without changing the code or redeploying the application.

Usage

from smarter.lib.django import waffle
from smarter.lib.django.waffle import SmarterWaffleSwitches
from smarter.lib.logging import WaffleSwitchedLoggerWrapper

def should_log(level):
    """Check if logging should be done based on the waffle switch."""
    return waffle.switch_is_active(SmarterWaffleSwitches.ACCOUNT_LOGGING) and level >= smarter_settings.log_level

base_logger = logging.getLogger(__name__)
logger = WaffleSwitchedLoggerWrapper(base_logger, should_log)

logger.debug("This is a debug message.")
logger.info("This is an info message.")
logger.warning("This is a warning message.")
logger.error("This is an error message.")
logger.critical("This is a critical message.")

Waffle Switches

Smarter Waffle Switches

WaffleSwitchedLoggerWrapper Class Reference

class smarter.lib.logging.waffle_switched_logger.WaffleSwitchedLoggerWrapper(logger, condition_func=None)[source]

Bases: object

A wrapper around a standard logger that adds conditional logic.

Usage:
import logging

from smarter.lib.django import waffle
from smarter.lib.django.waffle import SmarterWaffleSwitches
from smarter.lib.logging import WaffleSwitchedLoggerWrapper

def should_log_detailed(level):
    return waffle.switch_is_active(SmarterWaffleSwitches.PROMPT_LOGGING)

base_logger = logging.getLogger(__name__)
logger = WaffleSwitchedLoggerWrapper(base_logger, should_log_detailed)

logger.debug("This is a debug message.")
REQUIRED_LOG_LEVEL = 30
__init__(logger, condition_func=None)[source]
critical(msg, *args, **kwargs)[source]
debug(msg, *args, **kwargs)[source]
error(msg, *args, **kwargs)[source]
info(msg, *args, **kwargs)[source]
set_condition(condition_func)[source]

Update the condition function.

should_log(level=10)[source]

Check if we should log based on custom conditions.

Return type:

bool

warning(msg, *args, **kwargs)[source]