add package gctx

This commit is contained in:
John Guo
2021-08-11 13:20:00 +08:00
parent 91dd9e2bf9
commit 3fc96f2bd0
3 changed files with 68 additions and 47 deletions

View File

@ -6,57 +6,58 @@
package g
import "github.com/gogf/gf/container/gvar"
// Var is a universal variable interface, like generics.
type Var = gvar.Var
// Frequently-used map alias.
type (
Map = map[string]interface{}
MapAnyAny = map[interface{}]interface{}
MapAnyStr = map[interface{}]string
MapAnyInt = map[interface{}]int
MapStrAny = map[string]interface{}
MapStrStr = map[string]string
MapStrInt = map[string]int
MapIntAny = map[int]interface{}
MapIntStr = map[int]string
MapIntInt = map[int]int
MapAnyBool = map[interface{}]bool
MapStrBool = map[string]bool
MapIntBool = map[int]bool
import (
"context"
"github.com/gogf/gf/container/gvar"
)
// Frequently-used slice alias.
type (
List = []Map
ListAnyAny = []MapAnyAny
ListAnyStr = []MapAnyStr
ListAnyInt = []MapAnyInt
ListStrAny = []MapStrAny
ListStrStr = []MapStrStr
ListStrInt = []MapStrInt
ListIntAny = []MapIntAny
ListIntStr = []MapIntStr
ListIntInt = []MapIntInt
ListAnyBool = []MapAnyBool
ListStrBool = []MapStrBool
ListIntBool = []MapIntBool
Var = gvar.Var // Var is a universal variable interface, like generics.
Ctx = context.Context // Ctx is alias of frequently-used context.Context.
)
// Frequently-used slice alias.
type (
Slice = []interface{}
SliceAny = []interface{}
SliceStr = []string
SliceInt = []int
Map = map[string]interface{} // Map is alias of frequently-used map type map[string]interface{}.
MapAnyAny = map[interface{}]interface{} // MapAnyAny is alias of frequently-used map type map[interface{}]interface{}.
MapAnyStr = map[interface{}]string // MapAnyStr is alias of frequently-used map type map[interface{}]string.
MapAnyInt = map[interface{}]int // MapAnyInt is alias of frequently-used map type map[interface{}]int.
MapStrAny = map[string]interface{} // MapStrAny is alias of frequently-used map type map[string]interface{}.
MapStrStr = map[string]string // MapStrStr is alias of frequently-used map type map[string]string.
MapStrInt = map[string]int // MapStrInt is alias of frequently-used map type map[string]int.
MapIntAny = map[int]interface{} // MapIntAny is alias of frequently-used map type map[int]interface{}.
MapIntStr = map[int]string // MapIntStr is alias of frequently-used map type map[int]string.
MapIntInt = map[int]int // MapIntInt is alias of frequently-used map type map[int]int.
MapAnyBool = map[interface{}]bool // MapAnyBool is alias of frequently-used map type map[interface{}]bool.
MapStrBool = map[string]bool // MapStrBool is alias of frequently-used map type map[string]bool.
MapIntBool = map[int]bool // MapIntBool is alias of frequently-used map type map[int]bool.
)
// Array is alias of Slice.
type (
Array = []interface{}
ArrayAny = []interface{}
ArrayStr = []string
ArrayInt = []int
List = []Map // List is alias of frequently-used slice type []Map.
ListAnyAny = []MapAnyAny // ListAnyAny is alias of frequently-used slice type []MapAnyAny.
ListAnyStr = []MapAnyStr // ListAnyStr is alias of frequently-used slice type []MapAnyStr.
ListAnyInt = []MapAnyInt // ListAnyInt is alias of frequently-used slice type []MapAnyInt.
ListStrAny = []MapStrAny // ListStrAny is alias of frequently-used slice type []MapStrAny.
ListStrStr = []MapStrStr // ListStrStr is alias of frequently-used slice type []MapStrStr.
ListStrInt = []MapStrInt // ListStrInt is alias of frequently-used slice type []MapStrInt.
ListIntAny = []MapIntAny // ListIntAny is alias of frequently-used slice type []MapIntAny.
ListIntStr = []MapIntStr // ListIntStr is alias of frequently-used slice type []MapIntStr.
ListIntInt = []MapIntInt // ListIntInt is alias of frequently-used slice type []MapIntInt.
ListAnyBool = []MapAnyBool // ListAnyBool is alias of frequently-used slice type []MapAnyBool.
ListStrBool = []MapStrBool // ListStrBool is alias of frequently-used slice type []MapStrBool.
ListIntBool = []MapIntBool // ListIntBool is alias of frequently-used slice type []MapIntBool.
)
type (
Slice = []interface{} // Slice is alias of frequently-used slice type []interface{}.
SliceAny = []interface{} // SliceAny is alias of frequently-used slice type []interface{}.
SliceStr = []string // SliceStr is alias of frequently-used slice type []string.
SliceInt = []int // SliceInt is alias of frequently-used slice type []int.
)
type (
Array = []interface{} // Array is alias of frequently-used slice type []interface{}.
ArrayAny = []interface{} // ArrayAny is alias of frequently-used slice type []interface{}.
ArrayStr = []string // ArrayStr is alias of frequently-used slice type []string.
ArrayInt = []int // ArrayInt is alias of frequently-used slice type []int.
)

15
os/gctx/gctx.go Normal file
View File

@ -0,0 +1,15 @@
// 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 gctx wraps context.Context and provides extra context features.
package gctx
import "context"
type (
Ctx = context.Context // Ctx is short name alias for context.Context.
StrKey string // StrKey is a type for warps basic type string as context key.
)

View File

@ -12,6 +12,7 @@ import (
"github.com/fatih/color"
"github.com/gogf/gf/container/gtype"
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/os/gctx"
"github.com/gogf/gf/os/gfpool"
"github.com/gogf/gf/os/gmlock"
"github.com/gogf/gf/os/gtimer"
@ -175,12 +176,16 @@ func (l *Logger) print(ctx context.Context, level int, values ...interface{}) {
// Context values.
if len(l.config.CtxKeys) > 0 {
ctxStr := ""
for _, key := range l.config.CtxKeys {
if v := ctx.Value(key); v != nil {
for _, ctxKey := range l.config.CtxKeys {
var ctxValue interface{}
if ctxValue = ctx.Value(ctxKey); ctxValue == nil {
ctxValue = ctx.Value(gctx.StrKey(gconv.String(ctxKey)))
}
if ctxValue != nil {
if ctxStr != "" {
ctxStr += ", "
}
ctxStr += fmt.Sprintf("%s: %+v", key, v)
ctxStr += fmt.Sprintf("%s: %+v", ctxKey, ctxValue)
}
}
if ctxStr != "" {