Detect when logging is directed to a file and suppress ANSI formatting characters#74
Detect when logging is directed to a file and suppress ANSI formatting characters#74
Conversation
…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>
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
DavidHuber-NOAA
left a comment
There was a problem hiding this comment.
@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>
The automatic detection was already in place via I've added two new tests in commit
|
|
Merged this branch into #73. Closing. |
ColoredFormatterunconditionally 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_loggernow automatically detects whether its output destination is a TTY. Whencolored_log=Truebut 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:When bash redirects stdout, Python's
sys.stdout.isatty()returnsFalse, soColoredFormatteris 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)streamparameter (default:sys.stdout) for testability and flexibilityColoredFormatteris now only used whencolored_log=Trueand the target stream'sisatty()returnsTrue; falls back to plainlogging.Formatterotherwiseisatty()call is wrapped intry/exceptto guard against streams that expose the attribute but raise on callTests (
test_logger.py)test_stream_logger_no_ansi_on_non_tty—colored_log=True+ explicitio.StringIOstream → no ANSI codestest_stream_logger_ansi_on_tty—colored_log=True+ fake-TTY stream (isatty()=True) → ANSI codes presenttest_file_logger_no_ansi—add_file_loggeroutput never contains ANSI codestest_stream_logger_no_ansi_when_stdout_redirected—sys.stdoutmonkeypatched to a non-TTY (simulatesscript.sh > log.txt),add_stream_loggercalled without an explicit stream → no ANSI codestest_logger_class_no_ansi_when_stdout_redirected— fullLogger(colored_log=True)under simulated bash redirect → no ANSI codestest_logger_logit_logfile— asserts logfile is free of ANSI codes even whencolored_log=TrueType of change
How Has This Been Tested?
Checklist