Skip to content

Commit

Permalink
Fixed the color output on Windows
Browse files Browse the repository at this point in the history
Colored output seems to be broken on Windows Command Prompt
#8
  • Loading branch information
pieterclaerhout committed Dec 12, 2019
1 parent 23bf140 commit 411f652
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
1 change: 1 addition & 0 deletions cmd/go-log/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

func main() {

log.DebugMode = true
log.DebugSQLMode = true
log.PrintTimestamp = true
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.13
require (
github.com/fatih/color v1.7.0
github.com/go-errors/errors v1.0.1
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-colorable v0.1.4
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b
github.com/pieterclaerhout/go-formatter v1.0.2
github.com/pkg/errors v0.8.1
Expand Down
5 changes: 2 additions & 3 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package log

import (
"fmt"
"io"
"os"
"strings"
"time"
Expand Down Expand Up @@ -32,10 +31,10 @@ var DebugSQLMode = false
var TimeZone *time.Location

// Stdout is the writer to where the stdout messages should be written (defaults to os.Stdout)
var Stdout io.Writer = os.Stdout
var Stdout *os.File = os.Stdout

// Stderr is the writer to where the stderr messages should be written (defaults to os.Stderr)
var Stderr io.Writer = os.Stderr
var Stderr *os.File = os.Stderr

// DefaultTimeFormat is the default format to use for the timestamps
var DefaultTimeFormat = "2006-01-02 15:04:05.000"
Expand Down
18 changes: 9 additions & 9 deletions logger_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/fatih/color"
"github.com/mattn/go-colorable"
)

var logMutex = &sync.Mutex{}
Expand Down Expand Up @@ -48,8 +49,6 @@ func formatSeparator(message string, separator string, length int) string {

func printMessage(level string, message string) {

logMutex.Lock()

level = strings.ToUpper(level)

if PrintTimestamp {
Expand All @@ -60,20 +59,21 @@ func printMessage(level string, message string) {
formattedTime := tstamp.Format(TimeFormat)
message = formattedTime + " | " + level + " | " + message
}
if PrintColors {
if color, ok := colors[level]; ok {
message = color.Sprint(message)
}
}

w := Stdout
if level == "ERROR" || level == "FATAL" {
w = Stderr
}

w.Write([]byte(message + "\n"))
if PrintColors {
cw := colorable.NewColorable(w)
if c, ok := colors[level]; ok {
c.Fprintln(cw, message)
return
}
}

logMutex.Unlock()
w.Write([]byte(message + "\n"))

}

Expand Down

0 comments on commit 411f652

Please sign in to comment.