fupie/ws/logmware.go

45 lines
1.3 KiB
Go

package ws
import (
"net/http"
"sectorinf.com/emilis/fupie/logie"
"sectorinf.com/emilis/fupie/ws/route"
)
type LoggingSet struct {
log1XX logie.Log
log2XX logie.Log
log3XX logie.Log
log4XX logie.Log
log5XX logie.Log
}
func LoggingMiddleware(l logie.Log) route.Middleware {
l = l.ColorBracket(1, logie.Colors().Font(logie.FontFaint))
log := LoggingSet{
log1XX: l.ColorBracket(0, logie.Colors().Font(logie.FontFaint)),
log2XX: l.ColorBracket(0, logie.Colors().Foreground(logie.ColorWhite).Background(logie.ColorGreen)),
log3XX: l.ColorBracket(0, logie.Colors().Foreground(logie.ColorYellow).Font(logie.FontFaint)),
log4XX: l.ColorBracket(0, logie.Colors().Foreground(logie.ColorWhite).Background(logie.ColorMagenta)),
log5XX: l.ColorBracket(0, logie.Colors().Foreground(logie.ColorWhite).Background(logie.ColorRed).Font(logie.FontBold)),
}
return func(req http.Request, keep route.Keeper) {
var logger logie.Log
switch status := keep.Status; {
case status > 199 && status < 300:
logger = log.log2XX
case status > 299 && status < 400:
logger = log.log3XX
case status > 399 && status < 500:
logger = log.log4XX
case status > 499 && status < 600:
logger = log.log5XX
default:
logger = log.log1XX
}
logger.Infof("[%d]: [%s]", keep.Status, req.URL.Path)
}
}