Skip to content

Commit 4210d81

Browse files
Add support for printing log messages in color
Fixes #6
1 parent 14fa97c commit 4210d81

File tree

6 files changed

+25
-1
lines changed

6 files changed

+25
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func main() {
2222
log.DebugMode = true
2323
log.DebugSQLMode = true
2424
log.PrintTimestamp = true
25+
log.PrintColors = true
2526
log.TimeFormat = "2006-01-02 15:04:05.000"
2627

2728
myVar := map[string]string{"hello": "world"}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ module github.com/pieterclaerhout/go-log
33
go 1.13
44

55
require (
6+
github.com/fatih/color v1.7.0
67
github.com/go-errors/errors v1.0.1
8+
github.com/mattn/go-colorable v0.1.4 // indirect
9+
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b
710
github.com/pieterclaerhout/go-formatter v1.0.2
811
github.com/pkg/errors v0.8.1
912
github.com/sanity-io/litter v1.1.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
4+
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
35
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
46
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
57
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=

logger.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import (
1616
// PrintTimestamp indicates if the log messages should include a timestamp or not
1717
var PrintTimestamp = false
1818

19+
// PrintColors indicates if the messages should be printed in color or not
20+
var PrintColors = false
21+
1922
// DebugMode indicates if debug information should be printed or not
2023
//
2124
// If the environment variable called DEBUG is set to 1, this will default to true.

logger_internal.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@ import (
66
"strings"
77
"sync"
88
"time"
9+
10+
"github.com/fatih/color"
911
)
1012

1113
var logMutex = &sync.Mutex{}
1214

15+
var colors = map[string]*color.Color{
16+
"DEBUG": color.New(color.FgHiBlack),
17+
"INFO ": color.New(color.FgHiGreen),
18+
"WARN ": color.New(color.FgHiYellow),
19+
"ERROR": color.New(color.FgHiRed),
20+
"FATAL": color.New(color.FgHiRed),
21+
}
22+
1323
func init() {
1424
TimeZone, _ = time.LoadLocation("Europe/Brussels")
1525
DebugMode = os.Getenv("DEBUG") == "1"
@@ -50,14 +60,18 @@ func printMessage(level string, message string) {
5060
formattedTime := tstamp.Format(TimeFormat)
5161
message = formattedTime + " | " + level + " | " + message
5262
}
63+
if PrintColors {
64+
if color, ok := colors[level]; ok {
65+
message = color.Sprint(message)
66+
}
67+
}
5368

5469
w := Stdout
5570
if level == "ERROR" || level == "FATAL" {
5671
w = Stderr
5772
}
5873

5974
w.Write([]byte(message + "\n"))
60-
// fmt.Fprint(w, message+"\n")
6175

6276
logMutex.Unlock()
6377

logger_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ func TestCheckError(t *testing.T) {
609609

610610
func resetLogConfig() {
611611
log.PrintTimestamp = true
612+
log.PrintColors = true
612613
log.DebugMode = false
613614
log.DebugSQLMode = false
614615
log.TimeZone, _ = time.LoadLocation("Europe/Brussels")

0 commit comments

Comments
 (0)