context

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Canceled         = gcontext.Canceled
	DeadlineExceeded = gcontext.DeadlineExceeded
)

variable alias

Functions

func GetRealSession added in v0.1.0

func GetRealSession(ctx Context) string

GetRealSession use to get real session name

func GetSession added in v0.1.0

func GetSession(ctx Context) string

GetSession use to get session name if it has real session, otherwise return context name instead

func SetSession added in v0.1.0

func SetSession(ctx Context, session string)

SetSession use to set real session name

Types

type CancelFunc

type CancelFunc = gcontext.CancelFunc

type alias

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 interface{}) 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 interface{})
	Get(key interface{}) (value interface{}, ok bool)
	GetString(key interface{}) string
	GetInt(key interface{}) int
	GetUint(key interface{}) uint
	GetFloat(key interface{}) float64
	GetBool(key interface{}) bool

	// Logger return my logger
	Logger() Logger
	// shortcut methods of my logger
	Debug(msg string, kvs ...interface{})
	Info(msg string, kvs ...interface{})
	Warn(msg string, kvs ...interface{})
	Error(msg string, kvs ...interface{})
	Panic(msg string, kvs ...interface{})
	Fatal(msg string, kvs ...interface{})
}

Context extend default context package, make it better It should has name, location, environment and logger

func New

func New(gctx gcontext.Context, env Env, logger Logger) Context

an official Context, an Env and a Logger. It will use default value if not given by

func Simple

func Simple() Context

Simple return a very simple context, without name, without location, and use zap.S() as internal logger

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 interface{})
	// Get always check local, if the key not exists, then check parent
	Get(key interface{}) (value interface{}, ok bool)
	Has(key interface{}) (ok bool)
	Keys() []interface{}

	GetInt(key interface{}) int
	GetInt64(key interface{}) int64
	GetUint(key interface{}) uint
	GetUint64(key interface{}) uint64
	GetBool(key interface{}) bool
	GetFloat(key interface{}) float64
	GetString(key interface{}) string
	GetIP(key interface{}) net.IP
	GetAddr(key interface{}) net.Addr
	GetTime(key interface{}) time.Time
	GetDuration(key interface{}) 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

func NewEnv

func NewEnv() Env

NewEnv return a simple Env, use sync.Map as it's storage

type LocationJoiner

type LocationJoiner func(origin, given string) (field, location string)

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 ...interface{})
	Debugf(template string, args ...interface{})
	Info(msg string, kvs ...interface{})
	Infof(template string, args ...interface{})
	Warn(msg string, kvs ...interface{})
	Warnf(template string, args ...interface{})
	Error(msg string, kvs ...interface{})
	Errorf(template string, args ...interface{})
	Panic(msg string, kvs ...interface{})
	Panicf(template string, args ...interface{})
	Fatal(msg string, kvs ...interface{})
	Fatalf(template string, args ...interface{})

	// With return a Logger with specified key/value pairs
	With(kvs ...interface{}) 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

type NameJoiner func(origin, given string) (name string)

NameJoiner will generate a full name after joining the origin and given. Default logger use it to join name when invoking Fork()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL