Skip to content

Do not log self and do not write ANSI color codes when writing/piping output to a file#73

Merged
DavidHuber-NOAA merged 14 commits intoNOAA-EMC:developfrom
DavidHuber-NOAA:feature/no_log_self
May 5, 2026
Merged

Do not log self and do not write ANSI color codes when writing/piping output to a file#73
DavidHuber-NOAA merged 14 commits intoNOAA-EMC:developfrom
DavidHuber-NOAA:feature/no_log_self

Conversation

@DavidHuber-NOAA
Copy link
Copy Markdown
Contributor

@DavidHuber-NOAA DavidHuber-NOAA commented Apr 28, 2026

Description
When using the logit decorator on an instance method, this will prevent self from being logged. This improves readability of the logs.

Refs NOAA-EMC/global-workflow#4721
Resolves #70

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?
Ran a global-workflow stage_ic job. This should be tested more thoroughly to ensure NCO approval of this method.

  • 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 generate no new warnings
  • New and existing tests pass with my changes

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 28, 2026

Codecov Report

❌ Patch coverage is 87.50000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.72%. Comparing base (e952a8a) to head (a05cc24).

Files with missing lines Patch % Lines
src/wxflow/logger.py 87.50% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop      #73      +/-   ##
===========================================
+ Coverage    60.56%   60.72%   +0.15%     
===========================================
  Files           24       24              
  Lines         1973     1986      +13     
  Branches       370      374       +4     
===========================================
+ Hits          1195     1206      +11     
- 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.

Comment thread src/wxflow/logger.py Outdated
@DavidHuber-NOAA
Copy link
Copy Markdown
Contributor Author

Merged #74 into this PR. This removes ANSI formatting characters when piping output to a log file.

@DavidHuber-NOAA DavidHuber-NOAA changed the title Do not log self Do not log self and do not write ANSI color codes when writing/piping output to a file Apr 29, 2026
@DavidHuber-NOAA DavidHuber-NOAA requested a review from Copilot April 29, 2026 18:40
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates wxflow’s logging utilities to (1) suppress ANSI color escape codes when output is redirected/piped to non-TTY streams and (2) adjust the logit decorator’s argument logging for instance methods.

Changes:

  • Extend add_stream_logger to accept an optional stream and only apply ColoredFormatter when the stream is a TTY.
  • Modify logit to avoid logging full self representations (currently by substituting a placeholder).
  • Add tests to detect ANSI escape codes in file / non-TTY streams and exercise logit on an instance method.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/wxflow/logger.py Adds TTY detection for colored formatting and changes logit argument formatting logic.
tests/test_logger.py Adds ANSI escape detection regex and multiple tests for TTY vs non-TTY behavior plus an instance-method logit test.
Comments suppressed due to low confidence (1)

src/wxflow/logger.py:277

  • The changes to logit significantly alter what gets logged for positional args, but the tests don’t assert that normal functions still log their actual argument values (e.g., add(2, 3) should log 2 and 3, not placeholders). Adding an assertion against the logfile/stream output for a non-method function would prevent regressions in argument logging while still ensuring self is skipped/obfuscated for instance methods.
            # Get all of the arguments passed to the function and log them, skipping 'self'.
            passed_args = []
            # Determine if the function is an instance method.
            if len(args) > 0:
                if hasattr(args[0], '__class__'):
                    class_name = args[0].__class__.__name__
                    for aa in args:
                        if isinstance(aa, args[0].__class__):
                            passed_args.append(f"{class_name} object")
                        else:
                            passed_args.append(repr(aa))
                else:
                    passed_args = [repr(aa) for aa in args]

            passed_kwargs = [f"{kk}={repr(vv)}" for kk, vv in list(kwargs.items())]

            call_msg = 'BEGIN: ' + log_msg
            logger.info(call_msg)
            logger.debug(f"( {', '.join(passed_args + passed_kwargs)} )")

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/wxflow/logger.py
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_logger.py Outdated
Comment thread tests/test_logger.py Outdated
Comment thread src/wxflow/logger.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@DavidHuber-NOAA
Copy link
Copy Markdown
Contributor Author

@TravisElless-NOAA could you review this PR?

@DavidHuber-NOAA DavidHuber-NOAA merged commit e9af649 into NOAA-EMC:develop May 5, 2026
10 checks passed
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

4 participants