Skip to content

Commit

Permalink
Merge pull request #58 from skit-ai/lamba/fix-logfmt
Browse files Browse the repository at this point in the history
fix: missing default logs bug; refactor: variable names
  • Loading branch information
ydlamba committed Jun 22, 2023
2 parents 91798f8 + c0d863d commit 4b83524
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
52 changes: 25 additions & 27 deletions log/levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ type Logger struct {
}

var (
// Legacy logger
defaultLogger = Logger{WARN}
// Logfmt logger
KitLogger = kitLog.NewNopLogger()
LogfmtLogger kitLog.Logger
)

// Prefix based on the log level to be added to every log statement
Expand Down Expand Up @@ -181,41 +179,41 @@ func (logger *Logger) Error(err error, args ...interface{}) {
// Methods to log a message using the default logger without a format

func Trace(args ...interface{}) {
if KitLogger != nil {
if LogfmtLogger != nil {
// When logfmt is enabled, level trace becomes debug
kitLevel.Debug(KitLogger).Log(args...)
kitLevel.Debug(LogfmtLogger).Log(args...)
return
}
defaultLogger.Trace(args...)
}

func Debug(args ...interface{}) {
if KitLogger != nil {
kitLevel.Debug(KitLogger).Log(args...)
if LogfmtLogger != nil {
kitLevel.Debug(LogfmtLogger).Log(args...)
return
}
defaultLogger.Debug(args...)
}

func Info(args ...interface{}) {
if KitLogger != nil {
kitLevel.Info(KitLogger).Log(args...)
if LogfmtLogger != nil {
kitLevel.Info(LogfmtLogger).Log(args...)
return
}
defaultLogger.Info(args...)
}

func Warn(args ...interface{}) {
if KitLogger != nil {
kitLevel.Warn(KitLogger).Log(args...)
if LogfmtLogger != nil {
kitLevel.Warn(LogfmtLogger).Log(args...)
return
}
defaultLogger.Warn(args...)
}

func Error(err error, args ...interface{}) {
if KitLogger != nil {
kitLevel.Error(KitLogger).Log(args...)
if LogfmtLogger != nil {
kitLevel.Error(LogfmtLogger).Log(args...)
return
}
defaultLogger.Error(err, args...)
Expand All @@ -226,41 +224,41 @@ func Error(err error, args ...interface{}) {

// TODO: remove format methods
func Tracef(format string, args ...interface{}) {
if KitLogger != nil {
if LogfmtLogger != nil {
// When logfmt is enabled, level trace becomes debug
kitLevel.Debug(KitLogger).Log(fmt.Sprintf(format, args...))
kitLevel.Debug(LogfmtLogger).Log(fmt.Sprintf(format, args...))
return
}
defaultLogger.Tracef(format, args...)
}

func Debugf(format string, args ...interface{}) {
if KitLogger != nil {
kitLevel.Debug(KitLogger).Log(fmt.Sprintf(format, args...))
if LogfmtLogger != nil {
kitLevel.Debug(LogfmtLogger).Log(fmt.Sprintf(format, args...))
return
}
defaultLogger.Debugf(format, args...)
}

func Infof(format string, args ...interface{}) {
if KitLogger != nil {
kitLevel.Info(KitLogger).Log(fmt.Sprintf(format, args...))
if LogfmtLogger != nil {
kitLevel.Info(LogfmtLogger).Log(fmt.Sprintf(format, args...))
return
}
defaultLogger.Infof(format, args...)
}

func Warnf(format string, args ...interface{}) {
if KitLogger != nil {
kitLevel.Warn(KitLogger).Log(fmt.Sprintf(format, args...))
if LogfmtLogger != nil {
kitLevel.Warn(LogfmtLogger).Log(fmt.Sprintf(format, args...))
return
}
defaultLogger.Warnf(format, args...)
}

func Errorf(err error, format string, args ...interface{}) {
if KitLogger != nil {
kitLevel.Error(KitLogger).Log(fmt.Sprintf(format, args...))
if LogfmtLogger != nil {
kitLevel.Error(LogfmtLogger).Log(fmt.Sprintf(format, args...))
return
}
defaultLogger.Errorf(err, format, args...)
Expand All @@ -271,22 +269,22 @@ func Errorf(err error, format string, args ...interface{}) {

func DebugWithTrace(ctx context.Context, args ...interface{}) {
args = append([]any{"trace_id", instruments.ExtractTraceID(ctx).String()}, args...)
kitLevel.Debug(KitLogger).Log(args...)
kitLevel.Debug(LogfmtLogger).Log(args...)
}

func InfoWithTrace(ctx context.Context, args ...interface{}) {
args = append([]any{"trace_id", instruments.ExtractTraceID(ctx).String()}, args...)
kitLevel.Info(KitLogger).Log(args...)
kitLevel.Info(LogfmtLogger).Log(args...)
}

func WarnWithTrace(ctx context.Context, args ...interface{}) {
args = append([]any{"trace_id", instruments.ExtractTraceID(ctx).String()}, args...)
kitLevel.Warn(KitLogger).Log(args...)
kitLevel.Warn(LogfmtLogger).Log(args...)
}

func ErrorWithTrace(ctx context.Context, args ...interface{}) {
args = append([]any{"trace_id", instruments.ExtractTraceID(ctx).String()}, args...)
kitLevel.Error(KitLogger).Log(args...)
kitLevel.Error(LogfmtLogger).Log(args...)
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
7 changes: 3 additions & 4 deletions log/logfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
"github.com/go-kit/log/level"
)

// InitLogfmtLogger initialises the global gokit logger and overrides the
// default logger for the server.
// InitLogfmtLogger initialises a logfmt logger from go-kit
func InitLogfmtLogger() {
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
// Using legacy logger's level for level filtering
logger = level.NewFilter(logger, LevelFilter(defaultLogger.level))
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
KitLogger = log.With(logger, "caller", log.Caller(4))
LogfmtLogger = log.With(logger, "caller", log.Caller(4))
}

// CheckFatal prints an error and exits with error code 1 if err is non-nil.
Expand All @@ -24,7 +23,7 @@ func CheckFatal(location string, err error) {
return
}

logger := level.Error(KitLogger)
logger := level.Error(LogfmtLogger)
if location != "" {
logger = log.With(logger, "msg", "error "+location)
}
Expand Down

0 comments on commit 4b83524

Please sign in to comment.