Skip to content

Commit c4ff054

Browse files
authored
Merge pull request #10 from randlabs/improved
Redesign and improvements
2 parents 8f746bc + e1805c3 commit c4ff054

24 files changed

+991
-606
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@
2222
app
2323
demo
2424

25+
testdata/*
26+
!testdata/.gitkeep
27+
2528
vendor/*

console/console_logger.go

Lines changed: 0 additions & 53 deletions
This file was deleted.

file_logger/file_logger.go

Lines changed: 0 additions & 182 deletions
This file was deleted.

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/randlabs/go-logger
22

33
go 1.13
44

5-
require github.com/gookit/color v1.3.5
5+
require (
6+
github.com/gookit/color v1.5.0
7+
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f // indirect
8+
)

go.sum

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
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/gookit/color v1.3.5 h1:1nszcmDVrfti1Su5fhtuS5YBs/Xs6v8UIi0bJ/2oDHY=
4-
github.com/gookit/color v1.3.5/go.mod h1:GqqLKF1le3EfrbHbYsYa5WdLqfc/PHMdMRbt6tMnqIc=
3+
github.com/gookit/color v1.5.0 h1:1Opow3+BWDwqor78DcJkJCIwnkviFi+rrOANki9BUFw=
4+
github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo=
55
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
66
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7-
github.com/randlabs/server-watchdog-go v1.0.0 h1:Rk0y8scYH/OOZ1IwskwmgS8zA6OXA75ugOdCgaZxIX0=
8-
github.com/randlabs/server-watchdog-go v1.0.0/go.mod h1:E50+xlCEYqmA/nnJnyPoqCHBenzFEA9KAVChG9/6MpI=
97
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
10-
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
11-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
8+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
9+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
11+
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
12+
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
13+
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f h1:8w7RhxzTVgUzw/AH/9mUV5q0vMgy40SQRursCcfmkCw=
14+
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
16+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
17+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
18+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/console/console.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package console
2+
3+
import (
4+
"io"
5+
"os"
6+
"sync"
7+
"time"
8+
9+
"github.com/gookit/color"
10+
"github.com/randlabs/go-logger/internal/util"
11+
)
12+
13+
//------------------------------------------------------------------------------
14+
15+
var mtx = sync.Mutex{}
16+
17+
//------------------------------------------------------------------------------
18+
19+
// LogError prints an error message on screen.
20+
func LogError(now time.Time, msg string, isJSON bool) {
21+
if !isJSON {
22+
logPrint(os.Stderr, color.Error, now, "ERROR", msg)
23+
} else {
24+
logPrintJSON(os.Stderr, now, "error", msg)
25+
}
26+
}
27+
28+
// LogWarning prints a warning message on screen.
29+
func LogWarning(now time.Time, msg string, isJSON bool) {
30+
if !isJSON {
31+
logPrint(os.Stderr, color.Warn, now, "WARNING", msg)
32+
} else {
33+
logPrintJSON(os.Stderr, now, "warning", msg)
34+
}
35+
}
36+
37+
// LogInfo prints an information message on screen.
38+
func LogInfo(now time.Time, msg string, isJSON bool) {
39+
if !isJSON {
40+
logPrint(os.Stdout, color.Info, now, "INFO", msg)
41+
} else {
42+
logPrintJSON(os.Stdout, now, "info", msg)
43+
}
44+
}
45+
46+
// LogDebug prints a debug message on screen.
47+
func LogDebug(now time.Time, msg string, isJSON bool) {
48+
if !isJSON {
49+
logPrint(os.Stdout, color.Debug, now, "DEBUG", msg)
50+
} else {
51+
logPrintJSON(os.Stdout, now, "debug", msg)
52+
}
53+
}
54+
55+
//------------------------------------------------------------------------------
56+
57+
func logPrint(w io.Writer, theme *color.Theme, now time.Time, level string, msg string) {
58+
// Lock console access
59+
mtx.Lock()
60+
61+
// Print the message prefixed with the timestamp and level
62+
color.Fprintf(w, "%v %v %v\n", now.Format("2006-01-02 15:04:05.000"), theme.Sprintf("[%v]", level), msg)
63+
64+
// Unlock console access
65+
mtx.Unlock()
66+
}
67+
68+
func logPrintJSON(w io.Writer, now time.Time, level string, msg string) {
69+
// Lock console access
70+
mtx.Lock()
71+
72+
// Print the message with extra payload
73+
color.Fprintf(w, "%v\n", util.AddPayloadToJSON(msg, &now, level))
74+
75+
// Unlock console access
76+
mtx.Unlock()
77+
}

0 commit comments

Comments
 (0)