synapsecns/sanguine

View on GitHub
core/metrics/logger/experimental.go

Summary

Maintainability
A
0 mins
Test Coverage
package logger

import (
    "context"
    "github.com/uptrace/opentelemetry-go-extra/otelzap"
    "go.uber.org/zap"
    "log"
)

// MakeZapLogger  is it's own method so envs/configs are persisted across metrics handlers.
func MakeZapLogger() *zap.Logger {
    betaLogger, err := zap.NewProduction()
    if err != nil {
        // since there are no options, this shouldn't really fail.
        log.Fatal("could not create beta logger", "error", err)
    }
    defer func() {
        _ = betaLogger.Sync()
    }()

    return betaLogger
}

// MakeWrappedSugaredLogger creates a new wrapped sugar logger.
func MakeWrappedSugaredLogger(sugaredLogger *otelzap.SugaredLogger) ExperimentalLogger {
    return &wrappedSugarLogger{sugaredLogger}
}

//go:generate go run github.com/vburenin/ifacemaker -f experimental.go -s wrappedSugarLogger  -i ExperimentalLogger  -p logger  -o logger_generated.go -c "autogenerated file"
type wrappedSugarLogger struct {
    underlyingLogger *otelzap.SugaredLogger
}

func (l *wrappedSugarLogger) Debugw(ctx context.Context, msg string, keysAndValues ...interface{}) {
    l.underlyingLogger.DebugwContext(ctx, msg, keysAndValues...)
}

func (l *wrappedSugarLogger) Debugf(ctx context.Context, template string, args ...interface{}) {
    l.underlyingLogger.DebugfContext(ctx, template, args...)
}

func (l *wrappedSugarLogger) Infow(ctx context.Context, msg string, keysAndValues ...interface{}) {
    l.underlyingLogger.InfowContext(ctx, msg, keysAndValues...)
}

func (l *wrappedSugarLogger) Infof(ctx context.Context, template string, args ...interface{}) {
    l.underlyingLogger.InfofContext(ctx, template, args...)
}

func (l *wrappedSugarLogger) Warnw(ctx context.Context, msg string, keysAndValues ...interface{}) {
    l.underlyingLogger.WarnwContext(ctx, msg, keysAndValues...)
}

func (l *wrappedSugarLogger) Warnf(ctx context.Context, template string, args ...interface{}) {
    l.underlyingLogger.WarnfContext(ctx, template, args...)
}

func (l *wrappedSugarLogger) Errorw(ctx context.Context, msg string, keysAndValues ...interface{}) {
    l.underlyingLogger.ErrorwContext(ctx, msg, keysAndValues...)
}

func (l *wrappedSugarLogger) Errorf(ctx context.Context, template string, args ...interface{}) {
    l.underlyingLogger.ErrorfContext(ctx, template, args...)
}

func (l *wrappedSugarLogger) Dpanicf(ctx context.Context, msg string, keysAndValues ...interface{}) {
    l.underlyingLogger.DPanicfContext(ctx, msg, keysAndValues...)
}

func (l *wrappedSugarLogger) Dpanicw(ctx context.Context, template string, args ...interface{}) {
    l.underlyingLogger.DPanicwContext(ctx, template, args...)
}

func (l *wrappedSugarLogger) Fatalf(ctx context.Context, msg string, keysAndValues ...interface{}) {
    l.underlyingLogger.FatalfContext(ctx, msg, keysAndValues...)
}

func (l *wrappedSugarLogger) Fatalw(ctx context.Context, template string, args ...interface{}) {
    l.underlyingLogger.FatalwContext(ctx, template, args...)
}

// WithOptions returns a new ExperimentalLogger with the given options applied.
func (l *wrappedSugarLogger) WithOptions(opts ...zap.Option) ExperimentalLogger {
    res := l.underlyingLogger.Desugar().WithOptions(opts...)
    return &wrappedSugarLogger{
        underlyingLogger: res.Sugar(),
    }
}

var _ ExperimentalLogger = &wrappedSugarLogger{}

// NewNullLogger creates a new null logger.
func NewNullLogger() ExperimentalLogger {
    logger := MakeZapLogger()
    return &nullLogger{underlyingLogger: logger.Sugar()}
}

type nullLogger struct {
    underlyingLogger *zap.SugaredLogger
}

func (n nullLogger) Debugw(_ context.Context, msg string, keysAndValues ...interface{}) {
    n.underlyingLogger.Debugw(msg, keysAndValues...)
}

func (n nullLogger) Debugf(_ context.Context, template string, args ...interface{}) {
    n.underlyingLogger.Debugf(template, args...)
}

func (n nullLogger) Infow(_ context.Context, msg string, keysAndValues ...interface{}) {
    n.underlyingLogger.Infow(msg, keysAndValues...)
}

func (n nullLogger) Infof(_ context.Context, template string, args ...interface{}) {
    n.underlyingLogger.Infof(template, args...)
}

func (n nullLogger) Warnw(_ context.Context, msg string, keysAndValues ...interface{}) {
    n.underlyingLogger.Warnw(msg, keysAndValues...)
}

func (n nullLogger) Warnf(_ context.Context, template string, args ...interface{}) {
    n.underlyingLogger.Warnf(template, args...)
}

func (n nullLogger) Errorw(_ context.Context, msg string, keysAndValues ...interface{}) {
    n.underlyingLogger.Errorw(msg, keysAndValues...)
}

func (n nullLogger) Errorf(_ context.Context, template string, args ...interface{}) {
    n.underlyingLogger.Errorf(template, args...)
}

func (n nullLogger) Dpanicf(_ context.Context, msg string, keysAndValues ...interface{}) {
    n.underlyingLogger.DPanicf(msg, keysAndValues...)
}

func (n nullLogger) Dpanicw(_ context.Context, template string, args ...interface{}) {
    n.underlyingLogger.DPanicw(template, args...)
}

func (n nullLogger) Fatalf(_ context.Context, msg string, keysAndValues ...interface{}) {
    n.underlyingLogger.Fatalf(msg, keysAndValues...)
}

func (n nullLogger) Fatalw(_ context.Context, template string, args ...interface{}) {
    n.underlyingLogger.Fatalw(template, args...)
}

func (n nullLogger) WithOptions(opts ...zap.Option) ExperimentalLogger {
    return nullLogger{n.underlyingLogger.WithOptions(opts...)}
}

var _ ExperimentalLogger = &nullLogger{}