Skip to content

Safe Logger

Redaction-aware logging helpers for gllm-multimodal.

Provides a logger that automatically redacts sensitive data so that callers do not have to remember to wrap every log argument with redact_sensitive_data.

SafeLogger(logger, extra=None, safe_keys=None)

Bases: Logger

A Logger that redacts sensitive data before delegating to the underlying logger.

Redaction strategy: 1. Structured %-style positional args and dict/list/tuple messages are passed through redact_sensitive_data (whitelist-based). 2. String messages are additionally scrubbed with a best-effort regex to catch secrets that were already interpolated (e.g. via f-strings).

Safe-key scope: The whitelist used is the process-global SAFE_KEYS plus any keys registered on this logger via safe_keys/add_safe_keys. Per-logger keys are local to this instance (and therefore, by convention, to the module that created it): they never affect redaction for other loggers.

Attributes:

Name Type Description
logger Logger

The underlying logger that receives redacted records.

extra MutableMapping[str, Any]

Contextual extra data attached to log records.

Initializes a new instance of the SafeLogger class.

Parameters:

Name Type Description Default
logger Logger

The underlying logger to wrap.

required
extra MutableMapping[str, Any] | None

Contextual extra data attached to log records. Defaults to None.

None
safe_keys str | Iterable[str] | None

Extra key name(s) to whitelist locally on this logger only. Defaults to None.

None

addHandler(handler)

Add a handler to the underlying logger.

Parameters:

Name Type Description Default
handler Handler

The handler to register on the underlying logger.

required

add_safe_keys(*keys)

Register extra safe keys local to this logger only.

Parameters:

Name Type Description Default
*keys str | Iterable[str]

Key names (or iterables of names) to whitelist locally.

()

getEffectiveLevel()

Return the effective level from the underlying logger.

Returns:

Name Type Description
int int

The effective logging level of the underlying logger.

hasHandlers()

Return whether the underlying logger has handlers.

Returns:

Name Type Description
bool bool

True if the underlying logger has at least one handler.

isEnabledFor(level)

Return whether the underlying logger is enabled for the given level.

Parameters:

Name Type Description Default
level int

The logging level to check.

required

Returns:

Name Type Description
bool bool

True if the underlying logger would emit at this level.

log(level, msg, *args, **kwargs)

Log a redacted message at the given level.

Positional args carry the values for %-style formatting and are not seen by process, so they are redacted in _redact_log_inputs before delegation.

Parameters:

Name Type Description Default
level int

The logging level (for example, logging.INFO).

required
msg Any

The log message.

required
*args Any

Positional arguments for %-style message formatting.

()
**kwargs Any

Logging keyword arguments forwarded to the underlying logger.

{}

process(msg, kwargs)

Redact the log message before it is emitted.

Parameters:

Name Type Description Default
msg Any

The log message; may be a structured object or a string.

required
kwargs MutableMapping[str, Any]

Logging keyword arguments passed through unchanged.

required

Returns:

Type Description
tuple[Any, MutableMapping[str, Any]]

tuple[Any, MutableMapping[str, Any]]: The redacted message and the original kwargs.

removeHandler(handler)

Remove a handler from the underlying logger.

Parameters:

Name Type Description Default
handler Handler

The handler to remove from the underlying logger.

required

setLevel(level)

Set the level on the underlying logger and this wrapper.

Parameters:

Name Type Description Default
level int | str

The logging level to set (for example, logging.DEBUG or "DEBUG").

required

get_safe_logger(name=None, *, safe_keys=None)

Return a redaction-aware logger for the given name.

Drop-in replacement for LoggerManager().get_logger(name) that automatically redacts sensitive data. Use this in modules that log configuration or request data.

Parameters:

Name Type Description Default
name str | None

The logger name, typically __name__. Defaults to None.

None
safe_keys str | Iterable[str] | None

Extra key(s) to keep, local to the returned logger only (does not affect other loggers). Defaults to None.

None

Returns:

Name Type Description
SafeLogger SafeLogger

A redaction-aware logger wrapping the shared logger.