Add --log-color flag to control ANSI color log output#1374
Open
zhengxiexie wants to merge 1 commit intovmware-tanzu:mainfrom
Open
Add --log-color flag to control ANSI color log output#1374zhengxiexie wants to merge 1 commit intovmware-tanzu:mainfrom
--log-color flag to control ANSI color log output#1374zhengxiexie wants to merge 1 commit intovmware-tanzu:mainfrom
Conversation
58d11e4 to
0a1dd6b
Compare
Contributor
Author
|
/e2e |
Codecov Report❌ Patch coverage is
❌ Your patch status has failed because the patch coverage (64.28%) is below the target coverage (70.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #1374 +/- ##
==========================================
- Coverage 76.65% 76.58% -0.07%
==========================================
Files 151 151
Lines 21102 21109 +7
==========================================
- Hits 16175 16166 -9
- Misses 3773 3788 +15
- Partials 1154 1155 +1
🚀 New features to boost your workflow:
|
heypnus
reviewed
Feb 9, 2026
pkg/logger/logger.go
Outdated
| // ZapCustomLogger creates a CustomLogger with both logr.Logger and zerolog.Logger using the same configuration as ZapLogger | ||
| func ZapCustomLogger(cfDebug bool, cfLogLevel int) CustomLogger { | ||
| logLevel := getLogLevel(cfDebug, cfLogLevel) | ||
| enableColor := logLevel >= 3 |
Contributor
There was a problem hiding this comment.
Can we use a individual flag to control the color switch? I feel that we'd better not bind the color with a specific log level..
0a1dd6b to
03063d5
Compare
--log-color flag to control ANSI color log output
Contributor
Author
|
/e2e |
1 similar comment
Contributor
Author
|
/e2e |
yanjunz97
reviewed
Feb 10, 2026
cmd_clean/main.go
Outdated
| log = logger.ZapCustomLogger(cf.DefaultConfig.Debug, config.LogLevel, config.LogColor) | ||
| logger.Log = log | ||
| logf.SetLogger(log.Logger) | ||
| err := clean.Clean(ctx, cf, &log.Logger, cf.DefaultConfig.Debug, config.LogLevel) |
Contributor
There was a problem hiding this comment.
pass config.LogColor to clean.Clean?
- Updated `ZapCustomLogger` to accept `enableColor` flag instead of relying on log level. - Added `--log-color` flag to main programs and test frameworks for explicit control. - Updated tests to validate color handling based on the flag setting. Change-Id: I80a900072a535f0879f82ea99a90d68763da2aaf
03063d5 to
9eab983
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
✨ What's Changed
Log Color Control
--log-colorflag is added to explicitly enable colored log output--log-colorImplementation Details
LogColorconfig variable and--log-colorCLI flag (defaultfalse) inpkg/config/config.goZapCustomLoggersignature to acceptenableColor boolparameterConsoleWriter.NoColorbased on the flag to control zerolog color outputFormatLevelandFormatCallercolor wrapping withenableColorcheck== 2to>= 2forTraceLevelmapping--log-colorflag in all entry points:cmd/main.go,cmd_clean/main.go,test/e2e/main_test.goTestGetLogLevelandTestAnsiColorControlledByFlag🎯 Motivation
ANSI color escape codes were always emitted in log output regardless of log level. This causes garbled text in non-terminal environments such as
kubectl logs, log aggregators, and CI pipelines where ANSI codes are not interpreted.Issue: Log output contains raw ANSI escape sequences (e.g.
\033[32m) when viewed in environments that don't support terminal colors, making logs harder to read and parse.Solution: Make color output opt-in via a dedicated
--log-colorflag, keeping it independent from log verbosity levels. This is typically used only during local development/debugging where a real terminal is available.✅ Testing
Unit Tests
TestGetLogLevel- Validates log level calculation for all debug/logLevel combinations (default, debug_flag_only, log_level_1, log_level_3, debug_with_higher_level, debug_with_lower_level)TestAnsiColorControlledByFlag- Captures log output to buffer and verifies:color_disabled- No ANSI escape codes when--log-coloris not setcolor_enabled- ANSI escape codes present when--log-coloris set🔄 Backward Compatibility
This change is fully backward compatible:
--log-color