Documentation
¶
Overview ¶
Author: CJey Hou<cjey.hou@gmail.com>
Index ¶
- Variables
- func GetRealSession(ctx Context) string
- func GetSession(ctx Context) string
- func Gunzip(input []byte) ([]byte, error)
- func Gzip(input []byte) ([]byte, error)
- func Json(msg any) json.RawMessage
- func JsonPretty(msg any, prefix ...string) string
- func NewSimpleLogger(level, outpath, encoding string, disableCaller bool) (*zap.Logger, error)
- func ReplaceLogger(logger *zap.Logger) func()
- func SessionNameGenerator(header string) func(...string) string
- func SetSession(ctx Context, session string)
- func UseDefaultLogger() (func(), error)
- func UseDevelopLogger() (func(), error)
- func UseSimpleLogger(level, outpath, encoding string, disableCaller bool) (func(), error)
- type CancelFunc
- type Context
- func NamedContext(name string) Context
- func NewContext(gctx gcontext.Context, env Env, log Logger) Context
- func SessionalContext(prefix ...string) Context
- func SimpleContext() Context
- func ToNamedContext(gctx gcontext.Context, name string) Context
- func ToSessionalContext(gctx gcontext.Context, prefix ...string) Context
- func ToSimpleContext(gctx gcontext.Context) Context
- type ContextGenerator
- type Env
- type Event
- type LocationJoiner
- type Logger
- type NameJoiner
- type TaskGroup
Constants ¶
This section is empty.
Variables ¶
var ( Canceled = gcontext.Canceled DeadlineExceeded = gcontext.DeadlineExceeded )
variable alias
var ( BootID = uuid.NewString() BootTime = time.Now() )
Functions ¶
func GetRealSession ¶
GetRealSession use to get real session name
func GetSession ¶
GetSession use to get session name if it has real session, otherwise return context name instead
func Json ¶ added in v0.4.3
func Json(msg any) json.RawMessage
func JsonPretty ¶ added in v0.4.3
func NewSimpleLogger ¶
NewSimpleLogger 生成并返回一个简单的默认风格的zap.Logger
func ReplaceLogger ¶
ReplaceLogger 用给定的zap.Logger替换context内部的默认全局zap.Logger和zap.SugaredLogger
func SessionNameGenerator ¶
SessionNameGenerator 返回一个命名生成器,会自动在header后面追加12位长的自增字符串, 同时支持提供额外的前缀,会被附加在header之前。
func SetSession ¶
SetSession use to set real session name
func UseDefaultLogger ¶
func UseDefaultLogger() (func(), error)
UseDefaultLogger 使用预定义的简单zap.Logger替换全局默认zap.Logger。
func UseDevelopLogger ¶
func UseDevelopLogger() (func(), error)
UseDevlopLogger 使用预定义的简单zap.Logger替换全局默认zap.Logger,适合于开发、测试、写简单的工具时用。
func UseSimpleLogger ¶
UseSimpleLogger 使用简单的默认风格logger替换掉全局的zap.Logger
Types ¶
type Context ¶
type Context interface {
// composite
gcontext.Context
// Fork return a copied context, if there is a new goroutine generated
// it will use my name with a sequential number suffix started from 1
Fork() Context
// At return a copied context, specify the current location where it is in,
// it should chain all locations start from root
At(location string) Context
ForkAt(location string) Context
// Reborn will use gcontext.Background() instead of internal context,
// it used for escaping internal context's cancel request
Reborn() Context
// RebornWith will use specified context instead of internal context,
// it used for escaping internal context's cancel request
RebornWith(gcontext.Context) Context
// Name return my logger's name
Name() string
// Location return my logger's location
Location() string
// integrated base official context action
WithCancel() (Context, CancelFunc)
WithDeadline(time.Time) (Context, CancelFunc)
WithTimeout(time.Duration) (Context, CancelFunc)
WithValue(key, value any) Context
// Env return my env
// WARN: env value and official context value are two diffrent things
Env() Env
// shortcut methods of my env
Set(key, value any)
Get(key any) (value any, ok bool)
GetString(key any) string
GetInt(key any) int
GetUint(key any) uint
GetFloat(key any) float64
GetBool(key any) bool
// mute/unmute my logger
Mute()
Unmute()
// Logger return my logger
Logger() Logger
// shortcut methods of my logger
Debug(msg string, kvs ...any)
Debugf(template string, args ...any)
Info(msg string, kvs ...any)
Infof(template string, args ...any)
Warn(msg string, kvs ...any)
Warnf(template string, args ...any)
Error(msg string, kvs ...any)
Errorf(template string, args ...any)
Panic(msg string, kvs ...any)
Panicf(template string, args ...any)
Fatal(msg string, kvs ...any)
Fatalf(template string, args ...any)
}
Context extend default context package, make it better It should has name, location, environment and logger
func NamedContext ¶
NamedContext 返回一个简单的Context,Context的名称可以通过参数name实现自定义。
func NewContext ¶
NewContext use an official Context, an Env and a Logger to generate a new Context. It will use default value if not given.
func SessionalContext ¶
SessinalContext 返回一个简单的Context,命名部分使用自动生成的uuid。
func SimpleContext ¶
func SimpleContext() Context
SimpleContext return a very simple context, without name, without location, and use S() as internal logger
func ToNamedContext ¶
ToNamedContext 使用给定的标准库Context和名称列表构建一个Context。
func ToSessionalContext ¶
ToSesionalContext 将给定的标准库Context对象对位Context的内部基础对象,并自动生成uuid作为name。
func ToSimpleContext ¶
ToSimpleContext 将给定的标准库Context对象作为Context的内部基础对象。
type ContextGenerator ¶
type ContextGenerator = func() Context
Generator 定义了一个Context的生成函数,每次调用都应当返回一个新的Context
type Env ¶
type Env interface {
// Fork return an inherited sub Env, and I am it's parent
Fork() Env
// Set always set key & value at local storage
Set(key, value any)
// Get always check local, if the key not exists, then check parent
Get(key any) (value any, ok bool)
Has(key any) (ok bool)
Keys() []any
GetInt(key any) int
GetInt64(key any) int64
GetUint(key any) uint
GetUint64(key any) uint64
GetBool(key any) bool
GetFloat(key any) float64
GetString(key any) string
GetIP(key any) net.IP
GetAddr(key any) net.Addr
GetTime(key any) time.Time
GetDuration(key any) time.Duration
}
Env should used as a map, but it has an inherited mode, overlay liked. If you set, the value should be store at local. If you get, the value should be get from local first, otherwise from parent
type Event ¶
type Event struct {
Args []any // 触发事件时提供的关联数据
// contains filtered or unexported fields
}
Event 提供一个简单的一次性事件订阅和管理功能
type LocationJoiner ¶
LocationJoiner will generate a full location after joining the origin and given. Default logger use it to join location when invoking Fork()
type Logger ¶
type Logger interface {
// Fork return a new logger, with the given name and location,
// but it's full name and full location should inhert from me.
// It could just simplely join my name and location.
// e.g. my name = ServiceMonitor, name = go1, then new name = ServiceMonitor.go1.
// e.g. my location = CheckPort, location = CheckAlive, then new location = CheckPort/CheckAlive.
// name will be print as zap logger's name, mostly it looks like a uuid
// location will be print as a data field, field name is '@' mostly.
Fork(name, location string) Logger
// Name return my full name
Name() string
// Location return my full location
Location() string
// Debug("New user created", "name", "CJey", "sex", "male")
Debug(msg string, kvs ...any)
Debugf(template string, args ...any)
Info(msg string, kvs ...any)
Infof(template string, args ...any)
Warn(msg string, kvs ...any)
Warnf(template string, args ...any)
Error(msg string, kvs ...any)
Errorf(template string, args ...any)
Panic(msg string, kvs ...any)
Panicf(template string, args ...any)
Fatal(msg string, kvs ...any)
Fatalf(template string, args ...any)
// With return a Logger with specified key/value pairs
With(kvs ...any) Logger
// Sync flush log buffers
Sync() error
// Mute
Mute()
// Unmute
Unmute()
}
Logger is a logger, but it has name and location. When print message, it simulates zap's 'with' style, encourage print message and data seprately
func NewLogger ¶
func NewLogger(name, location string, z0 *zap.SugaredLogger, nj NameJoiner, lj LocationJoiner) Logger
NewLogger return a Logger, with name and location. name and location are optional, empty string means no name, no location. z0, nj, lj are optional too, default nj is nameJoiner, default lj is locationJoiner, but no default z0. nj and lj will be used when Fork() invoked
type NameJoiner ¶
NameJoiner will generate a full name after joining the origin and given. Default logger use it to join name when invoking Fork()
type TaskGroup ¶ added in v0.4.3
type TaskGroup struct {
// contains filtered or unexported fields
}