mirror of
https://github.com/retailcrm/mg-transport-core.git
synced 2024-11-24 06:06:03 +03:00
partial GIN logging replacement
This commit is contained in:
parent
e9e9c4c442
commit
8fd4bc26bf
@ -179,6 +179,22 @@ func (e *Engine) UseZabbix(collectors []metrics.Collector) *Engine {
|
||||
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.
|
||||
func (e *Engine) TemplateFuncMap(functions template.FuncMap) template.FuncMap {
|
||||
funcMap := e.LocalizationFuncMap()
|
||||
|
@ -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)
|
||||
if err != nil && j.ErrorHandler != nil {
|
||||
j.ErrorHandler(name, err, log)
|
||||
|
21
core/logger/writer_adapter.go
Normal file
21
core/logger/writer_adapter.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user