Skip to content

Commit 44f2533

Browse files
committed
Fixed locking
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent e76cead commit 44f2533

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

Diff for: go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
55
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
6+
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
67
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
78
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
89
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
10+
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
911
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
1012
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1113
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1214
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1315
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
1416
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
1517
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
18+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
1619
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
1720
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
1821
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
1922
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 h1:gKMu1Bf6QINDnvyZuTaACm9ofY+PRh+5vFz4oxBZeF8=
2023
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4/go.mod h1:50wTf68f99/Zt14pr046Tgt3Lp2vLyFZKzbFXTOabXw=
24+
github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8=
2125
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
2226
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
2327
golang.org/x/sys v0.0.0-20190531175056-4c3a928424d2 h1:T5DasATyLQfmbTpfEXx/IOL9vfjzW6up+ZDkmHvIf2s=

Diff for: log/log.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
type (
2222
Logger struct {
2323
prefix string
24-
level Lvl
24+
level uint8
2525
skip int
2626
output io.Writer
2727
template *fasttemplate.Template
@@ -37,7 +37,7 @@ type (
3737
)
3838

3939
const (
40-
DEBUG Lvl = iota + 1
40+
DEBUG uint8 = iota + 1
4141
INFO
4242
WARN
4343
ERROR
@@ -109,12 +109,12 @@ func (l *Logger) SetPrefix(p string) {
109109
l.prefix = p
110110
}
111111

112-
func (l *Logger) Level() Lvl {
112+
func (l *Logger) Level() uint8 {
113113
return l.level
114114
}
115115

116-
func (l *Logger) SetLevel(v Lvl) {
117-
l.level = v
116+
func (l *Logger) SetLevel(level uint8) {
117+
l.level = level
118118
}
119119

120120
func (l *Logger) Output() io.Writer {
@@ -243,12 +243,12 @@ func SetPrefix(p string) {
243243
global.SetPrefix(p)
244244
}
245245

246-
func Level() Lvl {
246+
func Level() uint8 {
247247
return global.Level()
248248
}
249249

250-
func SetLevel(v Lvl) {
251-
global.SetLevel(v)
250+
func SetLevel(level uint8) {
251+
global.SetLevel(level)
252252
}
253253

254254
func Output() io.Writer {
@@ -347,16 +347,14 @@ func Panicj(j JSON) {
347347
global.Panicj(j)
348348
}
349349

350-
func (l *Logger) log(v Lvl, format string, args ...interface{}) {
351-
l.mutex.Lock()
352-
defer l.mutex.Unlock()
353-
buf := l.bufferPool.Get().(*bytes.Buffer)
354-
buf.Reset()
355-
defer l.bufferPool.Put(buf)
356-
_, file, line, _ := runtime.Caller(l.skip)
357-
358-
if v >= l.level || v == 0 {
350+
func (l *Logger) log(level uint8, format string, args ...interface{}) {
351+
if level >= l.level || level == 0 {
352+
buf := l.bufferPool.Get().(*bytes.Buffer)
353+
buf.Reset()
354+
defer l.bufferPool.Put(buf)
355+
_, file, line, _ := runtime.Caller(l.skip)
359356
message := ""
357+
360358
if format == "" {
361359
message = fmt.Sprint(args...)
362360
} else if format == "json" {
@@ -376,7 +374,7 @@ func (l *Logger) log(v Lvl, format string, args ...interface{}) {
376374
case "time_rfc3339_nano":
377375
return w.Write([]byte(time.Now().Format(time.RFC3339Nano)))
378376
case "level":
379-
return w.Write([]byte(l.levels[v]))
377+
return w.Write([]byte(l.levels[level]))
380378
case "prefix":
381379
return w.Write([]byte(l.prefix))
382380
case "long_file":
@@ -409,6 +407,8 @@ func (l *Logger) log(v Lvl, format string, args ...interface{}) {
409407
buf.WriteString(message)
410408
}
411409
buf.WriteByte('\n')
410+
l.mutex.Lock()
411+
defer l.mutex.Unlock()
412412
l.output.Write(buf.Bytes())
413413
}
414414
}

0 commit comments

Comments
 (0)