log

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2022 License: MulanPSL-2.0 Imports: 11 Imported by: 5

README

utils.log.go

介绍

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 (
	LvlInvalid = int(iota) //0
	LvlTrace               //1
	LvlDebug               //2
	LvlInfo                //3
	LvlWarning             //4
	LvlError               //5
	LvlFatal               //6
)

define log level

Variables

View Source
var (

	//BCodeLineDetail switch of detail code line string
	BCodeLineDetail = false
)

CtxNil this context does not do anything

View Source
var Stdout = &TargetStdout{}

Stdout Target to stdout

Functions

func BuiltinTimeFmtCompact

func BuiltinTimeFmtCompact(buff *strings.Builder, t time.Time)

BuiltinTimeFmtCompact is builtin FuncTimeFmt with format "20190422160345223".

func BuiltinTimeFmtStandard

func BuiltinTimeFmtStandard(buff *strings.Builder, t time.Time)

BuiltinTimeFmtStandard is builtin FuncTimeFmt with format "2019-04-22 16:03:45.223".

func GlobalLevel

func GlobalLevel() int

GlobalLevel get current global level. Global level is also the switch of log level check.

func SetDefaultTargets

func SetDefaultTargets(ts []Target)

SetDefaultTargets set target list for default. It is used for creating logger handler.

func SetGlobalHandler

func SetGlobalHandler(h LoggerHandler)

SetGlobalHandler return the global handler.

func SetGlobalHandlerConfig

func SetGlobalHandlerConfig(cfg *HandlerConfig)

SetGlobalHandlerConfig set global handler config

func SetGlobalLevel

func SetGlobalLevel(l int) int

SetGlobalLevel set current global level. return the new level after set. Global level is also the switch of log level check.

Types

type Context

type Context interface {
	//clear context content
	Clear()

	//finally output function.
	Z()

	//get log level of context.
	Level() int
	//set log level to context. just available before any assemble function.
	SetLevel(level int)

	//add a temporary prefix in current log message util Z().
	Pre(prefix string) Context

	//formatted key-value message field.
	FS(key string, val string) Context  //string value
	FI(key string, val int64) Context   //integer value
	FU(key string, val uint64) Context  //unsigned integer value
	FD(key string, val float64) Context //double-float value
	FX(key string, val uint64) Context  //hex value

	//formatted key-value message custom field.
	FT(key string, val FmtField) Context

	//add a message string.
	P(msg string) Context
	//add a message string with format string and args.
	PF(format string, args ...interface{}) Context
}

Context is a status set for processing in a message outputting. Every message must output with context. context must flush outputting by function Z().

func DBG

func DBG() Context

DBG can be a start point of static calling to log with debug level.

func ERR

func ERR() Context

ERR can be a start point of static calling to log with error level.

func FAT

func FAT() Context

FAT can be a start point of static calling to log with fatal level.

func INF

func INF() Context

INF can be a start point of static calling to log with info level.

func TRC

func TRC() Context

TRC can be a start point of static calling to log with trace level.

func WAR

func WAR() Context

WAR can be a start point of static calling to log with warning level.

type ContextAllowTar

type ContextAllowTar interface {
	Context

	//add a target in current log message util Z().
	Tar(t Target) ContextAllowTar
	//only use this target in current log message util Z().
	UniTar(t Target) Context
}

ContextAllowTar Context allow target specify

type ContextBase

type ContextBase struct {
	//common config info from handler
	Common *HandlerCommon
	// contains filtered or unexported fields
}

ContextBase is a status set for processing in a message outputting.

func NewContextBase

func NewContextBase(c *HandlerCommon, level int) *ContextBase

NewContextBase make a new ContextBase instance

func (*ContextBase) Clear

func (ctx *ContextBase) Clear()

Clear implement Context.Clear()

func (*ContextBase) FD

func (ctx *ContextBase) FD(key string, val float64) Context

FD implement Context.FD()

func (*ContextBase) FI

func (ctx *ContextBase) FI(key string, val int64) Context

FI implement Context.FI()

func (*ContextBase) FS

func (ctx *ContextBase) FS(key string, val string) Context

FS implement Context.FS()

func (*ContextBase) FT

func (ctx *ContextBase) FT(key string, val FmtField) Context

FT implement Context.FT()

func (*ContextBase) FU

func (ctx *ContextBase) FU(key string, val uint64) Context

FU implement Context.FU()

func (*ContextBase) FX

func (ctx *ContextBase) FX(key string, val uint64) Context

FX implement Context.FX()

func (*ContextBase) Level

func (ctx *ContextBase) Level() int

Level implement Context.Level()

func (*ContextBase) P

func (ctx *ContextBase) P(msg string) Context

P implement for Context.P()

func (*ContextBase) PF

func (ctx *ContextBase) PF(msg string, args ...interface{}) Context

PF implement for Context.PF()

func (*ContextBase) Pre

func (ctx *ContextBase) Pre(prefix string) Context

Pre implement Context.Pre()

func (*ContextBase) SetLevel

func (ctx *ContextBase) SetLevel(level int)

SetLevel implement Context.SetLevel()

func (*ContextBase) Tar

func (ctx *ContextBase) Tar(t Target) ContextAllowTar

Tar implement for Context.Tar()

func (*ContextBase) UniTar

func (ctx *ContextBase) UniTar(t Target) Context

UniTar implement for Context.UniTar()

func (*ContextBase) Z

func (ctx *ContextBase) Z()

Z implement for Context.Z()

type ContextBaseAssembleFunc

type ContextBaseAssembleFunc func(*ContextBase) string

ContextBaseAssembleFunc assemble function for ContextBase implement

type ContextNil

type ContextNil struct {
}

ContextNil this context does not do anything

func (*ContextNil) Clear

func (ctx *ContextNil) Clear()

Clear implement Context.Clear()

func (*ContextNil) FD

func (ctx *ContextNil) FD(key string, val float64) Context

FD implement Context.FD()

func (*ContextNil) FI

func (ctx *ContextNil) FI(key string, val int64) Context

FI implement Context.FI()

func (*ContextNil) FS

func (ctx *ContextNil) FS(key string, val string) Context

FS implement Context.FS()

func (*ContextNil) FT

func (ctx *ContextNil) FT(key string, val FmtField) Context

FT implement Context.FT()

func (*ContextNil) FU

func (ctx *ContextNil) FU(key string, val uint64) Context

FU implement Context.FU()

func (*ContextNil) FX

func (ctx *ContextNil) FX(key string, val uint64) Context

FX implement Context.FX()

func (*ContextNil) Level

func (ctx *ContextNil) Level() int

Level implement Context.Level()

func (*ContextNil) P

func (ctx *ContextNil) P(msg string) Context

P implement Context.P()

func (*ContextNil) PF

func (ctx *ContextNil) PF(format string, args ...interface{}) Context

PF implement Context.PF()

func (*ContextNil) Pre

func (ctx *ContextNil) Pre(prefix string) Context

Pre implement Context.Pre()

func (*ContextNil) SetLevel

func (ctx *ContextNil) SetLevel(l int)

SetLevel implement Context.SetLevel()

func (*ContextNil) Tar

func (ctx *ContextNil) Tar(t Target) ContextAllowTar

Tar implement Context.Tar()

func (*ContextNil) UniTar

func (ctx *ContextNil) UniTar(t Target) Context

UniTar implement Context.UniTar()

func (*ContextNil) Z

func (ctx *ContextNil) Z()

Z implement Context.Z()

type ContextPool

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

ContextPool pool of context instances

func NewContextPool

func NewContextPool(common *HandlerCommon, size int) *ContextPool

NewContextPool build a ContextPool instance

func (*ContextPool) Get

func (p *ContextPool) Get(level int) ContextAllowTar

Get get a instance

type FFArrFloat64

type FFArrFloat64 []float64

FFArrFloat64 define FmtField for []float64

func (FFArrFloat64) AssembleAsJSON

func (f FFArrFloat64) AssembleAsJSON(buff *strings.Builder, com *HandlerCommon)

AssembleAsJSON implement FmtField.AssembleAsJSON()

func (FFArrFloat64) AssembleAsLineSeparator

func (f FFArrFloat64) AssembleAsLineSeparator(buff *strings.Builder, com *HandlerCommon)

AssembleAsLineSeparator implement FmtField.AssembleAsLineSeparator()

type FFArrInt64

type FFArrInt64 []int64

FFArrInt64 define FmtField for []int64

func (FFArrInt64) AssembleAsJSON

func (f FFArrInt64) AssembleAsJSON(buff *strings.Builder, com *HandlerCommon)

AssembleAsJSON implement FmtField.AssembleAsJSON()

func (FFArrInt64) AssembleAsLineSeparator

func (f FFArrInt64) AssembleAsLineSeparator(buff *strings.Builder, com *HandlerCommon)

AssembleAsLineSeparator implement FmtField.AssembleAsLineSeparator()

type FFArrString

type FFArrString []string

FFArrString define FmtField for []string

func (FFArrString) AssembleAsJSON

func (f FFArrString) AssembleAsJSON(buff *strings.Builder, com *HandlerCommon)

AssembleAsJSON implement FmtField.AssembleAsJSON()

func (FFArrString) AssembleAsLineSeparator

func (f FFArrString) AssembleAsLineSeparator(buff *strings.Builder, com *HandlerCommon)

AssembleAsLineSeparator implement FmtField.AssembleAsLineSeparator()

type FFArrUint64

type FFArrUint64 []uint64

FFArrUint64 define FmtField for []uint64

func (FFArrUint64) AssembleAsJSON

func (f FFArrUint64) AssembleAsJSON(buff *strings.Builder, com *HandlerCommon)

AssembleAsJSON implement FmtField.AssembleAsJSON()

func (FFArrUint64) AssembleAsLineSeparator

func (f FFArrUint64) AssembleAsLineSeparator(buff *strings.Builder, com *HandlerCommon)

AssembleAsLineSeparator implement FmtField.AssembleAsLineSeparator()

type FFMapStringFloat64

type FFMapStringFloat64 map[string]float64

FFMapStringFloat64 define FmtField for map[string]float64

func (FFMapStringFloat64) AssembleAsJSON

func (f FFMapStringFloat64) AssembleAsJSON(buff *strings.Builder, com *HandlerCommon)

AssembleAsJSON implement FmtField.AssembleAsJSON()

func (FFMapStringFloat64) AssembleAsLineSeparator

func (f FFMapStringFloat64) AssembleAsLineSeparator(buff *strings.Builder, com *HandlerCommon)

AssembleAsLineSeparator implement FmtField.AssembleAsLineSeparator()

type FFMapStringInt64

type FFMapStringInt64 map[string]int64

FFMapStringInt64 define FmtField for map[string]int64

func (FFMapStringInt64) AssembleAsJSON

func (f FFMapStringInt64) AssembleAsJSON(buff *strings.Builder, com *HandlerCommon)

AssembleAsJSON implement FmtField.AssembleAsJSON()

func (FFMapStringInt64) AssembleAsLineSeparator

func (f FFMapStringInt64) AssembleAsLineSeparator(buff *strings.Builder, com *HandlerCommon)

AssembleAsLineSeparator implement FmtField.AssembleAsLineSeparator()

type FFMapStringString

type FFMapStringString map[string]string

FFMapStringString define FmtField for map[string]string

func (FFMapStringString) AssembleAsJSON

func (f FFMapStringString) AssembleAsJSON(buff *strings.Builder, com *HandlerCommon)

AssembleAsJSON implement FmtField.AssembleAsJSON()

func (FFMapStringString) AssembleAsLineSeparator

func (f FFMapStringString) AssembleAsLineSeparator(buff *strings.Builder, com *HandlerCommon)

AssembleAsLineSeparator implement FmtField.AssembleAsLineSeparator()

type FFMapStringUint64

type FFMapStringUint64 map[string]uint64

FFMapStringUint64 define FmtField for map[string]uint64

func (FFMapStringUint64) AssembleAsJSON

func (f FFMapStringUint64) AssembleAsJSON(buff *strings.Builder, com *HandlerCommon)

AssembleAsJSON implement FmtField.AssembleAsJSON()

func (FFMapStringUint64) AssembleAsLineSeparator

func (f FFMapStringUint64) AssembleAsLineSeparator(buff *strings.Builder, com *HandlerCommon)

AssembleAsLineSeparator implement FmtField.AssembleAsLineSeparator()

type FmtField

type FmtField interface {
	//assemble field content as line with separator
	AssembleAsLineSeparator(*strings.Builder, *HandlerCommon)
	//assemble field content as json string
	AssembleAsJSON(*strings.Builder, *HandlerCommon)
}

FmtField define abstract formatting field

type FuncTimeFmt

type FuncTimeFmt func(*strings.Builder, time.Time)

FuncTimeFmt define declaration of time format function

type Handler

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

Handler is the builtin implement for LoggerHandler.

func NewHandlerWithConfig

func NewHandlerWithConfig(cfg *HandlerConfig) *Handler

NewHandlerWithConfig make a new Handler instance with configuration.

func (*Handler) AddPrefix

func (h *Handler) AddPrefix(key, val string)

AddPrefix implement for LoggerHandler.AddPrefix()

func (*Handler) AddTarget

func (h *Handler) AddTarget(t Target)

AddTarget implement for LoggerHandler.AddTarget()

func (*Handler) BindTimeFmt

func (h *Handler) BindTimeFmt(f FuncTimeFmt)

BindTimeFmt implement for LoggerHandler.BindTimeFmt()

func (*Handler) Clone

func (h *Handler) Clone() LoggerHandler

Clone implement for LoggerHandler.Clone()

func (*Handler) Ctx

func (h *Handler) Ctx() ContextAllowTar

Ctx implement for Logger.Ctx()

func (*Handler) DBG

func (h *Handler) DBG() ContextAllowTar

DBG implement for Logger.DBG()

func (*Handler) DelPrefix

func (h *Handler) DelPrefix(key string)

DelPrefix implement for LoggerHandler.DelPrefix()

func (*Handler) DelTarget

func (h *Handler) DelTarget(name string)

DelTarget implement for LoggerHandler.DelTarget()

func (*Handler) ERR

func (h *Handler) ERR() ContextAllowTar

ERR implement for Logger.ERR()

func (*Handler) FAT

func (h *Handler) FAT() ContextAllowTar

FAT implement for Logger.FAT()

func (*Handler) INF

func (h *Handler) INF() ContextAllowTar

INF implement for Logger.INF()

func (*Handler) Level

func (h *Handler) Level() int

Level implement for LoggerHandler.Level()

func (*Handler) Release

func (h *Handler) Release()

Release implement for LoggerHandler.Release()

func (*Handler) SetLevel

func (h *Handler) SetLevel(l int)

SetLevel implement for LoggerHandler.SetLevel()

func (*Handler) SetSeparator

func (h *Handler) SetSeparator(sep string)

SetSeparator implement for LoggerHandler.SetSeparator()

func (*Handler) SwitchCodeLine

func (h *Handler) SwitchCodeLine(b bool)

SwitchCodeLine implement for LoggerHandler.SwitchCodeLine

func (*Handler) SwitchJSONAssemble

func (h *Handler) SwitchJSONAssemble(b bool)

SwitchJSONAssemble implement for LoggerHandler.SwitchJSONAssemble

func (*Handler) TRC

func (h *Handler) TRC() ContextAllowTar

TRC implement for Logger.TRC()

func (*Handler) WAR

func (h *Handler) WAR() ContextAllowTar

WAR implement for Logger.WAR()

type HandlerCommon

type HandlerCommon struct {
	//separator char between message field.
	Sep string
	//separator char in format message field.
	SepFmt string

	//time format function bond with handler.
	TimeFmt FuncTimeFmt

	//outputting target list.
	Targets []Target

	//prefixes
	Prefixes [][2]string
	//prefix string cache
	PrefixStr string

	//switch of code line outputting
	BCodeLine bool

	//switch of JSON assemble using
	UseJSONAssemble bool
}

HandlerCommon common config info of handler

type HandlerConfig

type HandlerConfig struct {
	//log level
	Level int `json:"level,omitempty"`

	//separator char between message field
	Separator string `json:"separator,omitempty"`
	//separator char in format message field
	FmtSeparator string `json:"fmt_separator,omitempty"`

	//switch of code line outputting
	BCodeLine bool `json:"b_code_line,omitempty"`

	//switch of assemble message using JSON
	BUseJSONAssemble bool `json:"b_use_json_assemble,omitempty"`

	//size of idle context instance in handler context pool
	ContextPoolSize int
}

HandlerConfig define config options of Handler

func GlobalHandlerConfig

func GlobalHandlerConfig() *HandlerConfig

GlobalHandlerConfig get global handler config

type Logger

type Logger interface {
	//start logging message
	Ctx() ContextAllowTar

	//start logging message with specify log level
	TRC() ContextAllowTar
	DBG() ContextAllowTar
	INF() ContextAllowTar
	WAR() ContextAllowTar
	ERR() ContextAllowTar
	FAT() ContextAllowTar
}

Logger Interface about output behaviors of log handler.

func Log

func Log() Logger

Log is the start point of static calling to output log message.

type LoggerHandler

type LoggerHandler interface {
	//LoggerHandler is a superset of Logger.
	Logger

	//clone a new LoggerHandler instance with same configuration.
	//context will not be clone.
	Clone() LoggerHandler

	//bind time format function.
	//time format function is used to format time "now" for outputting log message.
	//if param f is nil, log message have no time string.
	BindTimeFmt(f FuncTimeFmt)

	//set separator of log format. default separator is "|".
	SetSeparator(sep string)

	//get current log level
	Level() int
	//set log level for handler.
	SetLevel(l int)

	//add a output target to handler.
	AddTarget(t Target)
	//del a output target from handler.
	DelTarget(name string)

	//add a prefix.
	//every prefix string will be joined to every log message.
	//param key is a inner identifier. only param value will be joined to message.
	AddPrefix(key, val string)
	//delete a prefix by key.
	DelPrefix(key string)

	//switch whether outputting code line in log message.
	SwitchCodeLine(b bool)

	//switch whether use json to assemble log message.
	SwitchJSONAssemble(b bool)

	//release log handler resources
	Release()
}

LoggerHandler Interface of log handler. LoggerHandler is an abstract definition. A log handler is a object instance that can be configured with some properties. And provides interfaces to output log message.

func GlobalHandler

func GlobalHandler() LoggerHandler

GlobalHandler return the global handler. It is the handler accepting static Logger function.

func NewHandler

func NewHandler() LoggerHandler

NewHandler return a Handler instance as a LoggerHandler interface instance.

type RotateConfig

type RotateConfig struct {
	Dir    string
	Prefix string
	Ext    string

	//seconds
	RotateDuring int64

	//bytes
	RotateSize int64
}

RotateConfig 滚动配置

type Target

type Target interface {
	//name of target.
	Name() string

	//using count of target
	IncrUsing()

	//check whether target need ignore a log message.
	//param l is log level.
	IsIgnore(l int) bool

	//output message
	Put(msg string)

	//close target resources
	Close()
}

Target is abstract interface of output target definition.

func GetDefaultTargets

func GetDefaultTargets() []Target

GetDefaultTargets get target list from default

type TargetFile

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

TargetFile implement Target with normal file printing.

func MakeTargetFile

func MakeTargetFile(dir string, fn string) (*TargetFile, error)

MakeTargetFile make a TargetFile instance with specific directory and file name.

func (*TargetFile) Close

func (t *TargetFile) Close()

Close close file

func (*TargetFile) IncrUsing

func (t *TargetFile) IncrUsing()

IncrUsing implement for Target.IncrUsing()

func (*TargetFile) IsIgnore

func (t *TargetFile) IsIgnore(l int) bool

IsIgnore implement for Target.IsIgnore()

func (*TargetFile) Name

func (t *TargetFile) Name() string

Name implement for Target.Name()

func (*TargetFile) Put

func (t *TargetFile) Put(msg string)

Put implement for Target.Put()

func (*TargetFile) ReopenFile

func (t *TargetFile) ReopenFile() error

ReopenFile open file again. used for tools like logrotate.

func (*TargetFile) SetLevels

func (t *TargetFile) SetLevels(ls []int)

SetLevels set levels this target should output.

type TargetFileRotate

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

TargetFileRotate file target with rotation.

func MakeTargetFileRotate

func MakeTargetFileRotate(cfg *RotateConfig) (*TargetFileRotate, error)

MakeTargetFileRotate make a TargetFileRotate instance.

func (*TargetFileRotate) Close

func (t *TargetFileRotate) Close()

Close close file

func (*TargetFileRotate) IncrUsing

func (t *TargetFileRotate) IncrUsing()

IncrUsing implement for Target.IncrUsing()

func (*TargetFileRotate) IsIgnore

func (t *TargetFileRotate) IsIgnore(l int) bool

IsIgnore implement for Target.IsIgnore()

func (*TargetFileRotate) Name

func (t *TargetFileRotate) Name() string

Name implement for Target.Name()

func (*TargetFileRotate) Put

func (t *TargetFileRotate) Put(msg string)

Put implement for Target.Put()

func (*TargetFileRotate) SetLevels

func (t *TargetFileRotate) SetLevels(ls []int)

SetLevels set levels this target should output.

type TargetStdout

type TargetStdout struct {
}

TargetStdout implement Target with stdout printing.

func (*TargetStdout) Close

func (t *TargetStdout) Close()

Close implement for Target.Close()

func (*TargetStdout) IncrUsing

func (t *TargetStdout) IncrUsing()

IncrUsing implement for Target.IncrUsing()

func (*TargetStdout) IsIgnore

func (t *TargetStdout) IsIgnore(l int) bool

IsIgnore implement for Target.IsIgnore()

func (*TargetStdout) Name

func (t *TargetStdout) Name() string

Name implement for Target.Name()

func (*TargetStdout) Put

func (t *TargetStdout) Put(msg string)

Put implement for Target.Put()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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