From 505ab7a296002ba33cda42b690fa959fcffdda80 Mon Sep 17 00:00:00 2001 From: mengfanxing Date: Wed, 27 May 2026 17:15:05 +0800 Subject: [PATCH] fix: correct variable shadowing in print_oldest_log and rename to find_latest_log Two issues in aw_cli: 1. print_oldest_log() shadows the `path` parameter by reassigning it to the return value of find_oldest_log(). When no log file is found, the error message prints `None` instead of the original directory path. Fix: use a separate `logfile` variable. 2. find_oldest_log() actually returns the most recently modified log file (sorts by st_mtime ascending, takes [-1]). Renamed to find_latest_log() to match what the code does. --- aw_cli/__main__.py | 16 ++++++++-------- aw_cli/log.py | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/aw_cli/__main__.py b/aw_cli/__main__.py index c302be62..38b25085 100644 --- a/aw_cli/__main__.py +++ b/aw_cli/__main__.py @@ -9,7 +9,7 @@ import click -from aw_cli.log import find_oldest_log, print_log, LOGLEVELS +from aw_cli.log import find_latest_log, print_log, LOGLEVELS from typing import Optional @@ -63,20 +63,20 @@ def logs( testing = ctx.parent.params["testing"] logdir: Path = Path(get_log_dir(None)) - # find the oldest logfile in each of the subdirectories in the logging directory, and print the last lines in each one. + # find the latest logfile in each of the subdirectories in the logging directory, and print the lines in each one. if module_name: - print_oldest_log(logdir / module_name, testing, since, level) + print_latest_log(logdir / module_name, testing, since, level) else: for subdir in sorted(logdir.iterdir()): if subdir.is_dir(): - print_oldest_log(subdir, testing, since, level) + print_latest_log(subdir, testing, since, level) -def print_oldest_log(path, testing, since, level): - path = find_oldest_log(path, testing) - if path: - print_log(path, since, level) +def print_latest_log(path, testing, since, level): + logfile = find_latest_log(path, testing) + if logfile: + print_log(logfile, since, level) else: print(f"No logfile found in {path}") diff --git a/aw_cli/log.py b/aw_cli/log.py index e55593ef..76d2b015 100644 --- a/aw_cli/log.py +++ b/aw_cli/log.py @@ -39,7 +39,7 @@ def print_log( print(f" (Filtered {lines_printed}/{len(lines)} lines)") -def find_oldest_log(path: Path, testing=False) -> Path: +def find_latest_log(path: Path, testing=False) -> Path: if not path.is_dir(): return