Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove un-necessary locking (#1) #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"runtime"
"strconv"
"sync"
"sync/atomic"
"time"

"github.com/mattn/go-isatty"
Expand All @@ -21,7 +22,7 @@ import (
type (
Logger struct {
prefix string
level Lvl
level uint32
skip int
output io.Writer
template *fasttemplate.Template
Expand Down Expand Up @@ -58,7 +59,7 @@ func init() {

func New(prefix string) (l *Logger) {
l = &Logger{
level: INFO,
level: uint32(INFO),
skip: 2,
prefix: prefix,
template: l.newTemplate(defaultHeader),
Expand Down Expand Up @@ -110,11 +111,11 @@ func (l *Logger) SetPrefix(p string) {
}

func (l *Logger) Level() Lvl {
return l.level
return Lvl(atomic.LoadUint32(&l.level))
}

func (l *Logger) SetLevel(v Lvl) {
l.level = v
atomic.StoreUint32(&l.level, uint32(v))
}

func (l *Logger) Output() io.Writer {
Expand Down Expand Up @@ -348,14 +349,16 @@ func Panicj(j JSON) {
}

func (l *Logger) log(v Lvl, format string, args ...interface{}) {
l.mutex.Lock()
defer l.mutex.Unlock()
buf := l.bufferPool.Get().(*bytes.Buffer)
buf.Reset()
defer l.bufferPool.Put(buf)
_, file, line, _ := runtime.Caller(l.skip)

if v >= l.level || v == 0 {
logLevel := l.Level()

if v >= logLevel || v == 0 {
l.mutex.Lock()
defer l.mutex.Unlock()
buf := l.bufferPool.Get().(*bytes.Buffer)
buf.Reset()
defer l.bufferPool.Put(buf)
_, file, line, _ := runtime.Caller(l.skip)

message := ""
if format == "" {
message = fmt.Sprint(args...)
Expand Down