2023-04-24 20:52:12 +02:00
|
|
|
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,
|
2024-03-28 19:47:32 +01:00
|
|
|
host: r.Host,
|
2023-04-24 20:52:12 +02:00
|
|
|
path: r.URL.Path,
|
|
|
|
ctx: r.Context(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLogFormatter() *LogFormatter {
|
|
|
|
return &LogFormatter{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ middleware.LogFormatter = &LogFormatter{}
|
|
|
|
|
|
|
|
type LogEntry struct {
|
|
|
|
method string
|
2024-03-28 19:47:32 +01:00
|
|
|
host string
|
2023-04-24 20:52:12 +02:00
|
|
|
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),
|
2024-03-28 19:47:32 +01:00
|
|
|
logger.F("host", e.host),
|
2023-04-24 20:52:12 +02:00
|
|
|
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{}
|