Skip to content

[CLI] Make per-run log files unique by appending timestamp + open with mode="w" (fixes #984)#83

Open
abhisheksingh-esp wants to merge 1 commit into
project-chip:v2.15-cli-developfrom
abhisheksingh-esp:fix/984_unique_log_file_per_run
Open

[CLI] Make per-run log files unique by appending timestamp + open with mode="w" (fixes #984)#83
abhisheksingh-esp wants to merge 1 commit into
project-chip:v2.15-cli-developfrom
abhisheksingh-esp:fix/984_unique_log_file_per_run

Conversation

@abhisheksingh-esp
Copy link
Copy Markdown

Fixes #984.

Root cause: configure_logger_for_run in
certification-tool-cli/th_cli/test_run/logging.py derived the log file path
solely from --title (test_run_{title}.log) and loguru.logger.add(...)
defaults to append mode. Reusing -n NAME across runs caused successive runs
to be concatenated into the same file.
Change:

  • Append a timestamp suffix (_YYYY-MM-DD-HH-MM-SS) to the log filename inside
    configure_logger_for_run, so every run produces a unique file regardless
    of the -n NAME value (matches the convention suggested by @OlivierGre).
  • Pass mode="w" to logger.add(...) as defense-in-depth, in case two runs
    ever land on the exact same path (e.g. same second).
    No public API change; configure_logger_for_run(title) still returns the
    resolved path used by run_tests at the end of execution.
    Verification:
  • Run the same test twice with -n my_run. Two distinct files in run_logs/,
    each containing only its own run.

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Abhishek Singh seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the logging configuration to include a timestamp in log filenames and sets the file mode to write. A review comment identifies a potential portability issue on Windows systems where the 'title' parameter might contain invalid characters like colons, and provides a code suggestion to sanitize the string.

Comment thread th_cli/test_run/logging.py
@abhisheksingh-esp abhisheksingh-esp force-pushed the fix/984_unique_log_file_per_run branch from 519ae4a to 8d92cd1 Compare May 14, 2026 10:14
@rquidute rquidute self-requested a review May 14, 2026 11:15
Copy link
Copy Markdown
Contributor

@antonio-amjr antonio-amjr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @abhisheksingh-esp,

Seems good, but I would like to ask for a little change if I may.
Let me know what you think of the suggestion below.

# filenames that are invalid on Windows (e.g. when logs are shared across
# systems for triage).
safe_title = title.replace(":", "-")
timestamp = datetime.datetime.now().strftime(_LOG_TIMESTAMP_FORMAT)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the .isoformat() for the timestamp?
It will have the appropriate format (YYYY-MM-DDTHH:MM:SS.ffffff) and we would use a standard approach.

Suggested change
timestamp = datetime.datetime.now().strftime(_LOG_TIMESTAMP_FORMAT)
timestamp = datetime.datetime.now().isoformat()

Let me know what you think and if that works.

Copy link
Copy Markdown
Author

@abhisheksingh-esp abhisheksingh-esp May 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed — using .isoformat() makes sense here. It already provides the correct standardized format (YYYY-MM-DDTHH:MM:SS.ffffff) and keeps the implementation cleaner and more consistent.

Only one catch: isoformat() produces timestamps containing :, which makes the filename invalid on Windows (and some network/USB filesystems). This could create issues if the log file is copied to or opened on a Windows machine.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch about Windows systems.

Maybe that's not the best option.
Or maybe we could replace all : for - from the isoformat's output.

In that case, please feel free to continue with your solution if that would be a pain in Windows machines. Since my team is not using, sometimes those details may slip.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know which approach do you think would be better for all systems in this case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants