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

Code before:
        self.method_name = method_name
        self.level_styles = level_styles
        self.styles = styles
        self.no_colors = self.styles.reset == ""

    def __getitem__(self, key):
        # Roughly compatible with names from https://github.com/python/cpython/blob/v3.13.7/Lib/logging/__init__.py#L571
        # Plus with ColoredLog added in

        # If there is no callsite info (often for stdout/stderr), show the same sort of thing that stdlib
        # logging would
        # https://github.com/python/cpython/blob/d3c888b4ec15dbd7d6b6ef4f15b558af77c228af/Lib/logging/__init__.py#L1652C34-L1652C48
        if key in PercentFormatRender.callsite_parameters:
            val = self.event.get(PercentFormatRender.callsite_parameters[key].value)
            # Return appropriate default based on the parameter type
            if val is None:
                if key == "lineno":
                    return 0
                if key in ("process", "thread"):
                    return 0
                if key == "filename":
                    return "(unknown file)"
                if key == "funcName":
                    return "(unknown function)"
                return "(unknown)"
            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 ""

Review comment (suggestion):
Honestly this is now a bit hard to follow:

```suggestion
        if key in {"lineno", "process" "thread"}:
            return self.event.get(key, 0)
        if key == "filename":
            return self.event.get("filename", "(unknown file)")
        if key == "funcName":
            return self.event.get("funcName", "(unknown function)")

        if key in PercentFormatRender.callsite_parameters:
            return self.event.get(PercentFormatRender.callsite_parameters[key].value, "(unknown)")
```

Code after:
        self.method_name = method_name
        self.level_styles = level_styles
        self.styles = styles
        self.no_colors = self.styles.reset == ""

    def __getitem__(self, key):
        # Roughly compatible with names from https://github.com/python/cpython/blob/v3.13.7/Lib/logging/__init__.py#L571
        # Plus with ColoredLog added in

        # If there is no callsite info (often for stdout/stderr), show the same sort of thing that stdlib
        # logging would
        # https://github.com/python/cpython/blob/d3c888b4ec15dbd7d6b6ef4f15b558af77c228af/Lib/logging/__init__.py#L1652C34-L1652C48
        if key == "lineno":
            return self.event.get("lineno") or 0
        if key == "filename":
            return self.event.get("filename", "(unknown file)")
        if key == "funcName":
            return self.event.get("funcName", "(unknown function)")
        if key in PercentFormatRender.callsite_parameters:
            return self.event.get(PercentFormatRender.callsite_parameters[key].value, "(unknown)")
        if key == "name":
            return self.event.get("logger") or self.event.get("logger_name", "(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), "")

        return self.event.get(key)

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

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