Skip to content

prefect.logging.filters

ObfuscateApiKeyFilter

Bases: Filter

A logging filter that obfuscates any string that matches the obfuscate_string function.

Source code in src/prefect/logging/filters.py
29
30
31
32
33
34
35
36
37
38
39
40
41
class ObfuscateApiKeyFilter(logging.Filter):
    """
    A logging filter that obfuscates any string that matches the obfuscate_string function.
    """

    def filter(self, record: logging.LogRecord) -> bool:
        # Need to import here to avoid circular imports
        from prefect.settings import PREFECT_API_KEY

        if PREFECT_API_KEY:
            record.msg = redact_substr(record.msg, PREFECT_API_KEY.value())

        return True

redact_substr(obj, substr)

Redact a string from a potentially nested object.

Parameters:

Name Type Description Default
obj Any

The object to redact the string from

required
substr str

The string to redact.

required

Returns:

Name Type Description
Any

The object with the API key redacted.

Source code in src/prefect/logging/filters.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def redact_substr(obj: Any, substr: str):
    """
    Redact a string from a potentially nested object.

    Args:
        obj: The object to redact the string from
        substr: The string to redact.

    Returns:
        Any: The object with the API key redacted.
    """

    def redact_item(item):
        if isinstance(item, str):
            return item.replace(substr, obfuscate(substr))
        return item

    redacted_obj = visit_collection(obj, visit_fn=redact_item, return_data=True)
    return redacted_obj