Repository: apache/airflow
File: providers/amazon/src/airflow/providers/amazon/aws/hooks/s3.py (Python)

Code before:
        for item in local_dir.iterdir():
            item: Path  # type: ignore[no-redef]
            absolute_item_path = item.resolve()

            if absolute_item_path not in current_s3_keys:
                try:
                    if item.is_file():
                        item.unlink(missing_ok=True)
                        self.log.debug("Deleted stale local file: %s", item)
                    elif item.is_dir():
                        # delete only when the folder is empty
                        if not os.listdir(item):
                            item.rmdir()
                            self.log.debug("Deleted stale empty directory: %s", item)
                    else:
                        self.log.debug("Skipping stale item of unknown type: %s", item)
                except OSError as e:
                    self.log.error("Error deleting stale item %s: %s", item, e)
                    raise e

    def _sync_to_local_dir_if_changed(self, s3_bucket, s3_object, local_target_path: Path):
        should_download = False
        download_reasons = []
        if not local_target_path.exists():
            should_download = True
            download_reasons.append(f"Local file {local_target_path} does not exist.")
        else:
            local_stats = local_target_path.stat()
            if s3_object.size != local_stats.st_size:
                should_download = True
                download_reasons.append(
                    f"S3 object size ({s3_object.size}) and local file size ({local_stats.st_size}) differ."
                )

            s3_last_modified = s3_object.last_modified
            if local_stats.st_mtime < s3_last_modified.timestamp():
                should_download = True
                download_reasons.append(
                    f"S3 object last modified ({s3_last_modified.timestamp()}) and local file last modified ({local_stats.st_mtime}) differ."
                )

        if should_download:
            s3_bucket.download_file(s3_object.key, local_target_path)
            reason_msg = " ".join(download_reasons)
            self.log.debug("%s. Downloaded %s to %s", reason_msg, s3_object.key, local_target_path.as_posix())
        else:
            self.log.debug(
                "Local file %s is up-to-date with S3 object %s. Skipping download.",
                local_target_path.as_posix(),
                s3_object.key,
            )

Review comment (bug):
Instead of using f-string, can we do something like this instead?

```python
download_logs = []
download_log_params = []
if ...
    download_logs.append("Local file %s does not exist.")
    download_log_params.append(local_target_path)
...
download_logs.append("Downloaded %s to %s")
download_log_params.extend((s3_object.key, local_target_path.as_posix()))
self.log.debug(" ".join(download_logs), *download_log_params)
```

Or, maybe just log each message separately. There’s no need to combine them into one line.

Code after:
        for item in local_dir.iterdir():
            item: Path  # type: ignore[no-redef]
            absolute_item_path = item.resolve()

            if absolute_item_path not in current_s3_keys:
                try:
                    if item.is_file():
                        item.unlink(missing_ok=True)
                        self.log.debug("Deleted stale local file: %s", item)
                    elif item.is_dir():
                        # delete only when the folder is empty
                        if not os.listdir(item):
                            item.rmdir()
                            self.log.debug("Deleted stale empty directory: %s", item)
                    else:
                        self.log.debug("Skipping stale item of unknown type: %s", item)
                except OSError as e:
                    self.log.error("Error deleting stale item %s: %s", item, e)
                    raise e

    def _sync_to_local_dir_if_changed(self, s3_bucket, s3_object, local_target_path: Path):
        should_download = False
        download_logs: list[str] = []
        download_log_params: list[Any] = []

        if not local_target_path.exists():
            should_download = True
            download_logs.append("Local file %s does not exist.")
            download_log_params.append(local_target_path)
        else:
            local_stats = local_target_path.stat()
            if s3_object.size != local_stats.st_size:
                should_download = True
                download_logs.append("S3 object size (%s) and local file size (%s) differ.")
                download_log_params.extend([s3_object.size, local_stats.st_size])

            s3_last_modified = s3_object.last_modified
            if local_stats.st_mtime < s3_last_modified.timestamp():
                should_download = True
                download_logs.append("S3 object last modified (%s) and local file last modified (%s) differ.")
                download_log_params.extend([s3_last_modified.timestamp(), local_stats.st_mtime])

        if should_download:
            s3_bucket.download_file(s3_object.key, local_target_path)
            download_logs.append("Downloaded %s to %s")
            download_log_params.extend([s3_object.key, local_target_path.as_posix()])
            self.log.debug(" ".join(download_logs), *download_log_params)
        else:
            self.log.debug(
                "Local file %s is up-to-date with S3 object %s. Skipping download.",
                local_target_path.as_posix(),