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.")
Parameters:
  • logger (Logger | WaffleSwitchedLoggerWrapper) – The logger to wrap.

  • condition_func (Callable[[int], bool] | None) – Called with a log level; the message is logged only if it returns True. None means always log, at the levels that the wrapped logger is enabled for.

REQUIRED_LOG_LEVEL = 30

Log entries are always emitted at this level and above, whatever the condition says.

__init__(logger, condition_func=None)[source]
_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_DEPTH frames up, plus however many frames a stacklevel argument skips. The wrapped logger is given the same stacklevel, adjusted for the wrapper’s own two frames, so a record’s funcName and lineno are 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_info or extra.

  • 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:

None

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:

int

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 level if the condition allows it, or the level is at least REQUIRED_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.

set_condition(condition_func)[source]

Update the condition function.

Parameters:

condition_func (Callable[[int], bool]) – Called with a log level; the message is logged only if it returns True.

Return type:

None

should_log(level=10)[source]

Check if we should log based on custom conditions.

Parameters:

level (int) – The log level, e.g. logging.INFO.

Return type:

bool

Returns:

False if the wrapped logger isn’t enabled for level, else the condition’s answer (False if it needs a synchronous database call from async code), or True if there is no condition.

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.