Skip to content

Commit

Permalink
Add support for printing log messages in color
Browse files Browse the repository at this point in the history
Fixes #6
  • Loading branch information
pieterclaerhout committed Oct 26, 2019
1 parent 14fa97c commit 4210d81
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func main() {
log.DebugMode = true
log.DebugSQLMode = true
log.PrintTimestamp = true
log.PrintColors = true
log.TimeFormat = "2006-01-02 15:04:05.000"

myVar := map[string]string{"hello": "world"}
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ module github.com/pieterclaerhout/go-log
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/mgutz/ansi v0.0.0-20170206155736-9520e82c474b
github.com/pieterclaerhout/go-formatter v1.0.2
github.com/pkg/errors v0.8.1
github.com/sanity-io/litter v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
Expand Down
3 changes: 3 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
// PrintTimestamp indicates if the log messages should include a timestamp or not
var PrintTimestamp = false

// PrintColors indicates if the messages should be printed in color or not
var PrintColors = false

// DebugMode indicates if debug information should be printed or not
//
// If the environment variable called DEBUG is set to 1, this will default to true.
Expand Down
16 changes: 15 additions & 1 deletion logger_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ import (
"strings"
"sync"
"time"

"github.com/fatih/color"
)

var logMutex = &sync.Mutex{}

var colors = map[string]*color.Color{
"DEBUG": color.New(color.FgHiBlack),
"INFO ": color.New(color.FgHiGreen),
"WARN ": color.New(color.FgHiYellow),
"ERROR": color.New(color.FgHiRed),
"FATAL": color.New(color.FgHiRed),
}

func init() {
TimeZone, _ = time.LoadLocation("Europe/Brussels")
DebugMode = os.Getenv("DEBUG") == "1"
Expand Down Expand Up @@ -50,14 +60,18 @@ 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"))
// fmt.Fprint(w, message+"\n")

logMutex.Unlock()

Expand Down
1 change: 1 addition & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ func TestCheckError(t *testing.T) {

func resetLogConfig() {
log.PrintTimestamp = true
log.PrintColors = true
log.DebugMode = false
log.DebugSQLMode = false
log.TimeZone, _ = time.LoadLocation("Europe/Brussels")
Expand Down

0 comments on commit 4210d81

Please sign in to comment.