Smarter Logging Utilities

Logging utilities for the Smarter app.

WaffleSwitchedLoggerWrapper uses these to start every log message with the dotted name of the function or method that logged it, so that call sites never write their own name into a message.

smarter.lib.logging.utils.SELF_PARAMETER_NAMES = ('self', 'cls')

The names of a method’s first parameter that runtime_class() trusts to be its instance or class.

('self', 'cls')
smarter.lib.logging.utils.caller_name(frame)[source]

Return the dotted name of the function or method that a frame is running, for logging.

Parameters:

frame (FrameType) – A frame of the call stack, e.g. the caller of a logging method.

Return type:

str

Returns:

The module and qualified name followed by parentheses, e.g. smarter.apps.llmtool.models.plugin_data_api.PluginDataApi.execute_request(). A method is named after the class that runs it (see runtime_class()); code at module level is named by its module alone.

smarter.lib.logging.utils.formatted_caller_name(frame, prefix_format=None)[source]

Return the name of a frame’s function or method in the log prefix’s color.

Parameters:
  • frame (FrameType) – A frame of the call stack.

  • prefix_format (str | None) – An ANSI format code from SmarterFormattedText, e.g. SmarterFormattedText.BOLD_DARK_BLUE. None means the usual prefix color, SmarterFormattedText.DEFAULT.

Return type:

str

Returns:

caller_name(), formatted with SmarterFormattedText.text().

smarter.lib.logging.utils.prefixed_message(prefix, msg, has_args)[source]

Return a log message that starts with a prefix.

The prefix is written into the message, rather than passed as an argument, so that the message’s own arguments, including a single mapping for %(name)s placeholders, are left as they are.

Parameters:
  • prefix (str) – The formatted prefix, e.g. from caller_name().

  • msg (object) – The message, usually a string with %-style placeholders.

  • has_args (bool) – Whether the message has arguments. If so, a % in the prefix is escaped, because the logging module will %-format the message.

Return type:

str

Returns:

The prefix, a space, then the message.

smarter.lib.logging.utils.runtime_class(frame)[source]

Return the class that runs the method of a frame, or None if the frame isn’t a method’s.

A method inherited from a base class or a mixin is run by the instance’s own class, so a log entry can say which subclass ran it. The frame’s first parameter must be self or cls, and the class that defines the code must be in its class’s MRO, so that a function whose first parameter only happens to be called self (e.g. a bound Celery task) keeps its own name.

Parameters:

frame (FrameType) – A frame of the call stack.

Return type:

type | None

Returns:

The class of self, or cls itself, or None.