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

Add ability to only colorize the header, not the whole log message #108

Merged
merged 1 commit into from
Mar 3, 2022
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
2 changes: 2 additions & 0 deletions colorize_unix.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package hclog
Expand All @@ -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
}
}
Expand Down
7 changes: 6 additions & 1 deletion colorize_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package hclog
Expand Down Expand Up @@ -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)
}
}
}
22 changes: 20 additions & 2 deletions intlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type intLogger struct {
writer *writer
level *int32

headerColor ColorOption

implied []interface{}

exclude func(level Level, msg string, args ...interface{}) bool
Expand Down Expand Up @@ -113,17 +115,28 @@ 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,
timeFormat: TimeFormat,
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
Expand Down Expand Up @@ -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("[?????]")
}
Expand Down
3 changes: 3 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down