Skip to content

Commit

Permalink
Merge pull request #136 from hashicorp/f-color-doc
Browse files Browse the repository at this point in the history
Fix colors not being forced on correctly.
  • Loading branch information
evanphx authored Dec 4, 2023
2 parents 852f2c3 + a72e7ad commit 71d286f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
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

0 comments on commit 71d286f

Please sign in to comment.