refact: use the new klog

master v0.0.1
Evan Chen 2021-11-05 23:31:55 +08:00
parent 196c73edfb
commit 52501e0d01
4 changed files with 10 additions and 17 deletions

2
go.mod
View File

@ -2,7 +2,7 @@ module kumoly.io/lib/ksrv
go 1.17 go 1.17
require kumoly.io/lib/klog v0.0.3-0.20211105083111-32b758725e46 require kumoly.io/lib/klog v0.0.4
require ( require (
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect

4
go.sum
View File

@ -8,3 +8,7 @@ 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.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 h1:00CMUgz64o+YVJqGbAp+ZP+m1oLuvC0nTMNO0jiaKes=
kumoly.io/lib/klog v0.0.3-0.20211105083111-32b758725e46/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M= kumoly.io/lib/klog v0.0.3-0.20211105083111-32b758725e46/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M=
kumoly.io/lib/klog v0.0.3 h1:DpJ6i9Y5I4DCEBb7j5oaAPc9tUTB9rDHE2J4bqmsYJY=
kumoly.io/lib/klog v0.0.3/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M=
kumoly.io/lib/klog v0.0.4 h1:Ev9G/zvLt/C8Q1yWfYoUcXVJWgPMjpqHPat2WKyOPIM=
kumoly.io/lib/klog v0.0.4/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M=

View File

@ -6,13 +6,6 @@ import (
"net/http" "net/http"
"kumoly.io/lib/klog" "kumoly.io/lib/klog"
"kumoly.io/lib/klog/color"
)
const (
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}}`
) )
func (k *kserver) SetLogger(l *klog.Logger) { func (k *kserver) SetLogger(l *klog.Logger) {
@ -27,18 +20,14 @@ func (k *kserver) GetLogger() *klog.Logger {
func New() *kserver { func New() *kserver {
l := klog.Sub("KSRV") l := klog.Sub("KSRV")
l.SetPrinter(func(w io.Writer, d *klog.Ldata, l2 *klog.Logger) { 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", fmt.Fprintf(w, "[%s] %s |%s| %15s %s %s %s\n%s",
l.M("KSRV ", color.FgHiCyan), l.M("KSRV ", klog.FgHiCyan),
d.Time.Format("2006/01/02 15:04:05"), d.Time.Format("2006/01/02 15:04:05"),
StatusColor(d.Fields["Status"].(int), l), StatusColor(d.Fields["Status"].(int), l),
d.Fields["IP"], d.Fields["IP"],
MethodColor(d.Fields["Method"], l), MethodColor(d.Fields["Method"], l),
d.Fields["URL"], msg, d.Fields["URL"], l.M(d.Message, klog.FgRed),
l.M(d.Stack, color.FgRed), l.M(d.Stack, klog.FgRed),
) )
}) })

View File

@ -19,8 +19,8 @@ func main() {
klog.Info("start") klog.Info("start")
err := ksrv.New().SetNoLogCondition(func(r *http.Request) bool { err := ksrv.New().SetNoLogCondition(func(r *http.Request) bool {
return true return r.URL.String() == "/"
}).Handle(mux).Listen("127.0.0.1:8080").Serve() }).Handle(mux).Listen("0.0.0.0:8080").Serve()
if err != nil { if err != nil {
panic(err) panic(err)
} }