build: use klog instead

master
Evan Chen 2021-11-04 03:08:05 +08:00
parent 337d64b13b
commit 1668a7940c
5 changed files with 20 additions and 18 deletions

2
go.mod
View File

@ -2,7 +2,7 @@ module kumoly.io/lib/ksrv
go 1.17 go 1.17
require kumoly.io/core/log v0.1.7 require kumoly.io/lib/klog v0.1.9
require ( require (
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect

6
go.sum
View File

@ -2,5 +2,7 @@ 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= 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 h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
kumoly.io/core/log v0.1.7 h1:eAOitZUYyeivFlqcdNsITZT79vVXCzRckuZozqNwx44= kumoly.io/lib/klog v0.1.8 h1:CguIVyo2T/E2cdrc30mFh/KPhIiM/K2hNV8aR8KuSdw=
kumoly.io/core/log v0.1.7/go.mod h1:DrKjL3xGEEJ7OC+NHuuv0JSMV7ZqDSw2GiSFFWxilT4= 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=

View File

@ -3,7 +3,7 @@ package ksrv
import ( import (
"net/http" "net/http"
"kumoly.io/core/log" "kumoly.io/lib/klog"
) )
const ( const (
@ -14,22 +14,22 @@ const (
`{{if .Stack}}{{.Stack|redl}}{{end}}` `{{if .Stack}}{{.Stack|redl}}{{end}}`
) )
func (k *kserver) SetLogger(l *log.Logger) { func (k *kserver) SetLogger(l *klog.Logger) {
k.l = l k.l = l
} }
func (k *kserver) GetLogger() *log.Logger { func (k *kserver) GetLogger() *klog.Logger {
return k.l return k.l
} }
// New returns a extended http.Server // New returns a extended http.Server
func New() *kserver { func New() *kserver {
l := log.Sub("KSRV") l := klog.Sub("KSRV")
tmpl := log.NewLogFormater() tmpl := klog.NewLogFormater()
tmpl.InfoTmplStr = DEFAULT_INFO tmpl.InfoTmplStr = DEFAULT_INFO
tmpl.ErrTmplStr = DEFAULT_ERR tmpl.ErrTmplStr = DEFAULT_ERR
fMap := log.DefaultFuncMap() fMap := klog.DefaultFuncMap()
fMap["statcol"] = func(code int, s string) string { fMap["statcol"] = func(code int, s string) string {
switch { switch {
case code >= http.StatusOK && code < http.StatusMultipleChoices: case code >= http.StatusOK && code < http.StatusMultipleChoices:
@ -92,10 +92,10 @@ func (k *kserver) catch(rw *responseWriter, r *http.Request) {
ex := recover() ex := recover()
if ex != nil { if ex != nil {
Abort(rw, ex) Abort(rw, ex)
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) k.l.ErrorF(klog.H{"Status": rw.StatueCode, "IP": GetIP(r), "Method": r.Method, "URL": r.URL.Path, "Agent": r.Header.Get("User-Agent")}, ex)
} else if rw.StatueCode >= 500 { } else if rw.StatueCode >= 500 {
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) k.l.ErrorF(klog.H{"Status": rw.StatueCode, "IP": GetIP(r), "Method": r.Method, "URL": r.URL.Path, "Agent": r.Header.Get("User-Agent")}, rw.err)
} else if k.nolog == nil || !k.nolog(r) { } else if k.nolog == nil || !k.nolog(r) {
k.l.InfoF(log.H{"Status": rw.StatueCode, "IP": GetIP(r), "Method": r.Method, "URL": r.URL.Path, "Agent": r.Header.Get("User-Agent")}) k.l.InfoF(klog.H{"Status": rw.StatueCode, "IP": GetIP(r), "Method": r.Method, "URL": r.URL.Path, "Agent": r.Header.Get("User-Agent")})
} }
} }

View File

@ -3,12 +3,12 @@ package ksrv
import ( import (
"net/http" "net/http"
"kumoly.io/core/log" "kumoly.io/lib/klog"
) )
type kserver struct { type kserver struct {
http.Server http.Server
l *log.Logger l *klog.Logger
nolog func(r *http.Request) bool nolog func(r *http.Request) bool
} }

View File

@ -5,20 +5,20 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"kumoly.io/core/log" "kumoly.io/lib/klog"
"kumoly.io/lib/ksrv" "kumoly.io/lib/ksrv"
) )
func main() { func main() {
log.PROD = false klog.PROD = false
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) { rw.Write([]byte("ok")) }) mux.HandleFunc("/", 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("/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("/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]) }) mux.HandleFunc("/out", func(rw http.ResponseWriter, r *http.Request) { arr := []int{0, 1}; fmt.Print(arr[9]) })
log.Info("start") klog.Info("start")
err := ksrv.New().Handle(mux).Listen("0.0.0.0:8080").Serve() err := ksrv.New().Handle(mux).Listen("0.0.0.0:8081").Serve()
if err != nil { if err != nil {
panic(err) panic(err)
} }