Skip to content

Commit

Permalink
atomiclevel log with traceid
Browse files Browse the repository at this point in the history
  • Loading branch information
axiaoxin committed Sep 9, 2020
1 parent 3c92334 commit 5f3338e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 3 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func NewCtxLogger(c context.Context, logger *zap.Logger, traceID string) (contex
if c == nil {
c = context.Background()
}
if traceID == "" {
traceID = CtxTraceID(c)
}
ctxLogger := logger.With(zap.String(string(TraceIDKeyname), traceID))
if gc, ok := c.(*gin.Context); ok {
// set ctxlogger in gin.Context
Expand Down
5 changes: 3 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,14 @@ func runAtomicLevelServer(atomicLevel zap.AtomicLevel, options AtomicLevelServer
levelServer := http.NewServeMux()
levelServer.HandleFunc(urlPath, func(w http.ResponseWriter, r *http.Request) {
msg := fmt.Sprintf("%s %s the logger atomic level", r.RemoteAddr, r.Method)
_, logger := NewCtxLogger(r.Context(), CloneLogger("atomiclevel"), r.Header.Get(string(TraceIDKeyname)))
if r.Method == http.MethodPut {
b, _ := ioutil.ReadAll(r.Body)
msg += " to " + string(b)
r.Body = ioutil.NopCloser(bytes.NewBuffer(b))
CtxLogger(r.Context()).Warn(msg)
logger.Warn(msg)
} else {
CtxLogger(r.Context()).Info(msg)
logger.Info(msg)
}
if options.Username != "" && options.Password != "" {
if _, _, ok := r.BasicAuth(); !ok {
Expand Down

0 comments on commit 5f3338e

Please sign in to comment.