-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
243 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.