github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/examples/context_logging/main.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/v2pro/plz/countlog"
     5  	"github.com/v2pro/plz/concurrent"
     6  	"time"
     7  	"errors"
     8  )
     9  
    10  func main() {
    11  	concurrent.GlobalUnboundedExecutor.Go(func(ctx *countlog.Context) {
    12  		ctx.Add("traceId", "axkenfppkl")
    13  		timer := countlog.InfoTimer()
    14  		ctx.SuppressLevelsBelow(countlog.LevelInfo)
    15  		req := "request 111"
    16  		err := processRequest(ctx, req)
    17  		ctx.LogAccess("process request", err,
    18  			"request", req,
    19  			"timer", timer)
    20  	})
    21  	time.Sleep(time.Second)
    22  }
    23  
    24  func processRequest(ctx *countlog.Context, request string) error {
    25  	ctx.Trace("something minor")
    26  	ctx.Add("userId", "111")
    27  	ctx.Info("calculated game scores", "score", []int{1, 2, 3})
    28  	return errors.New("failed")
    29  }