From fc772a82634c36e9e7f6bab2869be9ec23481879 Mon Sep 17 00:00:00 2001 From: Evan Phoenix Date: Thu, 24 Feb 2022 14:34:06 -0800 Subject: [PATCH] Add ability to only colorize the header, not the whole log message --- colorize_unix.go | 2 ++ colorize_windows.go | 7 ++++++- intlogger.go | 22 ++++++++++++++++++++-- logger.go | 3 +++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/colorize_unix.go b/colorize_unix.go index 44aa9bf..9635c83 100644 --- a/colorize_unix.go +++ b/colorize_unix.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows package hclog @@ -21,6 +22,7 @@ func (l *intLogger) setColorization(opts *LoggerOptions) { isCygwinTerm := isatty.IsCygwinTerminal(fi.Fd()) isTerm := isUnixTerm || isCygwinTerm if !isTerm { + l.headerColor = ColorOff l.writer.color = ColorOff } } diff --git a/colorize_windows.go b/colorize_windows.go index 23486b6..3085916 100644 --- a/colorize_windows.go +++ b/colorize_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package hclog @@ -26,8 +27,12 @@ func (l *intLogger) setColorization(opts *LoggerOptions) { isTerm := isUnixTerm || isCygwinTerm if !isTerm { l.writer.color = ColorOff + l.headerColor = ColorOff return } - l.writer.w = colorable.NewColorable(fi) + + if l.headerColor == ColorOff { + l.writer.w = colorable.NewColorable(fi) + } } } diff --git a/intlogger.go b/intlogger.go index 0a3206b..83232f7 100644 --- a/intlogger.go +++ b/intlogger.go @@ -69,6 +69,8 @@ type intLogger struct { writer *writer level *int32 + headerColor ColorOption + implied []interface{} exclude func(level Level, msg string, args ...interface{}) bool @@ -113,6 +115,16 @@ func newLogger(opts *LoggerOptions) *intLogger { mutex = new(sync.Mutex) } + var primaryColor, headerColor ColorOption + + if opts.ColorHeaderOnly { + primaryColor = ColorOff + headerColor = opts.Color + } else { + primaryColor = opts.Color + headerColor = ColorOff + } + l := &intLogger{ json: opts.JSONFormat, name: opts.Name, @@ -120,10 +132,11 @@ func newLogger(opts *LoggerOptions) *intLogger { timeFn: time.Now, disableTime: opts.DisableTime, mutex: mutex, - writer: newWriter(output, opts.Color), + writer: newWriter(output, primaryColor), level: new(int32), exclude: opts.Exclude, independentLevels: opts.IndependentLevels, + headerColor: headerColor, } if opts.IncludeLocation { l.callerOffset = offsetIntLogger + opts.AdditionalLocationOffset @@ -232,7 +245,12 @@ func (l *intLogger) logPlain(t time.Time, name string, level Level, msg string, s, ok := _levelToBracket[level] if ok { - l.writer.WriteString(s) + if l.headerColor != ColorOff { + color := _levelToColor[level] + color.Fprint(l.writer, s) + } else { + l.writer.WriteString(s) + } } else { l.writer.WriteString("[?????]") } diff --git a/logger.go b/logger.go index 34ff358..8581430 100644 --- a/logger.go +++ b/logger.go @@ -271,6 +271,9 @@ type LoggerOptions struct { // are concretely instances of *os.File. Color ColorOption + // Only color the header, not the body. This can help with readability of long messages. + ColorHeaderOnly bool + // A function which is called with the log information and if it returns true the value // should not be logged. // This is useful when interacting with a system that you wish to suppress the log