mutex for log race
@@ -6,11 +6,15 @@ import (
"path/filepath"
"runtime"
"strings"
+ "sync"
"github.com/rs/zerolog"
)
-var log zerolog.Logger
+var (
+ log zerolog.Logger
+ mu sync.RWMutex
+)
const (
timeFormat = "2006-01-02 15:04:05.000"
@@ -51,36 +55,51 @@ func Init(level string) {
funcName := strings.TrimPrefix(filepath.Ext((runtime.FuncForPC(caller).Name())), ".")
return fmt.Sprintf("[%d][%s][%s] => %s", line, fileName, funcName, i)
}
+
+ mu.Lock()
log = zerolog.New(output).Level(l).With().Timestamp().Logger()
+ mu.Unlock()
}
// Infof logs a formatted info level log to the console
func Infof(format string, v ...interface{}) {
+ mu.RLock()
+ defer mu.RUnlock()
log.Info().Msgf(format, v...)
}
// Tracef logs a formatted debug level log to the console
func Tracef(format string, v ...interface{}) {
+ mu.RLock()
+ defer mu.RUnlock()
log.Trace().Msgf(format, v...)
}
// Debugf logs a formatted debug level log to the console
func Debugf(format string, v ...interface{}) {
+ mu.RLock()
+ defer mu.RUnlock()
log.Debug().Msgf(format, v...)
}
// Warnf logs a formatted warn level log to the console
func Warnf(format string, v ...interface{}) {
+ mu.RLock()
+ defer mu.RUnlock()
log.Warn().Msgf(format, v...)
}
// Errorf logs a formatted error level log to the console
func Errorf(format string, v ...interface{}) {
+ mu.RLock()
+ defer mu.RUnlock()
log.Error().Msgf(format, v...)
}
// Panicf logs a formatted panic level log to the console.
// The panic() function is called, which stops the ordinary flow of a goroutine.
func Panicf(format string, v ...interface{}) {
+ mu.RLock()
+ defer mu.RUnlock()
log.Panic().Msgf(format, v...)
}
