partial GIN logging replacement

This commit is contained in:
Pavel 2023-10-18 17:56:21 +03:00
parent e9e9c4c442
commit 8fd4bc26bf
3 changed files with 38 additions and 0 deletions

View File

@ -179,6 +179,22 @@ func (e *Engine) UseZabbix(collectors []metrics.Collector) *Engine {
return e return e
} }
func (e *Engine) HijackGinLogs() *Engine {
if e.Logger() == nil {
return e
}
gin.DefaultWriter = logger.WriterAdapter(e.Logger(), slog.LevelDebug)
gin.DefaultErrorWriter = logger.WriterAdapter(e.Logger(), slog.LevelError)
gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
e.Logger().Debug("route",
slog.String(logger.HTTPMethodAttr, httpMethod),
slog.String("path", absolutePath),
slog.String(logger.HandlerAttr, handlerName),
slog.Int("handlerCount", nuHandlers))
}
return e
}
// TemplateFuncMap combines func map for templates. // TemplateFuncMap combines func map for templates.
func (e *Engine) TemplateFuncMap(functions template.FuncMap) template.FuncMap { func (e *Engine) TemplateFuncMap(functions template.FuncMap) template.FuncMap {
funcMap := e.LocalizationFuncMap() funcMap := e.LocalizationFuncMap()

View File

@ -74,6 +74,7 @@ func (j *Job) getWrappedFunc(name string, log logger.Logger) func(callback JobAf
} }
}() }()
log = log.With(logger.HandlerAttr, name)
err := j.Command(log) err := j.Command(log)
if err != nil && j.ErrorHandler != nil { if err != nil && j.ErrorHandler != nil {
j.ErrorHandler(name, err, log) j.ErrorHandler(name, err, log)

View File

@ -0,0 +1,21 @@
package logger
import (
"context"
"io"
"log/slog"
)
type writerAdapter struct {
log Logger
level slog.Level
}
func WriterAdapter(log Logger, level slog.Level) io.Writer {
return &writerAdapter{log: log, level: level}
}
func (w *writerAdapter) Write(p []byte) (n int, err error) {
w.log.Log(context.Background(), w.level, string(p))
return len(p), nil
}