63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package chi
|
|
|
|
import (
|
|
"context"
|
|
"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,
|
|
host: r.Host,
|
|
path: r.URL.Path,
|
|
ctx: r.Context(),
|
|
}
|
|
}
|
|
|
|
func NewLogFormatter() *LogFormatter {
|
|
return &LogFormatter{}
|
|
}
|
|
|
|
var _ middleware.LogFormatter = &LogFormatter{}
|
|
|
|
type LogEntry struct {
|
|
method string
|
|
host string
|
|
path string
|
|
ctx context.Context
|
|
}
|
|
|
|
// Panic implements middleware.LogEntry
|
|
func (e *LogEntry) Panic(v interface{}, stack []byte) {
|
|
logger.Error(
|
|
e.ctx, "http panic",
|
|
logger.F("stack", string(stack)),
|
|
logger.F("host", e.host),
|
|
logger.F("method", e.method),
|
|
logger.F("path", e.path),
|
|
)
|
|
}
|
|
|
|
// Write implements middleware.LogEntry
|
|
func (e *LogEntry) Write(status int, bytes int, header http.Header, elapsed time.Duration, extra interface{}) {
|
|
logger.Info(
|
|
e.ctx, "http request",
|
|
logger.F("host", e.host),
|
|
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{}
|