Skip to content

Commit

Permalink
clean up interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
d80tb7 committed Jan 4, 2025
1 parent b660df0 commit 42baeae
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 230 deletions.
2 changes: 1 addition & 1 deletion internal/common/armadacontext/armada_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func WithTimeout(parent *Context, timeout time.Duration) (*Context, context.Canc
}

// WithLogField returns a copy of parent with the supplied key-value added to the logger
func WithLogField(parent *Context, key string, val interface{}) *Context {
func WithLogField(parent *Context, key string, val any) *Context {
return &Context{
Context: parent.Context,
logger: parent.logger.WithField(key, val),
Expand Down
8 changes: 7 additions & 1 deletion internal/common/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,13 @@ func panicRecoveryHandler(p interface{}) (err error) {
func InterceptorLogger() grpc_logging.Logger {
return grpc_logging.LoggerFunc(func(ctx context.Context, lvl grpc_logging.Level, msg string, fields ...any) {
armadaCtx := armadacontext.FromGrpcCtx(ctx)
l := armadaCtx.Logger().WithField(fields...)
logFields := make(map[string]any, len(fields)/2)
i := grpc_logging.Fields(fields).Iterator()
for i.Next() {
k, v := i.At()
logFields[k] = v
}
l := armadacontext.WithLogFields(armadaCtx, logFields)
switch lvl {
case grpc_logging.LevelDebug:
l.Debug(msg)
Expand Down
93 changes: 93 additions & 0 deletions internal/common/logging/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package logging

import (
"go.uber.org/zap"
)

// The default Logger
var stdLogger = &Logger{zap.S()}

// ReplaceStdLogger Replaces the defualt logger. This should be called once at app startup!

Check failure on line 10 in internal/common/logging/global.go

View workflow job for this annotation

GitHub Actions / lint / Lint Go

`defualt` is a misspelling of `default` (misspell)
func ReplaceStdLogger(l *Logger) {
stdLogger = l
}

// StdLogger Returns the default logger
func StdLogger() *Logger {
return stdLogger
}

// Debug logs a message at level Debug.
func Debug(args ...any) {
stdLogger.Debug(args...)
}

// Info logs a message at level Info.
func Info(args ...any) {
stdLogger.Info(args...)
}

// Warn logs a message at level Warn on the standard logger.
func Warn(args ...any) {
stdLogger.Warn(args...)
}

// Error logs a message at level Error on the standard logger.
func Error(args ...any) {
stdLogger.Error(args...)
}

// Panic logs a message at level Panic on the standard logger.
func Panic(args ...any) {
stdLogger.Panic(args...)
}

// Fatal logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
func Fatal(args ...any) {
stdLogger.Fatal(args...)
}

// Debugf logs a message at level Debug on the standard logger.
func Debugf(format string, args ...any) {
stdLogger.Debugf(format, args...)
}

// Infof logs a message at level Info on the standard logger.
func Infof(format string, args ...any) {
stdLogger.Infof(format, args...)
}

// Warnf logs a message at level Warn on the standard logger.
func Warnf(format string, args ...any) {
stdLogger.Warnf(format, args...)
}

// Errorf logs a message at level Error on the standard logger.
func Errorf(format string, args ...any) {
stdLogger.Errorf(format, args...)
}

// Fatalf logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
func Fatalf(format string, args ...any) {
stdLogger.Fatalf(format, args...)
}

// WithField returns a new Logger with the key-value pair added as a new field
func WithField(key string, value any) *Logger {
return stdLogger.WithField(key, value)
}

// WithFields returns a new Logger with all key-value pairs in the map added as new fields
func WithFields(args map[string]any) *Logger {
return stdLogger.WithFields(args)
}

// WithError returns a new Logger with the error added as a field
func WithError(err error) *Logger {
return stdLogger.WithError(err)
}

// WithStacktrace returns a new Logger with the error and (if available) the stacktrace added as fields
func WithStacktrace(err error) *Logger {
return stdLogger.WithStacktrace(err)
}
120 changes: 120 additions & 0 deletions internal/common/logging/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package logging

import (
"github.com/pkg/errors"
"go.uber.org/zap"
)

// Logger wraps a *zap.SugaredLogger so that the rest of the code doesn't depend directly on Zap
type Logger struct {
undlerlying *zap.SugaredLogger
}

// FromZap returns a New Logger backed by the supplied *zap.SugaredLogger
func FromZap(l *zap.Logger) *Logger {
return &Logger{
undlerlying: l.Sugar(),
}
}

// Debug logs a message at level Debug
func (l *Logger) Debug(args ...any) {
l.undlerlying.Debug(args...)
}

// Info logs a message at level Info
func (l *Logger) Info(args ...any) {
l.undlerlying.Info(args...)
}

// Warn logs a message at level Warn
func (l *Logger) Warn(args ...any) {
l.undlerlying.Warn(args...)
}

// Error logs a message at level Error
func (l *Logger) Error(args ...any) {
l.undlerlying.Error(args...)
}

// Panic logs a message at level Panic
func (l *Logger) Panic(args ...any) {
l.undlerlying.Panic(args...)
}

// Fatal logs a message at level Fatal then the process will exit with status set to 1.
func (l *Logger) Fatal(args ...any) {
l.undlerlying.Fatal(args...)
}

// Debugf logs a message at level Debug.
func (l *Logger) Debugf(format string, args ...interface{}) {
l.undlerlying.Debugf(format, args...)
}

// Infof logs a message at level Info.
func (l *Logger) Infof(format string, args ...interface{}) {
l.undlerlying.Infof(format, args...)
}

// Warnf logs a message at level Warn.
func (l *Logger) Warnf(format string, args ...interface{}) {
l.undlerlying.Warnf(format, args...)
}

// Errorf logs a message at level Error.
func (l *Logger) Errorf(format string, args ...interface{}) {
l.undlerlying.Errorf(format, args...)
}

// Fatalf logs a message at level Fatal.
func (l *Logger) Fatalf(format string, args ...interface{}) {
l.undlerlying.Fatalf(format, args...)
}

// WithError returns a new Logger with the error added as a field
func (l *Logger) WithError(err error) *Logger {
return l.WithField("error", err)
}

// WithStacktrace returns a new Logger with the error and (if available) the stacktrace added as fields
func (l *Logger) WithStacktrace(err error) *Logger {
logger := l.WithError(err)
if stackErr, ok := err.(stackTracer); ok {
return logger.WithField("stacktrace", stackErr.StackTrace())
} else {
return logger
}
}

// WithField returns a new Logger with the key-value pair added as a new field
func (l *Logger) WithField(key string, value any) *Logger {
return &Logger{
undlerlying: l.undlerlying.With(key, value),
}
}

// WithFields returns a new Logger with all key-value pairs in the map added as new fields
func (l *Logger) WithFields(args map[string]any) *Logger {
fields := make([]any, 0, len(args))
for key, value := range args {
fields = append(fields, zap.Any(key, value))
}
return &Logger{
undlerlying: l.undlerlying.With(fields...),
}
}

// WithCallerSkip returns a new Logger with the number of callers skipped increased by the skip amount.
// This is needed when building wrappers around the Logger so as to prevent us from always reporting the
// wrapper code as the caller.
func (l *Logger) WithCallerSkip(skip int) *Logger {
return &Logger{
undlerlying: l.undlerlying.WithOptions(zap.AddCallerSkip(skip)),
}
}

// Unexported but considered part of the stable interface of pkg/errors.
type stackTracer interface {
StackTrace() errors.StackTrace
}
Loading

0 comments on commit 42baeae

Please sign in to comment.