logger

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: MulanPSL-2.0 Imports: 12 Imported by: 0

README

wgs.ths.log.go

介绍

属于WGS的THS项目集. Go实现环境通用的句柄风格的日志组件接口.

软件架构

软件架构说明

安装教程
  1. xxxx
  2. xxxx
  3. xxxx
使用说明
  1. xxxx
  2. xxxx
  3. xxxx
参与贡献
  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
特技
  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. Gitee 官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解 Gitee 上的优秀开源项目
  4. GVP 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
  5. Gitee 官方提供的使用手册 https://gitee.com/help
  6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 https://gitee.com/gitee-stars/

Documentation

Index

Constants

View Source
const MaxSwitchModule int32 = 256

Variables

View Source
var DefaultContextPool = IContextPool(newContextPool(256))
View Source
var DefaultFormatterJson = &FormatterJson{}
View Source
var DefaultFormatterSeparator = &FormatterSeparator{
	Separator:   "|",
	KVSeparator: "=",
}
View Source
var DefaultGlobalLevel = LevelDebug
View Source
var DefaultTargetStdout = &TargetStdout{}
View Source
var GlobalSwitch = makeSwitch()
View Source
var LevelStrings = []string{
	"NON",
	"TRC",
	"DBG",
	"INF",
	"WRN",
	"ERR",
	"FAT",
	"IGN",
}

Functions

func ResetDefaultLogger added in v0.2.0

func ResetDefaultLogger(l *Logger)

func TimeFormatNow

func TimeFormatNow(format string, now time.Time) string

func TimeFormatNowToBuffer

func TimeFormatNowToBuffer(dst []byte, format string, now time.Time) int

Types

type Context

type Context struct {
	Now time.Time
	// contains filtered or unexported fields
}

func NewContext

func NewContext(pool IContextPool) *Context

func (*Context) Buffer

func (c *Context) Buffer() *bytes.Buffer

func (*Context) D

func (c *Context) D(k string, v float64) IContext

func (*Context) I

func (c *Context) I(k string, v int64) IContext

func (*Context) Level

func (c *Context) Level() Level

func (*Context) Msg

func (c *Context) Msg(msg string) IContext

func (*Context) Pre

func (c *Context) Pre(p string) IContext

func (*Context) S

func (c *Context) S(k string, v string) IContext

func (*Context) U

func (c *Context) U(k string, v uint64) IContext

func (*Context) Z

func (c *Context) Z()

type ContextPool

type ContextPool struct {
	NewCnt int64
	// contains filtered or unexported fields
}

func (*ContextPool) Get

func (p *ContextPool) Get() *Context

func (*ContextPool) PoolSize

func (p *ContextPool) PoolSize() int

func (*ContextPool) Put

func (p *ContextPool) Put(ctx *Context)

type FakeTarget

type FakeTarget struct {
	Level    Level
	PrintCnt int
}

func (*FakeTarget) IgnoreCheck

func (t *FakeTarget) IgnoreCheck(ctx *Context) bool

func (*FakeTarget) Print

func (t *FakeTarget) Print(ctx *Context)

type Formatter

type Formatter interface {
	Begin(ctx *Context)
	End(ctx *Context)

	AppendTimeBytes(ctx *Context, bs []byte)
	AppendLevel(ctx *Context, lv Level)

	PrefixBegin(ctx *Context)
	AppendPrefix(ctx *Context, index int, pre string)
	PrefixEnd(ctx *Context)

	FieldBegin(ctx *Context)
	AppendFieldInt64(ctx *Context, index int, k string, v int64)
	AppendFieldUint64(ctx *Context, index int, k string, v uint64)
	AppendFieldFloat64(ctx *Context, index int, k string, v float64)
	AppendFieldString(ctx *Context, index int, k string, v string)
	FieldEnd(ctx *Context)

	AppendMessage(ctx *Context, msg string)

	AppendCodeLine(ctx *Context, file string, line int)
}

type FormatterJson

type FormatterJson struct {
	PrefixLikeMap bool
}

func (*FormatterJson) AppendCodeLine added in v0.2.0

func (f *FormatterJson) AppendCodeLine(ctx *Context, file string, line int)

func (*FormatterJson) AppendFieldFloat64

func (f *FormatterJson) AppendFieldFloat64(ctx *Context, index int, k string, v float64)

func (*FormatterJson) AppendFieldInt64

func (f *FormatterJson) AppendFieldInt64(ctx *Context, index int, k string, v int64)

func (*FormatterJson) AppendFieldString

func (f *FormatterJson) AppendFieldString(ctx *Context, index int, k string, v string)

func (*FormatterJson) AppendFieldUint64

func (f *FormatterJson) AppendFieldUint64(ctx *Context, index int, k string, v uint64)

func (*FormatterJson) AppendLevel

func (f *FormatterJson) AppendLevel(ctx *Context, lv Level)

func (*FormatterJson) AppendMessage

func (f *FormatterJson) AppendMessage(ctx *Context, msg string)

func (*FormatterJson) AppendPrefix

func (f *FormatterJson) AppendPrefix(ctx *Context, index int, pre string)

func (*FormatterJson) AppendTimeBytes

func (f *FormatterJson) AppendTimeBytes(ctx *Context, bs []byte)

func (*FormatterJson) Begin

func (f *FormatterJson) Begin(ctx *Context)

func (*FormatterJson) End

func (f *FormatterJson) End(ctx *Context)

func (*FormatterJson) FieldBegin

func (f *FormatterJson) FieldBegin(ctx *Context)

func (*FormatterJson) FieldEnd

func (f *FormatterJson) FieldEnd(ctx *Context)

func (*FormatterJson) PrefixBegin

func (f *FormatterJson) PrefixBegin(ctx *Context)

func (*FormatterJson) PrefixEnd

func (f *FormatterJson) PrefixEnd(ctx *Context)

type FormatterSeparator

type FormatterSeparator struct {
	Separator   string
	KVSeparator string
}

func (*FormatterSeparator) AppendCodeLine added in v0.2.0

func (f *FormatterSeparator) AppendCodeLine(ctx *Context, file string, line int)

func (*FormatterSeparator) AppendFieldFloat64

func (f *FormatterSeparator) AppendFieldFloat64(ctx *Context, index int, k string, v float64)

func (*FormatterSeparator) AppendFieldInt64

func (f *FormatterSeparator) AppendFieldInt64(ctx *Context, index int, k string, v int64)

func (*FormatterSeparator) AppendFieldString

func (f *FormatterSeparator) AppendFieldString(ctx *Context, index int, k string, v string)

func (*FormatterSeparator) AppendFieldUint64

func (f *FormatterSeparator) AppendFieldUint64(ctx *Context, index int, k string, v uint64)

func (*FormatterSeparator) AppendLevel

func (f *FormatterSeparator) AppendLevel(ctx *Context, lv Level)

func (*FormatterSeparator) AppendMessage

func (f *FormatterSeparator) AppendMessage(ctx *Context, msg string)

func (*FormatterSeparator) AppendPrefix

func (f *FormatterSeparator) AppendPrefix(ctx *Context, index int, pre string)

func (*FormatterSeparator) AppendTimeBytes

func (f *FormatterSeparator) AppendTimeBytes(ctx *Context, bs []byte)

func (*FormatterSeparator) Begin

func (f *FormatterSeparator) Begin(ctx *Context)

func (*FormatterSeparator) End

func (f *FormatterSeparator) End(ctx *Context)

func (*FormatterSeparator) FieldBegin

func (f *FormatterSeparator) FieldBegin(ctx *Context)

func (*FormatterSeparator) FieldEnd

func (f *FormatterSeparator) FieldEnd(ctx *Context)

func (*FormatterSeparator) PrefixBegin

func (f *FormatterSeparator) PrefixBegin(ctx *Context)

func (*FormatterSeparator) PrefixEnd

func (f *FormatterSeparator) PrefixEnd(ctx *Context)

type IContext

type IContext interface {
	Level() Level

	//添加一个前缀
	Pre(p string) IContext

	//添加一个KV对Int64
	I(k string, v int64) IContext

	//添加一个KV对Uint64
	U(k string, v uint64) IContext

	//添加一个KV对Float64
	D(k string, v float64) IContext

	//添加一个KV对String
	S(k string, v string) IContext

	//语句消息, 只能有一个
	Msg(msg string) IContext

	//结束日志拼装并输出
	Z()
}

用interface裁剪用户代码的调用范围

func DBG added in v0.2.0

func DBG() IContext

func ERR added in v0.2.0

func ERR() IContext

func FAT added in v0.2.0

func FAT() IContext

func INF added in v0.2.0

func INF() IContext

func TRC added in v0.2.0

func TRC() IContext

func WRN added in v0.2.0

func WRN() IContext

type IContextPool

type IContextPool interface {
	PoolSize() int

	Get() *Context
	Put(ctx *Context)
}

type Level

type Level int
const (
	LevelNone Level = iota
	LevelTrace
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
	LevelFatal
	LevelIgnore
)

func (Level) String

func (lv Level) String() string

type Logger

type Logger struct {
	Config LoggerConfig
	Level  Level
	// contains filtered or unexported fields
}
var DefaultLogger *Logger

func NewLogger

func NewLogger(cfg *LoggerConfig) (*Logger, error)

func (*Logger) AddTarget

func (l *Logger) AddTarget(t Target)

unsafe for concurrent use should be called before dispatch and any log

func (*Logger) AppendPrefix

func (l *Logger) AppendPrefix(prefix string)

func (*Logger) AppendPrefixExclusive

func (l *Logger) AppendPrefixExclusive(prefix string, key string)

func (*Logger) Clone

func (l *Logger) Clone() *Logger

func (*Logger) DBG

func (l *Logger) DBG() IContext

func (*Logger) ERR

func (l *Logger) ERR() IContext

func (*Logger) FAT

func (l *Logger) FAT() IContext

func (*Logger) INF

func (l *Logger) INF() IContext

func (*Logger) SwitchChecker

func (l *Logger) SwitchChecker() *SwitchChecker

func (*Logger) TRC

func (l *Logger) TRC() IContext

func (*Logger) WRN

func (l *Logger) WRN() IContext

type LoggerConfig

type LoggerConfig struct {
	IgnoreStdout bool

	TimeFormat             string //使用 github.com/phuslu/fasttime 的格式化字符串
	TimeFormatBufferLength int

	CodeLine           bool
	CodeLineSkipCaller int
	CodeLineLong       bool

	Formatter Formatter

	ContextPool IContextPool

	SwitchChecker *SwitchChecker
}

func (*LoggerConfig) FixDefault

func (cfg *LoggerConfig) FixDefault()

type Switch

type Switch struct {
	// contains filtered or unexported fields
}

bitset模型的模块日志开关 模块号请用户自行定义, 从0开始, 最大值为MaxSwitchModule-1

func (*Switch) Check

func (s *Switch) Check(module int32, innerMask uint64) bool

func (*Switch) Get

func (s *Switch) Get(module int32) uint64

func (*Switch) Set

func (s *Switch) Set(module int32, innerMask uint64)

func (*Switch) Unset

func (s *Switch) Unset(module int32, innerMask uint64)

type SwitchChecker

type SwitchChecker struct {
	// contains filtered or unexported fields
}

模块日志开关检查器, 只可配置一个模块. 用于在运行时动态调整日志开关. 如果想要配置多个模块, 请分开使用多个Logger

func NewSwitchChecker

func NewSwitchChecker(module int32, mask uint64) *SwitchChecker

func (*SwitchChecker) Check

func (sc *SwitchChecker) Check(s *Switch) bool

func (*SwitchChecker) Clone

func (sc *SwitchChecker) Clone() *SwitchChecker

type Target

type Target interface {
	IgnoreCheck(*Context) bool
	Print(*Context)
}

type TargetFile

type TargetFile struct {
	Level Level
	File  *os.File
	// contains filtered or unexported fields
}

func NewTargetFile

func NewTargetFile(_cfg *TargetFileConfig) (*TargetFile, error)

func (*TargetFile) Close

func (t *TargetFile) Close() error

func (*TargetFile) IgnoreCheck

func (t *TargetFile) IgnoreCheck(ctx *Context) bool

func (*TargetFile) Print

func (t *TargetFile) Print(ctx *Context)

type TargetFileConfig

type TargetFileConfig struct {
	Filename string
	Ext      string

	RotatePerSizeKB     int64
	RotatePerTimeMinute int64
	// contains filtered or unexported fields
}

func (*TargetFileConfig) FixDefault

func (cfg *TargetFileConfig) FixDefault()

type TargetStdout

type TargetStdout struct {
	Level Level
}

func (*TargetStdout) IgnoreCheck

func (t *TargetStdout) IgnoreCheck(ctx *Context) bool

func (*TargetStdout) Print

func (t *TargetStdout) Print(ctx *Context)

Jump to

Keyboard shortcuts

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