Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Dev #18

Merged
merged 3 commits into from
Feb 10, 2024
Merged

Dev #18

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
51 changes: 31 additions & 20 deletions logger/loggerconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package logger
// Ref: https://betterstack.com/community/guides/logging/go/zap/#logging-errors-with-zap

import (
"fmt"
"os"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand All @@ -28,16 +28,11 @@ func BuildLogger(logLevel LogLevel, logOutputFormat string) Logger {
// Convert the custom LogLevel to zap's logging level
zapLogLevel := convertToZapLevel(logLevel)

// Select the appropriate encoder based on the logOutputFormat
if logOutputFormat == LogOutputHumanReadable {
encoderCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder // For human-readable output, use colored level encoder
}

// Define the logger configuration
config := zap.Config{
Level: zap.NewAtomicLevelAt(zapLogLevel), // Default log level is Info
Development: false, // Set to true if the logger is used in a development environment
Encoding: "json", // Use JSON format for structured logging
Encoding: "console", // Supports 'json' and 'console' encodings
DisableCaller: true,
DisableStacktrace: true,
Sampling: nil,
Expand All @@ -53,20 +48,8 @@ func BuildLogger(logLevel LogLevel, logOutputFormat string) Logger {
//"application": version.GetAppName(),
},
}
/*
// Build the logger from the configuration
logger := zap.Must(config.Build())
*/
// Override Encoding for human-readable format
if logOutputFormat == LogOutputHumanReadable {
config.Encoding = "console"
}

// Build the logger from the configuration
logger, err := config.Build()
if err != nil {
panic(fmt.Sprintf("Failed to build logger: %v", err))
}
logger := zap.Must(config.Build())

// Wrap the original core with the custom core
wrappedCore := &customCore{logger.Core()}
Expand All @@ -79,6 +62,34 @@ 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