Skip to content

Commit

Permalink
😊 colorful for http trace
Browse files Browse the repository at this point in the history
  • Loading branch information
fengyuan-liang committed Dec 18, 2023
1 parent c1d2fac commit 0d690e4
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,10 @@ $ curl http://localhost/v1/usage/1/week

### 4. 依赖注入支持

### 5.其他更新

**2023/12/18**

请求计时

![image-20231218173140763](https://cdn.fengxianhub.top/resources-master/image-20231218173140763.png)
5 changes: 3 additions & 2 deletions core/handler/handler_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/fengyuan-liang/jet-web-fasthttp/pkg/xlog"
"github.com/valyala/fasthttp"
"reflect"
"time"
)

type handler struct {
Expand All @@ -28,7 +27,7 @@ type handler struct {
var handlerLog = xlog.NewWith("handler_log")

func (h handler) ServeHTTP(ctx *fasthttp.RequestCtx, args []string) {
defer utils.TraceElapsed(time.Now())
defer traceHttpReq(ctx)
switch string(ctx.Method()) {
case constant.MethodGet, constant.MethodPost, constant.MethodPut, constant.MethodDelete:
h.handleRequest(ctx, args)
Expand Down Expand Up @@ -106,6 +105,8 @@ func (h handler) handleRequest(ctx *fasthttp.RequestCtx, args []string) {
return
}
methodArgs = append(methodArgs, param, jetCtx)
default:
panic("illegal method signature")
}

callValues := h.method.Func.Call(methodArgs)
Expand Down
95 changes: 95 additions & 0 deletions core/handler/handler_util_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
package handler

import (
"fmt"
"github.com/fengyuan-liang/jet-web-fasthttp/pkg/constant"
"github.com/fengyuan-liang/jet-web-fasthttp/pkg/utils"
"github.com/fengyuan-liang/jet-web-fasthttp/pkg/xlog"
"github.com/valyala/fasthttp"
"os"
"time"
)

const (
Expand Down Expand Up @@ -38,3 +42,94 @@ func FailHandler(ctx *fasthttp.RequestCtx, data string) {
ctx.Response.Header.SetServer("JetServer")
ctx.SetBodyString(data)
}

// --------------------------------------------------------------------

var httpTraceLog = func() *xlog.Logger {
logger := xlog.NewWith("jet")
logger.SetCalldPath(3)
logger.SetFlags(xlog.LstdFlags | xlog.Llevel)
return logger
}()

func httpTrace(start time.Time, traceName string) {
httpTraceLog.Infof(" %v | elapsed [%v]", traceName, time.Since(start))
}

var (
green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109})
red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
reset = string([]byte{27, 91, 48, 109})
)

func colorForStatus(code int) string {
switch {
case code >= 200 && code < 300:
return green
case code >= 300 && code < 400:
return white
case code >= 400 && code < 500:
return yellow
default:
return red
}
}

func colorForMethod(method string) string {
switch method {
case "GET":
return blue
case "POST":
return cyan
case "PUT":
return yellow
case "DELETE":
return red
case "PATCH":
return green
case "HEAD":
return magenta
case "OPTIONS":
return white
default:
return reset
}
}

// isTerminal Check if the output destination is a terminal
var isTerminal = func() bool {
fileInfo, _ := os.Stdout.Stat()
return (fileInfo.Mode() & os.ModeCharDevice) != 0
}()

func traceHttpReq(ctx *fasthttp.RequestCtx) {
var (
statusCode = ctx.Response.StatusCode()
method = string(ctx.Method())
path = string(ctx.Path())
)
if isTerminal {
// status method path
httpTrace(time.Now(),
fmt.Sprintf("|%s %3d %s| |%s %s %s| %v",
colorForStatus(statusCode), statusCode, reset,
colorForMethod(method), method, reset,
path,
),
)
} else {
// status method path
httpTrace(time.Now(),
fmt.Sprintf("%v %v %v",
statusCode,
method,
path,
),
)
}
}
3 changes: 2 additions & 1 deletion pkg/utils/util_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
var traceElapsed = func() *xlog.Logger {
logger := xlog.NewWith("traceElapsed")
logger.SetCalldPath(3)
logger.SetFlags(xlog.LstdFlags | xlog.Llevel)
return logger
}()

Expand All @@ -20,5 +21,5 @@ func TraceElapsed(start time.Time) {
}

func TraceElapsedByName(start time.Time, traceName string) {
traceElapsed.Infof("trace [%v] elapsed [%v]", traceName, time.Since(start))
traceElapsed.Infof(" trace[%v] elapsed[%v]", traceName, time.Since(start))
}

0 comments on commit 0d690e4

Please sign in to comment.