Added some files
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
var (
|
||||
// Logger is the global logger instance
|
||||
Logger *zap.Logger
|
||||
|
||||
// SugaredLogger is the global sugared logger instance
|
||||
SugaredLogger *zap.SugaredLogger
|
||||
)
|
||||
|
||||
// Init initializes the logger with the specified level and config
|
||||
func Init(level string, jsonOutput bool) {
|
||||
var config zap.Config
|
||||
|
||||
// Set the log level
|
||||
logLevel := zap.NewAtomicLevel()
|
||||
err := logLevel.UnmarshalText([]byte(level))
|
||||
if err != nil {
|
||||
logLevel.SetLevel(zap.InfoLevel)
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
// JSON output for Grafana Loki
|
||||
config = zap.Config{
|
||||
Level: logLevel,
|
||||
Development: false,
|
||||
Sampling: nil,
|
||||
Encoding: "json",
|
||||
EncoderConfig: zapcore.EncoderConfig{
|
||||
MessageKey: "msg",
|
||||
LevelKey: "level",
|
||||
TimeKey: "time",
|
||||
NameKey: "logger",
|
||||
CallerKey: "caller",
|
||||
FunctionKey: zapcore.OmitKey,
|
||||
StacktraceKey: "stacktrace",
|
||||
SkipLineEnding: false,
|
||||
LineEnding: zapcore.DefaultLineEnding,
|
||||
EncodeLevel: zapcore.LowercaseLevelEncoder,
|
||||
EncodeTime: zapcore.ISO8601TimeEncoder,
|
||||
EncodeDuration: zapcore.StringDurationEncoder,
|
||||
EncodeCaller: zapcore.ShortCallerEncoder,
|
||||
},
|
||||
OutputPaths: []string{"stdout"},
|
||||
ErrorOutputPaths: []string{"stderr"},
|
||||
InitialFields: map[string]interface{}{"service": "music-server"},
|
||||
}
|
||||
} else {
|
||||
// Human-readable output for development
|
||||
config = zap.Config{
|
||||
Level: logLevel,
|
||||
Development: true,
|
||||
Sampling: nil,
|
||||
Encoding: "console",
|
||||
EncoderConfig: zapcore.EncoderConfig{
|
||||
MessageKey: "msg",
|
||||
LevelKey: "level",
|
||||
TimeKey: "time",
|
||||
NameKey: "logger",
|
||||
CallerKey: "caller",
|
||||
FunctionKey: zapcore.OmitKey,
|
||||
StacktraceKey: "stacktrace",
|
||||
SkipLineEnding: false,
|
||||
LineEnding: zapcore.DefaultLineEnding,
|
||||
EncodeLevel: zapcore.CapitalLevelEncoder,
|
||||
EncodeTime: zapcore.ISO8601TimeEncoder,
|
||||
EncodeDuration: zapcore.StringDurationEncoder,
|
||||
EncodeCaller: zapcore.ShortCallerEncoder,
|
||||
},
|
||||
OutputPaths: []string{"stdout"},
|
||||
ErrorOutputPaths: []string{"stderr"},
|
||||
InitialFields: map[string]interface{}{"service": "music-server"},
|
||||
}
|
||||
}
|
||||
|
||||
logger, err := config.Build()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Logger = logger
|
||||
SugaredLogger = logger.Sugar()
|
||||
}
|
||||
|
||||
// GetLogger returns the global logger
|
||||
func GetLogger() *zap.Logger {
|
||||
if Logger == nil {
|
||||
Init("info", false)
|
||||
}
|
||||
return Logger
|
||||
}
|
||||
|
||||
// GetSugaredLogger returns the global sugared logger
|
||||
func GetSugaredLogger() *zap.SugaredLogger {
|
||||
if SugaredLogger == nil {
|
||||
Init("info", false)
|
||||
}
|
||||
return SugaredLogger
|
||||
}
|
||||
Reference in New Issue
Block a user