Skip to content

Detect when logging is directed to a file and suppress ANSI formatting characters#74

Closed
Copilot wants to merge 3 commits intodevelopfrom
copilot/detect-file-logging-formatting
Closed

Detect when logging is directed to a file and suppress ANSI formatting characters#74
Copilot wants to merge 3 commits intodevelopfrom
copilot/detect-file-logging-formatting

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 29, 2026

ColoredFormatter unconditionally prepended ANSI escape codes (e.g. \x1b[38;21m) to every log record, meaning those codes would appear verbatim in log files whenever stdout/stderr was redirected.

Description

add_stream_logger now automatically detects whether its output destination is a TTY. When colored_log=True but the stream is not a TTY — including when an entire bash script is redirected to a file (e.g. ./script.sh > logfile.log 2>&1) — color formatting is automatically suppressed. No user-side changes are required; the detection is transparent.

The key detection logic in add_stream_logger:

try:
    is_tty = colored_log and hasattr(stream, 'isatty') and stream.isatty()
except Exception:
    is_tty = False
_format = ColoredFormatter(_format) if is_tty else logging.Formatter(_format)

When bash redirects stdout, Python's sys.stdout.isatty() returns False, so ColoredFormatter is never used and no ANSI codes reach the log file. Output to an interactive terminal is still colourised as expected.

Changes

  • add_stream_logger (logger.py)
    • Added optional stream parameter (default: sys.stdout) for testability and flexibility
    • TTY detection: ColoredFormatter is now only used when colored_log=True and the target stream's isatty() returns True; falls back to plain logging.Formatter otherwise
    • isatty() call is wrapped in try/except to guard against streams that expose the attribute but raise on call

Tests (test_logger.py)

  • test_stream_logger_no_ansi_on_non_ttycolored_log=True + explicit io.StringIO stream → no ANSI codes
  • test_stream_logger_ansi_on_ttycolored_log=True + fake-TTY stream (isatty()=True) → ANSI codes present
  • test_file_logger_no_ansiadd_file_logger output never contains ANSI codes
  • test_stream_logger_no_ansi_when_stdout_redirectedsys.stdout monkeypatched to a non-TTY (simulates script.sh > log.txt), add_stream_logger called without an explicit stream → no ANSI codes
  • test_logger_class_no_ansi_when_stdout_redirected — full Logger(colored_log=True) under simulated bash redirect → no ANSI codes
  • Updated test_logger_logit_logfile — asserts logfile is free of ANSI codes even when colored_log=True

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • pynorms
  • pytests

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes need updates to the documentation. I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • New and existing tests pass with my changes
  • Any dependent changes have been merged and published

…Y streams

Agent-Logs-Url: https://github.com/NOAA-EMC/wxflow/sessions/94ef3770-3e22-4a0d-b5f2-2e1a92fea70d

Co-authored-by: DavidHuber-NOAA <69919478+DavidHuber-NOAA@users.noreply.github.com>
Copilot AI changed the title [WIP] Update Logger class to handle file output properly Detect when logging is directed to a file and suppress ANSI formatting characters Apr 29, 2026
Copilot AI requested a review from DavidHuber-NOAA April 29, 2026 14:35
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.58%. Comparing base (e952a8a) to head (6d14715).

Files with missing lines Patch % Lines
src/wxflow/logger.py 75.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop      #74      +/-   ##
===========================================
+ Coverage    60.56%   60.58%   +0.01%     
===========================================
  Files           24       24              
  Lines         1973     1979       +6     
  Branches       370      371       +1     
===========================================
+ Hits          1195     1199       +4     
- Misses         724      726       +2     
  Partials        54       54              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@DavidHuber-NOAA DavidHuber-NOAA left a comment

Choose a reason for hiding this comment

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

@copilot the scripts that use logging function typically write to stdout but are redirected to a file. These jobs run within a bash script and redirection of the entire script to the same file must be possible. Please rework this solution to automatically detect if redirection is being performed and, if so, remove colored formatting.

…dd_stream_logger

Agent-Logs-Url: https://github.com/NOAA-EMC/wxflow/sessions/a11632c0-b4f0-467d-bf41-56329914d73b

Co-authored-by: DavidHuber-NOAA <69919478+DavidHuber-NOAA@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 29, 2026

@copilot the scripts that use logging function typically write to stdout but are redirected to a file. These jobs run within a bash script and redirection of the entire script to the same file must be...

The automatic detection was already in place via sys.stdout.isatty() — when bash redirects an entire script (./script.sh > logfile.log), Python's sys.stdout.isatty() returns False, so ColoredFormatter is never used and no ANSI codes reach the file.

I've added two new tests in commit 6d14715 that explicitly verify this end-to-end case by monkeypatching sys.stdout with a non-TTY stream:

  • test_stream_logger_no_ansi_when_stdout_redirected — calls add_stream_logger with colored_log=True and no explicit stream (mirrors real-world usage), verifies no ANSI codes
  • test_logger_class_no_ansi_when_stdout_redirected — exercises Logger(colored_log=True) directly under the same simulated redirect, verifies no ANSI codes

@DavidHuber-NOAA
Copy link
Copy Markdown
Contributor

Merged this branch into #73. Closing.

@DavidHuber-NOAA DavidHuber-NOAA deleted the copilot/detect-file-logging-formatting branch April 29, 2026 15:44
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.

Detect when logging is directed to a file and remove formatting characters

2 participants