add example for ctx keys feature of package glog (#3273)

This commit is contained in:
John Guo 2024-01-22 21:07:40 +08:00 committed by GitHub
parent f919f90bf5
commit 383937fe69
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 0 deletions

View File

@ -0,0 +1,4 @@
logger:
Level: "all"
Stdout: true
CtxKeys: ["RequestId", "UserId"]

View File

@ -0,0 +1,20 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package main
import (
"context"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var ctx = context.Background()
ctx = context.WithValue(ctx, "RequestId", "123456789")
ctx = context.WithValue(ctx, "UserId", "10000")
g.Log().Error(ctx, "runtime error")
}

View File

@ -193,6 +193,7 @@ func (v *Validator) doCheckValue(ctx context.Context, in doCheckValueInput) Erro
}
// The error should have error code that is `gcode.CodeValidationFailed`.
if gerror.Code(err) == gcode.CodeNil {
// TODO it's better using interface?
if e, ok := err.(*gerror.Error); ok {
e.SetCode(gcode.CodeValidationFailed)
}