Skip to content

Refactor logger package and update logger configuration #19

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Feb 10, 2024
Merged
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
87 changes: 0 additions & 87 deletions logger/format.go

This file was deleted.

35 changes: 4 additions & 31 deletions logger/loggerconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package logger
// Ref: https://betterstack.com/community/guides/logging/go/zap/#logging-errors-with-zap

import (
"os"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand All @@ -23,7 +21,10 @@ func BuildLogger(logLevel LogLevel, logOutputFormat string) Logger {
// Set up custom encoder configuration
encoderCfg := zap.NewProductionEncoderConfig()
encoderCfg.TimeKey = "timestamp"
encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder // Use ISO8601 format for timestamps
encoderCfg.EncodeTime = zapcore.RFC3339TimeEncoder // ISO8601TimeEncoder /Use ISO8601 format for timestamps
encoderCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder // Encodes the level in uppercase (e.g., "INFO", "ERROR") with ANSI colour.
encoderCfg.EncodeCaller = zapcore.ShortCallerEncoder // Encodes the file and line number of the caller if the log level is at least DebugLevel
encoderCfg.EncodeName = zapcore.FullNameEncoder // Encodes the logger name

// Convert the custom LogLevel to zap's logging level
zapLogLevel := convertToZapLevel(logLevel)
Expand Down Expand Up @@ -62,34 +63,6 @@ func BuildLogger(logLevel LogLevel, logOutputFormat string) Logger {
}
}

func NewHumanReadableLogger(logLevel LogLevel, logOutputFormat string) Logger {
encoderCfg := zap.NewProductionEncoderConfig()
encoderCfg.TimeKey = "timestamp"
encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder

var encoder zapcore.Encoder
if logOutputFormat == LogOutputHumanReadable {
// For human-readable format, use a console encoder with customized level encoder
encoderCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
encoder = zapcore.NewConsoleEncoder(encoderCfg)
} else {
// Default to JSON encoder for structured logging
encoder = zapcore.NewJSONEncoder(encoderCfg)
}

core := zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), convertToZapLevel(logLevel))

// Wrap the core with customCore to maintain field reordering logic
wrappedCore := &customCore{Core: core}

logger := zap.New(wrappedCore, zap.AddCaller())

return &defaultLogger{
logger: logger,
logLevel: logLevel,
}
}

// convertToZapLevel converts the custom LogLevel to a zapcore.Level
func convertToZapLevel(level LogLevel) zapcore.Level {
switch level {
Expand Down