Skip to content
Closed
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
25 changes: 24 additions & 1 deletion promlog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/go-kit/kit/log/term"
"github.com/pkg/errors"
)

Expand All @@ -31,6 +32,25 @@ type AllowedLevel struct {
o level.Option
}

func colorFn(keyvals ...interface{}) term.FgBgColor {
for i := 1; i < len(keyvals); i += 2 {
if keyvals[i] != "level" {
continue
}
switch keyvals[i+1] {
case "debug":
return term.FgBgColor{Fg: term.Blue}
case "warn":
return term.FgBgColor{Fg: term.Yellow}
case "error":
return term.FgBgColor{Fg: term.Red}
default:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about green for INFO?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

return term.FgBgColor{}
}
}
return term.FgBgColor{}
}

func (l *AllowedLevel) String() string {
return l.s
}
Expand All @@ -56,7 +76,10 @@ func (l *AllowedLevel) Set(s string) error {
// New returns a new leveled oklog logger in the logfmt format. Each logged line will be annotated
// with a timestamp. The output always goes to stderr.
func New(al AllowedLevel) log.Logger {
l := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
syncWriter := log.NewSyncWriter(os.Stderr)
// Returns a new logger with color logging capabilites if we're in a terminal, otherwise we
// just get a standard go-kit logger.
l := term.NewLogger(syncWriter, log.NewLogfmtLogger, colorFn)
l = level.NewFilter(l, al.o)
l = log.With(l, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
return l
Expand Down