master
Evan Chen 2021-11-05 21:19:45 +08:00
parent 77be1d6ae3
commit 196c73edfb
4 changed files with 68 additions and 54 deletions

4
go.mod
View File

@ -2,9 +2,9 @@ module kumoly.io/lib/ksrv
go 1.17
require kumoly.io/lib/klog v0.1.10
require kumoly.io/lib/klog v0.0.3-0.20211105083111-32b758725e46
require (
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect
)

12
go.sum
View File

@ -2,9 +2,9 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
kumoly.io/lib/klog v0.1.8 h1:CguIVyo2T/E2cdrc30mFh/KPhIiM/K2hNV8aR8KuSdw=
kumoly.io/lib/klog v0.1.8/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M=
kumoly.io/lib/klog v0.1.9 h1:rS9PPqfyBIIfeQlPSuMv+7StGPiFVuAdp04HDwwDY3E=
kumoly.io/lib/klog v0.1.9/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M=
kumoly.io/lib/klog v0.1.10 h1:GJxwcsUct8nF3oHtsJPTIlPKTUeB/+7jsbgh3bMvKMc=
kumoly.io/lib/klog v0.1.10/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b h1:1VkfZQv42XQlA/jchYumAnv1UPo6RgF9rJFkTgZIxO4=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
kumoly.io/lib/klog v0.0.2 h1:6U0k5efspuu0eomvKZ+78WyFqak2WXohjDrg/6R3C3Y=
kumoly.io/lib/klog v0.0.2/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M=
kumoly.io/lib/klog v0.0.3-0.20211105083111-32b758725e46 h1:00CMUgz64o+YVJqGbAp+ZP+m1oLuvC0nTMNO0jiaKes=
kumoly.io/lib/klog v0.0.3-0.20211105083111-32b758725e46/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M=

View File

@ -1,14 +1,15 @@
package ksrv
import (
"fmt"
"io"
"net/http"
"kumoly.io/lib/klog"
"kumoly.io/lib/klog/color"
)
const (
DEFAULT_INFO = `[{{"KSRV"|cyan}}] {{Time}} {{with .Fields}}|{{printf " %3d " .Status|statcol .Status}}| ` +
`{{printf "%15s" .IP}} {{printf " %-7s " .Method|methcol .Method}} {{.URL}}{{"\n"}}{{end}}`
DEFAULT_ERR = `[{{"KSRV"|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}}`
@ -25,48 +26,22 @@ func (k *kserver) GetLogger() *klog.Logger {
// New returns a extended http.Server
func New() *kserver {
l := klog.Sub("KSRV")
tmpl := klog.NewLogFormater()
tmpl.InfoTmplStr = DEFAULT_INFO
tmpl.ErrTmplStr = DEFAULT_ERR
l.SetPrinter(func(w io.Writer, d *klog.Ldata, l2 *klog.Logger) {
msg := ""
if d.Fields["Message"] != nil {
msg = l.M(d.Fields["Message"], color.FgHiRed)
}
fmt.Fprintf(w, "[%s] %s |%s| %15s %s %s %s\n%s",
l.M("KSRV ", color.FgHiCyan),
d.Time.Format("2006/01/02 15:04:05"),
StatusColor(d.Fields["Status"].(int), l),
d.Fields["IP"],
MethodColor(d.Fields["Method"], l),
d.Fields["URL"], msg,
l.M(d.Stack, color.FgRed),
)
})
fMap := klog.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)
}
k := &kserver{
l: l,
nolog: func(r *http.Request) bool { return false },
@ -84,8 +59,9 @@ func (k *kserver) Middleware(next http.Handler) http.Handler {
)
}
func (k *kserver) SetNoLogCondition(nolog func(r *http.Request) bool) {
func (k *kserver) SetNoLogCondition(nolog func(r *http.Request) bool) *kserver {
k.nolog = nolog
return k
}
func (k *kserver) catch(rw *responseWriter, r *http.Request) {
@ -99,3 +75,39 @@ func (k *kserver) catch(rw *responseWriter, r *http.Request) {
k.l.InfoF(klog.H{"Status": rw.StatueCode, "IP": GetIP(r), "Method": r.Method, "URL": r.URL.Path, "Agent": r.Header.Get("User-Agent")})
}
}
func MethodColor(method interface{}, l *klog.Logger) string {
show := fmt.Sprintf(" %-7s", method)
switch method {
case http.MethodGet:
return l.M(show, 97, 44)
case http.MethodPost:
return l.M(show, 97, 46)
case http.MethodPut:
return l.M(show, 90, 43)
case http.MethodDelete:
return l.M(show, 97, 41)
case http.MethodPatch:
return l.M(show, 97, 42)
case http.MethodHead:
return l.M(show, 97, 45)
case http.MethodOptions:
return l.M(show, 90, 47)
default:
return l.M(show, 0)
}
}
func StatusColor(code int, l *klog.Logger) string {
s := fmt.Sprintf(" %d ", code)
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)
}
}

View File

@ -12,13 +12,15 @@ import (
func main() {
klog.PROD = false
mux := http.NewServeMux()
mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) { rw.Write([]byte("ok")) })
mux.HandleFunc("/ok", func(rw http.ResponseWriter, r *http.Request) { rw.Write([]byte("ok")) })
mux.HandleFunc("/err", func(rw http.ResponseWriter, r *http.Request) { ksrv.Abort(rw, errors.New("small err")) })
mux.HandleFunc("/panic", func(rw http.ResponseWriter, r *http.Request) { panic(500) })
mux.HandleFunc("/out", func(rw http.ResponseWriter, r *http.Request) { arr := []int{0, 1}; fmt.Print(arr[9]) })
klog.Info("start")
err := ksrv.New().Handle(mux).Listen("0.0.0.0:8081").Serve()
err := ksrv.New().SetNoLogCondition(func(r *http.Request) bool {
return true
}).Handle(mux).Listen("127.0.0.1:8080").Serve()
if err != nil {
panic(err)
}