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

WaffleSwitchedLoggerWrapper Class Reference
- class smarter.lib.logging.waffle_switched_logger.WaffleSwitchedLoggerWrapper(logger, condition_func=None)[source]
Bases:
objectA 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.")
- Parameters:
- REQUIRED_LOG_LEVEL = 30
Log entries are always emitted at this level and above, whatever the condition says.
- _log(level, msg, args, kwargs, prefix_format=None)[source]
Emit a record, starting with the caller’s name, if the condition allows it.
Only the public logging methods call this, so the caller is
CALLER_DEPTHframes up, plus however many frames astacklevelargument skips. The wrapped logger is given the samestacklevel, adjusted for the wrapper’s own two frames, so a record’sfuncNameandlinenoare the caller’s.- Parameters:
level (
int) – The logging level, e.g.logging.INFO.msg (
Any) – The message, with %-style placeholders.args (
tuple) – The message’s arguments.kwargs (
dict) – Passed to the wrapped logger, e.g.exc_infoorextra.prefix_format (
str|None) – An ANSI format code for the caller’s name, e.g.SmarterFormattedText.BOLD_DARK_BLUE, or None for the usual prefix color.
- Return type:
- critical(msg, *args, prefix_format=None, **kwargs)[source]
Log at CRITICAL if the condition allows it, or the level is at least
REQUIRED_LOG_LEVEL.- Parameters:
msg (
Any) – The message, with %-style placeholders. The caller’s name is added in front of it.args – The message’s arguments.
prefix_format (
str|None) – An ANSI format code for the caller’s name, e.g.SmarterFormattedText.BOLD_DARK_BLUE. The usual prefix color by default.kwargs – Passed to the wrapped logger.
- debug(msg, *args, prefix_format=None, **kwargs)[source]
Log at DEBUG if the condition allows it, or the level is at least
REQUIRED_LOG_LEVEL.- Parameters:
msg (
Any) – The message, with %-style placeholders. The caller’s name is added in front of it.args – The message’s arguments.
prefix_format (
str|None) – An ANSI format code for the caller’s name, e.g.SmarterFormattedText.BOLD_DARK_BLUE. The usual prefix color by default.kwargs – Passed to the wrapped logger.
- error(msg, *args, prefix_format=None, **kwargs)[source]
Log at ERROR if the condition allows it, or the level is at least
REQUIRED_LOG_LEVEL.- Parameters:
msg (
Any) – The message, with %-style placeholders. The caller’s name is added in front of it.args – The message’s arguments.
prefix_format (
str|None) – An ANSI format code for the caller’s name, e.g.SmarterFormattedText.BOLD_DARK_BLUE. The usual prefix color by default.kwargs – Passed to the wrapped logger.
- exception(msg, *args, exc_info=True, prefix_format=None, **kwargs)[source]
Log at ERROR, with the stack trace, if the condition allows it, or ERROR is at least
REQUIRED_LOG_LEVEL.- Parameters:
msg (
Any) – The message, with %-style placeholders. The caller’s name is added in front of it.args – The message’s arguments.
exc_info – Whether to log the current exception’s stack trace.
prefix_format (
str|None) – An ANSI format code for the caller’s name, e.g.SmarterFormattedText.BOLD_DARK_BLUE. The usual prefix color by default.kwargs – Passed to the wrapped logger.
- getEffectiveLevel()[source]
Get the effective logging level of the underlying logger.
- Return type:
- Returns:
The wrapped logger’s effective level, e.g.
logging.INFO.
- info(msg, *args, prefix_format=None, **kwargs)[source]
Log at INFO if the condition allows it, or the level is at least
REQUIRED_LOG_LEVEL.- Parameters:
msg (
Any) – The message, with %-style placeholders. The caller’s name is added in front of it.args – The message’s arguments.
prefix_format (
str|None) – An ANSI format code for the caller’s name, e.g.SmarterFormattedText.BOLD_DARK_BLUE. The usual prefix color by default.kwargs – Passed to the wrapped logger.
- log(level, msg, *args, prefix_format=None, **kwargs)[source]
Log at
levelif the condition allows it, or the level is at leastREQUIRED_LOG_LEVEL.- Parameters:
level (
int) – The logging level, e.g.logging.INFO.msg (
Any) – The message, with %-style placeholders. The caller’s name is added in front of it.args – The message’s arguments.
prefix_format (
str|None) – An ANSI format code for the caller’s name, e.g.SmarterFormattedText.BOLD_DARK_BLUE. The usual prefix color by default.kwargs – Passed to the wrapped logger.
- warning(msg, *args, prefix_format=None, **kwargs)[source]
Log at WARNING if the condition allows it, or the level is at least
REQUIRED_LOG_LEVEL.- Parameters:
msg (
Any) – The message, with %-style placeholders. The caller’s name is added in front of it.args – The message’s arguments.
prefix_format (
str|None) – An ANSI format code for the caller’s name, e.g.SmarterFormattedText.BOLD_DARK_BLUE. The usual prefix color by default.kwargs – Passed to the wrapped logger.