2021-11-03 17:17:58 +00:00
|
|
|
package ksrv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"kumoly.io/core/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DEFAULT_INFO = `[{{"KMUX"|cyan}}] {{Time}} {{with .Fields}}|{{printf " %3d " .Status|statcol .Status}}| ` +
|
|
|
|
`{{printf "%15s" .IP}} {{printf " %-7s " .Method|methcol .Method}} {{.URL}}{{"\n"}}{{end}}`
|
|
|
|
DEFAULT_ERR = `[{{"KMUX"|cyan}}] {{Time}} {{with .Fields}}|{{printf " %3d " .Status|statcol .Status}}| ` +
|
|
|
|
`{{printf "%15s" .IP}} {{printf " %-7s " .Method|methcol .Method}} {{.URL}}{{end}} {{.Message|red}}{{"\n"}}` +
|
|
|
|
`{{if .Stack}}{{.Stack|redl}}{{end}}`
|
|
|
|
)
|
|
|
|
|
|
|
|
func (k *kserver) SetLogger(l *log.Logger) {
|
|
|
|
k.l = l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kserver) GetLogger() *log.Logger {
|
|
|
|
return k.l
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a extended http.Server
|
|
|
|
func New() *kserver {
|
|
|
|
l := log.Sub("KSRV")
|
|
|
|
tmpl := log.NewLogFormater()
|
|
|
|
tmpl.InfoTmplStr = DEFAULT_INFO
|
|
|
|
tmpl.ErrTmplStr = DEFAULT_ERR
|
|
|
|
|
|
|
|
fMap := log.DefaultFuncMap()
|
|
|
|
fMap["statcol"] = func(code int, s string) string {
|
|
|
|
switch {
|
|
|
|
case code >= http.StatusOK && code < http.StatusMultipleChoices:
|
|
|
|
return l.M(s, 97, 42)
|
|
|
|
case code >= http.StatusMultipleChoices && code < http.StatusBadRequest:
|
|
|
|
return l.M(s, 90, 47)
|
|
|
|
case code >= http.StatusBadRequest && code < http.StatusInternalServerError:
|
|
|
|
return l.M(s, 90, 43)
|
|
|
|
default:
|
|
|
|
return l.M(s, 97, 41)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fMap["methcol"] = func(method string, s string) string {
|
|
|
|
switch method {
|
|
|
|
case http.MethodGet:
|
|
|
|
return l.M(s, 97, 44)
|
|
|
|
case http.MethodPost:
|
|
|
|
return l.M(s, 97, 46)
|
|
|
|
case http.MethodPut:
|
|
|
|
return l.M(s, 90, 43)
|
|
|
|
case http.MethodDelete:
|
|
|
|
return l.M(s, 97, 41)
|
|
|
|
case http.MethodPatch:
|
|
|
|
return l.M(s, 97, 42)
|
|
|
|
case http.MethodHead:
|
|
|
|
return l.M(s, 97, 45)
|
|
|
|
case http.MethodOptions:
|
|
|
|
return l.M(s, 90, 47)
|
|
|
|
default:
|
|
|
|
return l.M(s, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l.SetTmpl(tmpl, fMap)
|
|
|
|
err := l.Reload()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-11-03 18:09:00 +00:00
|
|
|
k := &kserver{
|
|
|
|
l: l,
|
2021-11-03 18:15:45 +00:00
|
|
|
nolog: func(r *http.Request) bool { return false },
|
2021-11-03 18:09:00 +00:00
|
|
|
}
|
2021-11-03 17:17:58 +00:00
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kserver) Middleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
rw := &responseWriter{w, 0, ""}
|
|
|
|
defer k.catch(rw, r)
|
|
|
|
next.ServeHTTP(rw, r)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:09:00 +00:00
|
|
|
func (k *kserver) SetNoLogCondition(nolog func(r *http.Request) bool) {
|
|
|
|
k.nolog = nolog
|
|
|
|
}
|
|
|
|
|
2021-11-03 17:17:58 +00:00
|
|
|
func (k *kserver) catch(rw *responseWriter, r *http.Request) {
|
|
|
|
ex := recover()
|
|
|
|
if ex != nil {
|
|
|
|
Abort(rw, ex)
|
2021-11-03 18:09:00 +00:00
|
|
|
k.l.ErrorF(log.H{"Status": rw.StatueCode, "IP": GetIP(r), "Method": r.Method, "URL": r.URL.Path, "Agent": r.Header.Get("User-Agent")}, ex)
|
2021-11-03 17:17:58 +00:00
|
|
|
} else if rw.StatueCode >= 500 {
|
2021-11-03 18:09:00 +00:00
|
|
|
k.l.ErrorF(log.H{"Status": rw.StatueCode, "IP": GetIP(r), "Method": r.Method, "URL": r.URL.Path, "Agent": r.Header.Get("User-Agent")}, rw.err)
|
2021-11-03 18:15:45 +00:00
|
|
|
} else if k.nolog == nil || !k.nolog(r) {
|
2021-11-03 18:09:00 +00:00
|
|
|
k.l.InfoF(log.H{"Status": rw.StatueCode, "IP": GetIP(r), "Method": r.Method, "URL": r.URL.Path, "Agent": r.Header.Get("User-Agent")})
|
2021-11-03 17:17:58 +00:00
|
|
|
}
|
|
|
|
}
|