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

fix: alternative Logger utilization #75

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
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
32 changes: 24 additions & 8 deletions quartz/logger/default_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,41 @@ package logger
import (
"log"
"os"
"sync/atomic"
"sync"
)

var defaultLogger atomic.Value
type loggerValue struct {
sync.RWMutex
logger Logger
}

func (l *loggerValue) getLogger() Logger {
l.RLock()
defer l.RUnlock()
return l.logger
}

func (l *loggerValue) setLogger(new Logger) {
l.Lock()
defer l.Unlock()
l.logger = new
}

func init() {
defaultLogger.Store(NewSimpleLogger(
log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile), LevelInfo),
)
var defaultLogger = loggerValue{
logger: NewSimpleLogger(
log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile),
LevelInfo,
),
}

// Default returns the default Logger.
func Default() Logger {
return defaultLogger.Load().(Logger)
return defaultLogger.getLogger()
}

// SetDefault makes l the default Logger.
func SetDefault(l Logger) {
defaultLogger.Store(l)
defaultLogger.setLogger(l)
}

// Trace logs at LevelTrace.
Expand Down
98 changes: 94 additions & 4 deletions quartz/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"log"
"strings"
"sync"
"testing"

"github.com/reugn/go-quartz/quartz/logger"
Expand Down Expand Up @@ -47,14 +48,53 @@ func TestLoggerOff(t *testing.T) {
logger.SetDefault(logger.NewSimpleLogger(stdLogger, logger.LevelOff))

if logger.Enabled(logger.LevelError) {
t.Fatal("LevelError is enabled")
t.Fatal("logger.LevelError is enabled")
}
logger.Error("Error")
assertEmpty(&b, t)
logger.Errorf("Error%s", "f")
assertEmpty(&b, t)
}

func TestLoggerRace(t *testing.T) {
var b bytes.Buffer
stdLogger := log.New(&b, "", log.LstdFlags)

logger1 := logger.NewSimpleLogger(stdLogger, logger.LevelOff)
logger2 := logger.NewSimpleLogger(stdLogger, logger.LevelTrace)
logger3 := logger.NewSimpleLogger(stdLogger, logger.LevelDebug)

wg := sync.WaitGroup{}
wg.Add(3)
go setLogger(&wg, logger1)
go setLogger(&wg, logger2)
go setLogger(&wg, logger3)
wg.Wait()
wg.Add(1)
go setLogger(&wg, logger2)
wg.Wait()

if logger.Default() != logger2 {
t.Fatal("logger set race error")
}
}

func setLogger(wg *sync.WaitGroup, l *logger.SimpleLogger) {
defer wg.Done()
logger.SetDefault(l)
}

func TestCustomLogger(t *testing.T) {
l := &countingLogger{}
logger.SetDefault(l)
logger.Debug("debug")
logger.Info("info")
logger.Error("error")
if l.Count != 3 {
t.Fatal("custom logger error")
}
}

func TestLogFormat(t *testing.T) {
var b bytes.Buffer
stdLogger := log.New(&b, "", log.LstdFlags)
Expand Down Expand Up @@ -95,21 +135,21 @@ func TestLogFormat(t *testing.T) {
func assertEmpty(r io.Reader, t *testing.T) {
logMsg := readAll(r, t)
if logMsg != "" {
t.Fatalf("Log msg is not empty: %s", logMsg)
t.Fatalf("log msg is not empty: %s", logMsg)
}
}

func assertNotEmpty(r io.Reader, t *testing.T) {
logMsg := readAll(r, t)
if logMsg == "" {
t.Fatal("Log msg is empty")
t.Fatal("log msg is empty")
}
}

func checkLogFormat(r io.Reader, t *testing.T) {
logMsg := readAll(r, t)
if !strings.Contains(logMsg, "a, 1, true, {}") {
t.Fatalf("Invalid log format: %s", logMsg)
t.Fatalf("invalid log format: %s", logMsg)
}
}

Expand All @@ -120,3 +160,53 @@ func readAll(r io.Reader, t *testing.T) string {
}
return string(bytes)
}

type countingLogger struct {
Count int
}

var _ logger.Logger = (*countingLogger)(nil)

func (l *countingLogger) Trace(_ any) {
l.Count++
}

func (l *countingLogger) Tracef(_ string, _ ...any) {
l.Count++
}

func (l *countingLogger) Debug(_ any) {
l.Count++
}

func (l *countingLogger) Debugf(_ string, _ ...any) {
l.Count++
}

func (l *countingLogger) Info(_ any) {
l.Count++
}

func (l *countingLogger) Infof(_ string, _ ...any) {
l.Count++
}

func (l *countingLogger) Warn(_ any) {
l.Count++
}

func (l *countingLogger) Warnf(_ string, _ ...any) {
l.Count++
}

func (l *countingLogger) Error(_ any) {
l.Count++
}

func (l *countingLogger) Errorf(_ string, _ ...any) {
l.Count++
}

func (l *countingLogger) Enabled(_ logger.Level) bool {
return true
}
Loading