Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix colors not being forced on correctly. #136

Merged
merged 3 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions intlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,25 @@ var (

faintBoldColor = color.New(color.Faint, color.Bold)
faintColor = color.New(color.Faint)
faintMultiLinePrefix = faintColor.Sprint(" | ")
faintFieldSeparator = faintColor.Sprint("=")
faintFieldSeparatorWithNewLine = faintColor.Sprint("=\n")
faintMultiLinePrefix string
faintFieldSeparator string
faintFieldSeparatorWithNewLine string
)

func init() {
// Force all the colors to enabled because we do our own detection of color usage.
for _, c := range _levelToColor {
c.EnableColor()
}

faintBoldColor.EnableColor()
faintColor.EnableColor()

faintMultiLinePrefix = faintColor.Sprint(" | ")
faintFieldSeparator = faintColor.Sprint("=")
faintFieldSeparatorWithNewLine = faintColor.Sprint("=\n")
}

// Make sure that intLogger is a Logger
var _ Logger = &intLogger{}

Expand Down
32 changes: 32 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,38 @@ func TestLogger(t *testing.T) {
assert.Equal(t, "[INFO] sublogger: this is test\n", rest)
})

t.Run("can force colors to on in any context", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("colors are different on windows")
}

var buf bytes.Buffer

logger := New(&LoggerOptions{
// No name!
Output: &buf,
Level: Trace,
Color: ForceColor,
TimeFormat: "<time>",
})

logger.Trace("trace")
logger.Debug("debug")
logger.Info("info")
logger.Warn("warn")
logger.Error("error")
str := buf.String()

assert.Equal(t, ""+
"\033[92m<time> [TRACE] trace\n\033[0m"+
"\033[97m<time> [DEBUG] debug\n\033[0m"+
"\033[94m<time> [INFO] info\n\033[0m"+
"\033[93m<time> [WARN] warn\n\033[0m"+
"\033[91m<time> [ERROR] error\n\033[0m",
str,
)
})

t.Run("use a different time format", func(t *testing.T) {
var buf bytes.Buffer

Expand Down