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

feat: create zap logger on demand #269

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
99 changes: 99 additions & 0 deletions pkg/log/lazy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package log

import "go.uber.org/zap"

type lazyLogger struct {
zapFactory func() (*zap.SugaredLogger, error)
zapLogger *zap.SugaredLogger
}

// NewLazyLogger creates a new lazy logger with the specified function to call to create zap logger
func NewLazyLogger(zapFactory func() (*zap.SugaredLogger, error)) lazyLogger {
return lazyLogger{zapFactory: zapFactory}
}

// ZapLogger creates a new zap logger if no logger was created yet
func (l *lazyLogger) ZapLogger() *zap.SugaredLogger {
if l.zapLogger == nil {
l.zapLogger, _ = l.zapFactory() // no:lint: errcheck
}
return l.zapLogger
}

// Debug uses fmt.Sprint to construct and log a message.
func (l *lazyLogger) Debug(args ...interface{}) {
l.ZapLogger().Debug(args...)
}

// Debugf uses fmt.Sprintf to log a templated message.
func (l *lazyLogger) Debugf(template string, args ...interface{}) {
l.ZapLogger().Debugf(template, args...)
}

// Debugw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
//
// When debug-level logging is disabled, this is much faster than
//
// s.With(keysAndValues).Debug(msg)
func (l *lazyLogger) Debugw(msg string, keysAndValues ...interface{}) {
l.ZapLogger().Debugw(msg, keysAndValues...)
}

// Info uses fmt.Sprint to log a templated message.
func (l *lazyLogger) Info(args ...interface{}) {
l.ZapLogger().Info(args...)
}

// Infof uses fmt.Sprintf to log a templated message.
func (l *lazyLogger) Infof(template string, args ...interface{}) {
l.ZapLogger().Infof(template, args...)
}

// Infow logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (l *lazyLogger) Infow(msg string, keysAndValues ...interface{}) {
l.ZapLogger().Infow(msg, keysAndValues...)
}

// Warn uses fmt.Sprint to log a templated message.
func (l *lazyLogger) Warn(args ...interface{}) {
l.ZapLogger().Warn(args...)
}

// Warnf uses fmt.Sprintf to log a templated message.
func (l *lazyLogger) Warnf(template string, args ...interface{}) {
l.ZapLogger().Warnf(template, args...)
}

// Warnw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (l *lazyLogger) Warnw(msg string, keysAndValues ...interface{}) {
l.ZapLogger().Warnw(msg, keysAndValues...)
}

// Error uses fmt.Sprint to log a templated message.
func (l *lazyLogger) Error(args ...interface{}) {
l.ZapLogger().Error(args...)
}

// Errorf uses fmt.Sprintf to log a templated message.
func (l *lazyLogger) Errorf(template string, args ...interface{}) {
l.ZapLogger().Errorf(template, args...)
}

// Errorw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (l *lazyLogger) Errorw(msg string, keysAndValues ...interface{}) {
l.ZapLogger().Errorw(msg, keysAndValues...)
}

// Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.
func (l *lazyLogger) Fatal(args ...interface{}) {
l.ZapLogger().Fatal(args...)
}

// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
func (l *lazyLogger) Fatalf(template string, args ...interface{}) {
l.ZapLogger().Fatalf(template, args...)
}
15 changes: 8 additions & 7 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"go.uber.org/zap"
)

var Logger *zap.SugaredLogger

func init() {
var Logger = NewLazyLogger(func() (*zap.SugaredLogger, error) {
config := zap.Config{
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
Development: false,
Expand All @@ -15,10 +13,13 @@ func init() {
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}
logger, _ := config.Build()
Logger = logger.Sugar()
}
logger, err := config.Build()
if err != nil {
return nil, err
}
return logger.Sugar(), nil
})

func SetLogger(l *zap.SugaredLogger) {
Logger = l
Logger.zapLogger = l
}