Skip to content

API Reference

class

sanitary.Sanitizer(keys=(), patterns=(), replacement='********', message='#### WARNING: Message replaced due to sensitive information.')

Base class for sensitive data sanitizers.

Parameters
  • keys (iterable of str, optional) Collection of keys to sanitize. Will be normalized to lowercase.
  • patterns (iterable of pattern, optional) Collection of regular expression patterns; will be compiled using re.compile.
  • replacement (Union(str, callable(str: str), callable(bytes: hashobjectprotocol)), optional) A string or callable to be used to replace the value. A callable must either accept and return a str value, or accept a bytes object and return an object compatible with the hashlib function.
  • message (str, optional) The text to replace the matching string patterns.
Methods
  • sanitize(data) (any) Sanitize data by masking potentially sensitive information.
method

sanitize(data)

Sanitize data by masking potentially sensitive information.

If an unknown data type is encountered, its string representation will be sanitized.

Parameters
  • data (any) The data to sanitize.
Returns (any)

The sanitized form of data.

class

sanitary.StructlogSanitizer(keys=(), patterns=(), replacement='********', message='#### WARNING: Message replaced due to sensitive information.')

Bases
sanitary.Sanitizer

Structlog processor for cleaning up logging context by masking sensitive data.

Parameters
  • keys (iterable of str, optional) Collection of keys to sanitize. Will be normalized to lowercase.
  • patterns (iterable of pattern, optional) Collection of regular expression patterns; will be compiled using re.compile.
  • replacement (Union(str, callable(str: str), callable(bytes: hashobjectprotocol)), optional) A string or callable to be used to replace the value. A callable must either accept and return a str value, or accept a bytes object and return an object compatible with the hashlib function.
  • message (str, optional) The text to replace the matching string patterns.
Methods
  • __call__(logger, name, event_dict) (EventDict) Makes the sanitizer a callable, compatible with the Structlog processor API.
  • sanitize(data) (any) Sanitize data by masking potentially sensitive information.
method

sanitize(data)

Sanitize data by masking potentially sensitive information.

If an unknown data type is encountered, its string representation will be sanitized.

Parameters
  • data (any) The data to sanitize.
Returns (any)

The sanitized form of data.

method

__call__(logger, name, event_dict)

Makes the sanitizer a callable, compatible with the Structlog processor API.

For details see https://www.structlog.org/en/stable/processors.html

Parameters
  • logger (WrappedLogger) The logger instance doing the logging.
  • name (str) Name of the logging method, e.g. info or warning.
  • event_dict (EventDict) Current context, including modifications by other processors.
Returns (EventDict)

dict