fix: logging module comments
This commit is contained in:
124
pkg/log/log.go
124
pkg/log/log.go
@@ -1,6 +1,6 @@
|
||||
package log
|
||||
package log
|
||||
|
||||
import (
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -13,57 +13,57 @@
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
)
|
||||
|
||||
type Level string
|
||||
type DestinationType string
|
||||
type Destination struct {
|
||||
type Level string
|
||||
type DestinationType string
|
||||
type Destination struct {
|
||||
Type DestinationType `yaml:"type"`
|
||||
Config map[string]string `yaml:"config"`
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
const (
|
||||
Stdout DestinationType = "stdout"
|
||||
File DestinationType = "file"
|
||||
)
|
||||
)
|
||||
|
||||
const (
|
||||
const (
|
||||
DebugLevel Level = "debug"
|
||||
InfoLevel Level = "info"
|
||||
WarnLevel Level = "warn"
|
||||
ErrorLevel Level = "error"
|
||||
FatalLevel Level = "fatal"
|
||||
PanicLevel Level = "panic"
|
||||
)
|
||||
)
|
||||
|
||||
var logLevels = map[Level]zerolog.Level{
|
||||
var logLevels = map[Level]zerolog.Level{
|
||||
DebugLevel: zerolog.DebugLevel,
|
||||
InfoLevel: zerolog.InfoLevel,
|
||||
WarnLevel: zerolog.WarnLevel,
|
||||
ErrorLevel: zerolog.ErrorLevel,
|
||||
FatalLevel: zerolog.FatalLevel,
|
||||
PanicLevel: zerolog.PanicLevel,
|
||||
}
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
type Config struct {
|
||||
level Level `yaml:"level"`
|
||||
destinations []Destination `yaml:"destinations"`
|
||||
contextKeys []any `yaml:"contextKeys"`
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
var (
|
||||
logger zerolog.Logger
|
||||
cfg Config
|
||||
once sync.Once
|
||||
)
|
||||
)
|
||||
|
||||
var (
|
||||
var (
|
||||
ErrInvalidLogLevel = errors.New("invalid log level")
|
||||
ErrLogDestinationNil = errors.New("log Destinations cant be empty")
|
||||
ErrMissingFilePath = errors.New("file path missing in destination config for file logging")
|
||||
)
|
||||
)
|
||||
|
||||
func (config *Config) validate() error {
|
||||
func (config *Config) validate() error {
|
||||
if _, exists := logLevels[config.level]; !exists {
|
||||
return ErrInvalidLogLevel
|
||||
}
|
||||
@@ -92,21 +92,21 @@
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var defaultConfig = Config{
|
||||
var defaultConfig = Config{
|
||||
level: InfoLevel,
|
||||
destinations: []Destination{
|
||||
{Type: Stdout},
|
||||
},
|
||||
contextKeys: []any{"userID", "requestID"},
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
func init() {
|
||||
logger, _ = getLogger(defaultConfig)
|
||||
}
|
||||
}
|
||||
|
||||
func getLogger(config Config) (zerolog.Logger, error) {
|
||||
func getLogger(config Config) (zerolog.Logger, error) {
|
||||
var newLogger zerolog.Logger
|
||||
var writers []io.Writer
|
||||
for _, dest := range config.destinations {
|
||||
@@ -132,7 +132,7 @@
|
||||
if compress, ok := dest.Config["compress"]; ok {
|
||||
lumberjackLogger.Compress = compress == "true"
|
||||
}
|
||||
Info(context.Background(),"here")
|
||||
Info(context.Background(), "here")
|
||||
writers = append(writers, lumberjackLogger)
|
||||
|
||||
}
|
||||
@@ -147,8 +147,8 @@
|
||||
|
||||
cfg = config
|
||||
return newLogger, nil
|
||||
}
|
||||
func InitLogger(c Config) error {
|
||||
}
|
||||
func InitLogger(c Config) error {
|
||||
|
||||
if err := c.validate(); err != nil {
|
||||
return err
|
||||
@@ -160,62 +160,62 @@
|
||||
logger, initErr = getLogger(c)
|
||||
})
|
||||
return initErr
|
||||
}
|
||||
func Debug(ctx context.Context, msg string) {
|
||||
}
|
||||
func Debug(ctx context.Context, msg string) {
|
||||
logEvent(ctx, zerolog.DebugLevel, msg, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func Debugf(ctx context.Context, format string, v ...any) {
|
||||
func Debugf(ctx context.Context, format string, v ...any) {
|
||||
msg := fmt.Sprintf(format, v...)
|
||||
logEvent(ctx, zerolog.DebugLevel, msg, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func Info(ctx context.Context, msg string) {
|
||||
func Info(ctx context.Context, msg string) {
|
||||
logEvent(ctx, zerolog.InfoLevel, msg, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func Infof(ctx context.Context, format string, v ...any) {
|
||||
func Infof(ctx context.Context, format string, v ...any) {
|
||||
msg := fmt.Sprintf(format, v...)
|
||||
logEvent(ctx, zerolog.InfoLevel, msg, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func Warn(ctx context.Context, msg string) {
|
||||
func Warn(ctx context.Context, msg string) {
|
||||
logEvent(ctx, zerolog.WarnLevel, msg, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func Warnf(ctx context.Context, format string, v ...any) {
|
||||
func Warnf(ctx context.Context, format string, v ...any) {
|
||||
msg := fmt.Sprintf(format, v...)
|
||||
logEvent(ctx, zerolog.WarnLevel, msg, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func Error(ctx context.Context, err error, msg string) {
|
||||
func Error(ctx context.Context, err error, msg string) {
|
||||
logEvent(ctx, zerolog.ErrorLevel, msg, err)
|
||||
}
|
||||
}
|
||||
|
||||
func Errorf(ctx context.Context, err error, format string, v ...any) {
|
||||
func Errorf(ctx context.Context, err error, format string, v ...any) {
|
||||
msg := fmt.Sprintf(format, v...)
|
||||
logEvent(ctx, zerolog.ErrorLevel, msg, err)
|
||||
}
|
||||
}
|
||||
|
||||
func Fatal(ctx context.Context, err error, msg string) {
|
||||
func Fatal(ctx context.Context, err error, msg string) {
|
||||
logEvent(ctx, zerolog.FatalLevel, msg, err)
|
||||
}
|
||||
}
|
||||
|
||||
func Fatalf(ctx context.Context, err error, format string, v ...any) {
|
||||
func Fatalf(ctx context.Context, err error, format string, v ...any) {
|
||||
msg := fmt.Sprintf(format, v...)
|
||||
logEvent(ctx, zerolog.FatalLevel, msg, err)
|
||||
}
|
||||
}
|
||||
|
||||
func Panic(ctx context.Context, err error, msg string) {
|
||||
func Panic(ctx context.Context, err error, msg string) {
|
||||
logEvent(ctx, zerolog.PanicLevel, msg, err)
|
||||
}
|
||||
}
|
||||
|
||||
func Panicf(ctx context.Context, err error, format string, v ...any) {
|
||||
func Panicf(ctx context.Context, err error, format string, v ...any) {
|
||||
msg := fmt.Sprintf(format, v...)
|
||||
logEvent(ctx, zerolog.PanicLevel, msg, err)
|
||||
}
|
||||
}
|
||||
|
||||
func logEvent(ctx context.Context, level zerolog.Level, msg string, err error) {
|
||||
func logEvent(ctx context.Context, level zerolog.Level, msg string, err error) {
|
||||
event := logger.WithLevel(level)
|
||||
|
||||
if err != nil {
|
||||
@@ -223,8 +223,8 @@
|
||||
}
|
||||
addCtx(ctx, event)
|
||||
event.Msg(msg)
|
||||
}
|
||||
func Request(ctx context.Context, r *http.Request, body []byte) {
|
||||
}
|
||||
func Request(ctx context.Context, r *http.Request, body []byte) {
|
||||
event := logger.Info()
|
||||
addCtx(ctx, event)
|
||||
event.Str("method", r.Method).
|
||||
@@ -232,9 +232,9 @@
|
||||
Str("body", string(body)).
|
||||
Str("remoteAddr", r.RemoteAddr).
|
||||
Msg("HTTP Request")
|
||||
}
|
||||
}
|
||||
|
||||
func addCtx(ctx context.Context, event *zerolog.Event) {
|
||||
func addCtx(ctx context.Context, event *zerolog.Event) {
|
||||
for _, key := range cfg.contextKeys {
|
||||
val, ok := ctx.Value(key).(string)
|
||||
if !ok {
|
||||
@@ -243,9 +243,9 @@
|
||||
keyStr := key.(string)
|
||||
event.Str(keyStr, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Response(ctx context.Context, r *http.Request, statusCode int, responseTime time.Duration) {
|
||||
func Response(ctx context.Context, r *http.Request, statusCode int, responseTime time.Duration) {
|
||||
event := logger.Info()
|
||||
addCtx(ctx, event)
|
||||
event.Str("method", r.Method).
|
||||
@@ -253,4 +253,4 @@
|
||||
Int("statusCode", statusCode).
|
||||
Dur("responseTime", responseTime).
|
||||
Msg("HTTP Response")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user