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