diff --git a/.github/workflows/nfctl.yml b/.github/workflows/nfctl.yml index bb9e367..c9370d5 100644 --- a/.github/workflows/nfctl.yml +++ b/.github/workflows/nfctl.yml @@ -2,7 +2,7 @@ name: Auto Build on: push: branches: - - 'master' + - 'release/nfctl/*' env: RELEASE_VERSION: v24.07.14-r3 diff --git a/ctx.go b/ctx.go index 1645310..097a5a2 100644 --- a/ctx.go +++ b/ctx.go @@ -51,11 +51,10 @@ func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ct } c := context.WithValue(request.Context(), TraceKey, traceId) - request.WithContext(c) ctx := &Ctx{ lock: sync.Mutex{}, - Request: request, + Request: request.WithContext(c), path: request.URL.Path, method: request.Method, StatusCode: 200, @@ -75,6 +74,7 @@ func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ct } ctx.Writer = &ctx.writermem + ctx.writermem.Header().Set(TraceKey, traceId) return ctx } diff --git a/middleware.go b/middleware.go index e50149a..181e951 100644 --- a/middleware.go +++ b/middleware.go @@ -2,11 +2,9 @@ package nf import ( "fmt" - "github.com/google/uuid" "github.com/loveuer/nf/nft/log" "os" "runtime/debug" - "strings" "time" ) @@ -29,33 +27,19 @@ func NewRecover(enableStackTrace bool) HandlerFunc { } } -func NewLogger(traceHeader ...string) HandlerFunc { - Header := "X-Trace-ID" - if len(traceHeader) > 0 && traceHeader[0] != "" { - Header = traceHeader[0] - } +func NewLogger() HandlerFunc { return func(c *Ctx) error { var ( now = time.Now() - trace = c.Get(Header) logFn func(msg string, data ...any) ip = c.IP() ) - if trace == "" { - trace = uuid.Must(uuid.NewV7()).String() - } - - c.SetHeader(Header, trace) - - traces := strings.Split(trace, "-") - shortTrace := traces[len(traces)-1] - err := c.Next() duration := time.Since(now) - msg := fmt.Sprintf("NF | %s | %15s | %3d | %s | %6s | %s", shortTrace, ip, c.StatusCode, HumanDuration(duration.Nanoseconds()), c.Method(), c.Path()) + msg := fmt.Sprintf("NF | %v | %15s | %3d | %s | %6s | %s", c.Context().Value(TraceKey), ip, c.StatusCode, HumanDuration(duration.Nanoseconds()), c.Method(), c.Path()) switch { case c.StatusCode >= 500: