Repository: apache/airflow
File: shared/logging/src/airflow_shared/logging/percent_formatter.py (Python)

Code before:
            return val
        if key == "name":
            return self.event.get("logger") or self.event.get("logger_name") or "(unknown)"
        if key == "levelname":
            return self.event.get("level", self.method_name).upper()
        if key == "asctime" or key == "created":
            return (
                self.event.get("timestamp", None)
                or datetime.datetime.now(tz=datetime.timezone.utc).isoformat()
            )
        if key == "message":
            return self.event["event"]

        if key in ("red", "green", "yellow", "blue", "purple", "cyan"):
            if self.no_colors:
                return ""
            return getattr(structlog.dev, key.upper(), "")
        if key == "reset":
            return self.styles.reset
        if key == "log_color":
            if self.no_colors:
                return ""
            return self.level_styles.get(self.event.get("level", self.method_name), "")

        val = self.event.get(key)
        return val if val is not None else ""

    def __iter__(self):
        return self.event.__iter__()

    def __len__(self):
        return len(self.event)


class PercentFormatRender(ConsoleRenderer):
    """A Structlog processor that uses a stdlib-like percent based format string."""

    _fmt: str

    # From https://github.com/python/cpython/blob/v3.12.11/Lib/logging/__init__.py#L563-L587
    callsite_parameters: ClassVar[dict[str, CallsiteParameter]] = {
        "pathname": CallsiteParameter.PATHNAME,
        "filename": CallsiteParameter.FILENAME,
        "module": CallsiteParameter.MODULE,
        "lineno": CallsiteParameter.LINENO,
        "funcName": CallsiteParameter.FUNC_NAME,
        "thread": CallsiteParameter.THREAD,
        "threadName": CallsiteParameter.THREAD_NAME,
        "process": CallsiteParameter.PROCESS,
        # This one isn't listed in the docs until 3.14, but it's worked for a long time
        "processName": CallsiteParameter.PROCESS_NAME,

Review comment (suggestion):
I'm not so sure about this one -- it would mean that `"%(x)r"` would show `""` instead of `None`.

Code after:
            return (
                self.event.get("timestamp", None)
                or datetime.datetime.now(tz=datetime.timezone.utc).isoformat()
            )
        if key == "message":
            return self.event["event"]

        if key in ("red", "green", "yellow", "blue", "purple", "cyan"):
            if self.no_colors:
                return ""
            return getattr(structlog.dev, key.upper(), "")
        if key == "reset":
            return self.styles.reset
        if key == "log_color":
            if self.no_colors:
                return ""
            return self.level_styles.get(self.event.get("level", self.method_name), "")

        return self.event.get(key)

    def __iter__(self):
        return self.event.__iter__()

    def __len__(self):
        return len(self.event)


class PercentFormatRender(ConsoleRenderer):
    """A Structlog processor that uses a stdlib-like percent based format string."""

    _fmt: str

    # From https://github.com/python/cpython/blob/v3.12.11/Lib/logging/__init__.py#L563-L587
    callsite_parameters: ClassVar[dict[str, CallsiteParameter]] = {
        "pathname": CallsiteParameter.PATHNAME,
        "filename": CallsiteParameter.FILENAME,
        "module": CallsiteParameter.MODULE,
        "lineno": CallsiteParameter.LINENO,
        "funcName": CallsiteParameter.FUNC_NAME,
        "thread": CallsiteParameter.THREAD,
        "threadName": CallsiteParameter.THREAD_NAME,
        "process": CallsiteParameter.PROCESS,
        # This one isn't listed in the docs until 3.14, but it's worked for a long time
        "processName": CallsiteParameter.PROCESS_NAME,
    }

    special_keys = {
        "event",
        "name",
        "logger",
        "logger_name",