package chi import ( "context" "fmt" "net/http" "time" "github.com/go-chi/chi/v5/middleware" "gitlab.com/wpetit/goweb/logger" ) type LogFormatter struct{} // NewLogEntry implements middleware.LogFormatter func (*LogFormatter) NewLogEntry(r *http.Request) middleware.LogEntry { return &LogEntry{ method: r.Method, path: r.URL.Path, ctx: r.Context(), } } func NewLogFormatter() *LogFormatter { return &LogFormatter{} } var _ middleware.LogFormatter = &LogFormatter{} type LogEntry struct { method string path string ctx context.Context } // Panic implements middleware.LogEntry func (e *LogEntry) Panic(v interface{}, stack []byte) { logger.Error(e.ctx, fmt.Sprintf("%s %s", e.method, e.path), logger.F("stack", string(stack))) } // Write implements middleware.LogEntry func (e *LogEntry) Write(status int, bytes int, header http.Header, elapsed time.Duration, extra interface{}) { logger.Info(e.ctx, fmt.Sprintf("%s %s - %d", e.method, e.path, status), logger.F("status", status), logger.F("bytes", bytes), logger.F("elapsed", elapsed), logger.F("method", e.method), logger.F("path", e.path), logger.F("extra", extra), ) } var _ middleware.LogEntry = &LogEntry{}