Skip to content

Commit

Permalink
log: use color.Error in log
Browse files Browse the repository at this point in the history
So that color codes work in windows for log messages.
  • Loading branch information
dnephin committed Aug 27, 2021
1 parent c728c6c commit c5a73e1
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package log

import (
"fmt"
"os"

"github.com/fatih/color"
)
Expand All @@ -16,15 +15,10 @@ const (
)

var (
level = WarnLevel
out stringWriter = os.Stderr
level = WarnLevel
out = color.Error
)

// TODO: replace with io.StringWriter once support for go1.11 is dropped.
type stringWriter interface {
WriteString(s string) (n int, err error)
}

// SetLevel for the global logger.
func SetLevel(l Level) {
level = l
Expand All @@ -35,36 +29,35 @@ func Warnf(format string, args ...interface{}) {
if level < WarnLevel {
return
}
out.WriteString(color.YellowString("WARN "))
out.WriteString(fmt.Sprintf(format, args...))
out.WriteString("\n")
fmt.Fprint(out, color.YellowString("WARN "))
fmt.Fprintf(out, format, args...)
fmt.Fprint(out, "\n")
}

// Debugf prints the message to stderr, with no prefix.
func Debugf(format string, args ...interface{}) {
if level < DebugLevel {
return
}
out.WriteString(fmt.Sprintf(format, args...))
out.WriteString("\n")
fmt.Fprintf(out, format, args...)
fmt.Fprint(out, "\n")
}

// Errorf prints the message to stderr, with a red ERROR prefix.
func Errorf(format string, args ...interface{}) {
if level < ErrorLevel {
return
}
out.WriteString(color.RedString("ERROR "))
out.WriteString(fmt.Sprintf(format, args...))
out.WriteString("\n")
fmt.Fprint(out, color.RedString("ERROR "))
fmt.Fprintf(out, format, args...)
fmt.Fprint(out, "\n")
}

// Error prints the message to stderr, with a red ERROR prefix.
func Error(msg string) {
if level < ErrorLevel {
return
}
out.WriteString(color.RedString("ERROR "))
out.WriteString(msg)
out.WriteString("\n")
fmt.Fprint(out, color.RedString("ERROR "))
fmt.Fprintln(out, msg)
}

0 comments on commit c5a73e1

Please sign in to comment.