diff --git a/core/engine.go b/core/engine.go index f997b47..de7948d 100644 --- a/core/engine.go +++ b/core/engine.go @@ -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() diff --git a/core/job_manager.go b/core/job_manager.go index 8676246..da50bdd 100644 --- a/core/job_manager.go +++ b/core/job_manager.go @@ -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) diff --git a/core/logger/writer_adapter.go b/core/logger/writer_adapter.go new file mode 100644 index 0000000..890fe80 --- /dev/null +++ b/core/logger/writer_adapter.go @@ -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 +}