logs

package
v0.0.0-...-680e691 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2021 License: Apache-2.0 Imports: 25 Imported by: 0

README

glog golang日志库

有很多优秀的开源日志库,比如logrus,zap,他们都是基于structured log思想实现,其中zap效率更高,logrus开发更早,使用更广泛。之所以重复造轮子,主要是可以更灵活的控制

与logrus,zap的一些差异

  • 提供了一个异步队列,在开发环境可以使用同步输出,在线上可以使用异步输出,但可能会丢失日志
  • 所有输出都是对等一个Channel,而不是logrus中的Writer+Hook的模式,提供了几个常见的channel,包括console,file,graylog,AsyncChannel
  • 提供了一个类似Log4j的Layout输出格式解析
  • 增加Tags信息,用于log初始化时设置env,host,idc,facility,psm,cluster,pod,stage,unit等信息
  • 对于context.Context处理,在很多RPC服务中,通常会透传context,在打印日志时,第一个参数通常会传入ctx,调用者通常会通过Context向Fileds中写入RequestID等信息,对日志系统而言本身并不知道如何处理context,可以配合Filter设置相关Field

使用方法

func main() {
    // 初始化channel和相关配置
    conf := NewConfig()
	conf.AddTags(map[string]string{
		"facility": "staging_test",
	})

	url := "testing url"
	channel := NewGraylogChannel(WithURL(url))
	conf.AddChannels(channel)

    logger := NewLogger(conf)
    // 替换默认logger
    SetDefault(logger)
	logger.Infof(nil, "test glog")
}

TODO

  • 测试graylog,elastic
  • 完善file rotate

Documentation

Index

Constants

View Source
const (
	DefaultMax       = 10000 // 默认日志最大条数
	DefaultCallDepth = 3     // 堆栈深度,忽略log相关的堆栈
)

Variables

View Source
var (
	DefaultFormatter = MustNewTextFormatter(defaultTextLayout)
)
View Source
var (
	ErrNotReady = fmt.Errorf("channel not ready")
)

Functions

func Debug

func Debug(msg string, fields ...Field)

func Debugf

func Debugf(format string, args ...interface{})

func Error

func Error(msg string, fields ...Field)

func Errorf

func Errorf(format string, args ...interface{})

func Fatal

func Fatal(msg string, fields ...Field)

func Fatalf

func Fatalf(format string, args ...interface{})

func Info

func Info(msg string, fields ...Field)

func Infof

func Infof(format string, args ...interface{})

func IsTerminal

func IsTerminal(fd int) bool

func SetDefault

func SetDefault(l Logger)

SetDefault 设置默认的log

func Trace

func Trace(msg string, fields ...Field)

func Tracef

func Tracef(format string, args ...interface{})

func Warn

func Warn(msg string, fields ...Field)

func Warnf

func Warnf(format string, args ...interface{})

Types

type Action

type Action struct {
	Key    byte        // %xx
	Prefix string      // xx%
	Min    int         // 最小宽度
	Max    int         // 最大宽度
	Param  string      // %x{param},原始参数
	Data   interface{} // 计算处理后数据
}

Action {$prefix}%x{-$min.$max}token{$param}

type BaseChannel

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

BaseChannel 默认实现

func (*BaseChannel) Close

func (c *BaseChannel) Close() error

func (*BaseChannel) Format

func (c *BaseChannel) Format(e *Entry) []byte

func (*BaseChannel) Init

func (c *BaseChannel) Init(o *ChannelOptions)

func (*BaseChannel) IsEnable

func (c *BaseChannel) IsEnable(lv Level) bool

func (*BaseChannel) Level

func (c *BaseChannel) Level() Level

func (*BaseChannel) Open

func (c *BaseChannel) Open() error

func (*BaseChannel) SetLevel

func (c *BaseChannel) SetLevel(lv Level)

type BatchChannel

type BatchChannel interface {
	Channel
	WriteBatch(msg []*Entry)
}

BatchChannel 以batch形式发送,有需要的可以

type Buffer

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

Buffer ...

func NewBuffer

func NewBuffer() *Buffer

NewBuffer 从缓存中获取一个Buffer

func (*Buffer) AppendBool

func (b *Buffer) AppendBool(v bool)

func (*Buffer) AppendByte

func (b *Buffer) AppendByte(v byte)

func (*Buffer) AppendComplex128

func (b *Buffer) AppendComplex128(val complex128)

func (*Buffer) AppendComplex64

func (b *Buffer) AppendComplex64(val complex64)

func (*Buffer) AppendFloat32

func (b *Buffer) AppendFloat32(f float32)

func (*Buffer) AppendFloat64

func (b *Buffer) AppendFloat64(f float64)

func (*Buffer) AppendInt

func (b *Buffer) AppendInt(i int64)

func (*Buffer) AppendString

func (b *Buffer) AppendString(s string)

func (*Buffer) AppendTime

func (b *Buffer) AppendTime(t time.Time, layout string)

func (*Buffer) AppendUint

func (b *Buffer) AppendUint(i uint64)

func (*Buffer) Appendf

func (b *Buffer) Appendf(format string, args ...interface{})

Appendf 带有格式化写入message

func (*Buffer) At

func (b *Buffer) At(i int) byte

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes 返回Bytes数据

func (*Buffer) Cap

func (b *Buffer) Cap() int

func (*Buffer) Empty

func (b *Buffer) Empty() bool

Empty 判断是否为空

func (*Buffer) Free

func (b *Buffer) Free()

Free 将Buffer放入缓存中

func (*Buffer) Grow

func (b *Buffer) Grow(n int)

func (*Buffer) Last

func (b *Buffer) Last() byte

Last 返回最后一个字符

func (*Buffer) Len

func (b *Buffer) Len() int

Len 返回缓存大小

func (*Buffer) Put

func (b *Buffer) Put(min int, max int, data string)

Put 根据宽度限制写入字符串,min<0代表左对齐,>0代表右对齐,max代表输出的最大字符宽度

func (*Buffer) Putf

func (b *Buffer) Putf(min int, max int, format string, args ...interface{})

Putf 格式化字符串并调用Put

func (*Buffer) String

func (b *Buffer) String() string

String 转换为字符串

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (int, error)

type Builder

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

Builder 可通过With等方法构建Logger

func WithCtx

func WithCtx(ctx context.Context) *Builder

func WithFields

func WithFields(fields ...Field) *Builder

func (*Builder) Debug

func (b *Builder) Debug(msg string, fields ...Field)

func (*Builder) Debugf

func (b *Builder) Debugf(format string, args ...interface{})

func (*Builder) Error

func (b *Builder) Error(msg string, fields ...Field)

func (*Builder) Errorf

func (b *Builder) Errorf(format string, args ...interface{})

func (*Builder) Fatal

func (b *Builder) Fatal(msg string, fields ...Field)

func (*Builder) Fatalf

func (b *Builder) Fatalf(format string, args ...interface{})

func (*Builder) Info

func (b *Builder) Info(msg string, fields ...Field)

func (*Builder) Infof

func (b *Builder) Infof(format string, args ...interface{})

func (*Builder) Log

func (b *Builder) Log(lv Level, text string, fields ...Field)

func (*Builder) Logf

func (b *Builder) Logf(lv Level, format string, args ...interface{})

func (*Builder) Trace

func (b *Builder) Trace(msg string, fields ...Field)

func (*Builder) Tracef

func (b *Builder) Tracef(format string, args ...interface{})

func (*Builder) Tracew

func (b *Builder) Tracew(format string, args ...interface{})

func (*Builder) Warn

func (b *Builder) Warn(msg string, fields ...Field)

func (*Builder) Warnf

func (b *Builder) Warnf(format string, args ...interface{})

func (*Builder) WithCtx

func (b *Builder) WithCtx(ctx context.Context) *Builder

func (*Builder) WithDepth

func (b *Builder) WithDepth(depth int) *Builder

func (*Builder) WithFields

func (b *Builder) WithFields(fields ...Field) *Builder

type Channel

type Channel interface {
	IsEnable(lv Level) bool
	Level() Level
	SetLevel(lv Level)
	Name() string
	Open() error
	Close() error
	Write(msg *Entry)
}

Channel 代表日志输出通路

func NewAsyncChannel

func NewAsyncChannel(channels []Channel, logMax int) Channel

NewAsyncChannel 创建异步队列

func NewConsoleChannel

func NewConsoleChannel(opts ...ChannelOption) Channel

NewConsoleChannel 创建控制台输出Channel

func NewElasticChannel

func NewElasticChannel(opts ...ChannelOption) Channel

NewElasticChannel ...

func NewFileChannel

func NewFileChannel(opts ...ChannelOption) Channel

NewFileChannel ...

func NewGraylogChannel

func NewGraylogChannel(opts ...ChannelOption) Channel

NewGraylogChannel ...

type ChannelOption

type ChannelOption func(o *ChannelOptions)

func WithBatch

func WithBatch(b int) ChannelOption

func WithCompress

func WithCompress(ct CompressType, level int) ChannelOption

func WithCompressLevel

func WithCompressLevel(level int) ChannelOption

func WithCompressType

func WithCompressType(ct CompressType) ChannelOption

func WithFormatter

func WithFormatter(f Formatter) ChannelOption

func WithHttpClient

func WithHttpClient(c *http.Client) ChannelOption

func WithIndexName

func WithIndexName(index string) ChannelOption

func WithLayout

func WithLayout(l string) ChannelOption

func WithLevel

func WithLevel(lv Level) ChannelOption

func WithLocalIP

func WithLocalIP(ip string) ChannelOption

func WithRetry

func WithRetry(retry int) ChannelOption

func WithURL

func WithURL(url string) ChannelOption

type ChannelOptions

type ChannelOptions struct {
	Level         Level        // 日志级别
	Layout        string       // Text Format输出格式
	Formatter     Formatter    // 输出格式
	File          string       // 文件输出路径
	URL           string       // 连接用URL,支持scheme为tcp或udp
	LocalIP       string       // 本地地址
	CompressLevel int          // 压缩级别
	CompressType  CompressType // 压缩类型
	HttpClient    *http.Client // elastic使用http协议
	Retry         int          // 重试次数
	Batch         int          // 一次发送大小
	IndexName     string       // elastic索引名
}

ChannelOptions Channel常见可选配置

func NewChannelOptions

func NewChannelOptions(opts ...ChannelOption) *ChannelOptions

NewChannelOptions ...

type Color

type Color uint8

Color represents a text color.

const (
	Black Color = iota + 30
	Red
	Green
	Yellow
	Blue
	Magenta // 品红
	Cyan    // 青色
	White
)

Foreground colors.

type CompressType

type CompressType int
const (
	// CompressNone .
	CompressNone CompressType = iota
	// CompressGzip .
	CompressGzip
	// CompressZlib .
	CompressZlib
)

type Config

type Config struct {
	Channels      []Channel // 日志输出通路,至少1个,默认Console
	Filters       []Filter  // 过滤函数
	Tags          SortedMap // 全局Fields,比如env,cluster,psm,host等
	Level         Level     // 日志级别,默认Info
	LogMax        int       // 最大缓存日志数
	DisableCaller bool      // 是否关闭Caller,若为true则获取不到文件名等信息
	Async         bool      // 是否异步,默认同步
}

Config 配置信息

func NewConfig

func NewConfig() *Config

NewConfig 创建配置

func (*Config) AddChannels

func (c *Config) AddChannels(channels ...Channel)

func (*Config) AddTags

func (c *Config) AddTags(tags map[string]string)

AddTags 添加Tags

type DateFormat

type DateFormat struct {
	Loc       *time.Location
	StdLayout string    // 标准的格式
	Tokens    []dfToken // yyyy-MM这种格式
}

https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings yyyy-MM-ddTHH:mm:ss;GMT+8

func NewDateFormat

func NewDateFormat(layout string) (*DateFormat, error)

NewDateFormat 创建DateFormat

func (*DateFormat) Format

func (d *DateFormat) Format(t time.Time) string

func (*DateFormat) Parse

func (d *DateFormat) Parse(layout string) error

type Entry

type Entry struct {
	sync.RWMutex
	Logger    Logger          // 日志Owner
	Level     Level           // 日志级别
	Text      string          // 日志信息
	Tags      SortedMap       // Tags,初始化时设置的标签信息,比如env,host等
	Fields    []Field         // 附加字段,无序,k=v格式整体输出
	Time      time.Time       // 时间戳
	Context   context.Context // 上下文,通常用于填充Fields
	Host      string          // 配置host
	Path      string          // 文件全路径,包含文件名
	File      string          // 文件名
	Line      int             // 行号
	Method    string          // 方法名
	CallDepth int             // 需要忽略的堆栈
	// contains filtered or unexported fields
}

Entry 一条输出日志

func NewEntry

func NewEntry(logger Logger) *Entry

NewEntry 创建Entry

func (*Entry) Free

func (e *Entry) Free()

Free 当引用计数为0

func (*Entry) Obtain

func (e *Entry) Obtain()

Obtain 增加引用计数

type Field

type Field struct {
	Key    string
	Type   FieldType
	Int    int64
	String string
	Value  interface{}
}

func Any

func Any(key string, value interface{}) Field

func Bool

func Bool(key string, val bool) Field

func Byte

func Byte(key string, val byte) Field

func Float32

func Float32(key string, val float32) Field

func Float64

func Float64(key string, val float64) Field

func Int

func Int(key string, val int) Field

func Int16

func Int16(key string, val int16) Field

func Int32

func Int32(key string, val int32) Field

func Int64

func Int64(key string, val int64) Field

func Int8

func Int8(key string, val int8) Field

func String

func String(key string, val string) Field

func Uint

func Uint(key string, val uint) Field

func Uint16

func Uint16(key string, val uint16) Field

func Uint32

func Uint32(key string, val uint32) Field

func Uint64

func Uint64(key string, val uint64) Field

func Uint8

func Uint8(key string, val uint8) Field

func (*Field) AppendValueToBuffer

func (f *Field) AppendValueToBuffer(b *Buffer)

type FieldType

type FieldType uint8
const (
	FieldTypeUnknown FieldType = iota
	FieldTypeAny
	FieldTypeString
	FieldTypeByte
	FieldTypeBool
	FieldTypeInt
	FieldTypeUint
	FieldTypeFloat64
	FieldTypeFloat32
)

type Filter

type Filter func(*Entry) error

Filter 在每天日志写入Channel前统一预处理,若返回错误则忽略该条日志 可用于通过Context添加Field,对某些Field加密等处理

type Formatter

type Formatter interface {
	Name() string                      //
	Format(msg *Entry) ([]byte, error) // 格式化输出
}

Formatter 用于格式化Entry 相同的Formatter对每个Entry只会格式化一次

func MustNewJsonFormatter

func MustNewJsonFormatter(layout string) Formatter

MustNewJsonFormatter ...

func MustNewTextFormatter

func MustNewTextFormatter(layout string) Formatter

MustNewTextFormatter 通过Layout创建Formatter,若失败则抛出异常

func NewJsonFormatter

func NewJsonFormatter(layout string) (Formatter, error)

NewJsonForamtter 通过Layout创建Json Formatter

func NewTextFormatter

func NewTextFormatter(layout string) (Formatter, error)

NewTextFormatter 通过Layout创建Formatter

type Iter

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

Iter Queue迭代器 使用方式类似java iter := q.Iterator()

for iter.HasNext() {
	entry := iter.Next()
 process entry
}

func (*Iter) HasNext

func (i *Iter) HasNext() bool

func (*Iter) Next

func (i *Iter) Next() *Entry

type JsonEncoder

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

JsonEncoder 简单的Json编码器,通常只需要一级 TODO:支持多级

func NewJsonEncoder

func NewJsonEncoder() JsonEncoder

NewJsonEncoder 创建json编码器

func (*JsonEncoder) AddBool

func (enc *JsonEncoder) AddBool(key string, val bool)

func (*JsonEncoder) AddComplex128

func (enc *JsonEncoder) AddComplex128(key string, val complex128)

func (*JsonEncoder) AddField

func (enc *JsonEncoder) AddField(f *Field, fn func(string) string)

func (*JsonEncoder) AddFloat32

func (enc *JsonEncoder) AddFloat32(key string, val float32)

func (*JsonEncoder) AddFloat64

func (enc *JsonEncoder) AddFloat64(key string, val float64)

func (*JsonEncoder) AddInt

func (enc *JsonEncoder) AddInt(key string, val int64)

func (*JsonEncoder) AddString

func (enc *JsonEncoder) AddString(key string, val string)

func (*JsonEncoder) AddUint

func (enc *JsonEncoder) AddUint(key string, val uint64)

func (*JsonEncoder) AddValidString

func (enc *JsonEncoder) AddValidString(key string, val string)

AddValidString 添加非空字符串

func (*JsonEncoder) Begin

func (enc *JsonEncoder) Begin()

func (*JsonEncoder) Bytes

func (enc *JsonEncoder) Bytes() []byte

func (*JsonEncoder) End

func (enc *JsonEncoder) End()

type KV

type KV struct {
	Key   string
	Value string
}

type Layout

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

https://wiki.jikexueyuan.com/project/log4j/log4j-patternlayout.html 实现类似log4j的PatternLayout格式,比如 %r [%t] %p %c %x - %m%n TODO: 支持配色 https://blog.csdn.net/qq_40147863/article/details/88880053

func NewLayout

func NewLayout(format string) (*Layout, error)

NewLayout 创建Layout

func (*Layout) Format

func (l *Layout) Format(e *Entry) []byte

func (*Layout) Parse

func (l *Layout) Parse(format string) error

type Level

type Level int8

Level 日志级别

const (
	PanicLevel Level = iota
	FatalLevel
	ErrorLevel
	WarnLevel
	InfoLevel
	DebugLevel
	TraceLevel
)

func (Level) String

func (l Level) String() string

String returns a ASCII representation of the log level.

func (Level) ToSyslogLevel

func (l Level) ToSyslogLevel() SyslogLevel

type Logger

type Logger interface {
	IsEnable(lv Level) bool
	SetLevel(name string, lv Level)
	Start()
	Stop()
	Write(e *Entry)
	Log(lv Level, msg string, fields ...Field)
	Logf(lv Level, format string, args ...interface{})
}

Logger 日志系统接口(structured, leveled logging) 1:支持同步模式和异步模式

在Debug模式下,通常使用同步模式,因为可以保证console与fmt顺序一致
正式环境下可以使用异步模式,保证日志不会影响服务质量,不能保证日志不丢失

2:配置信息

大部分配置信息是不能动态更新的,异步处理时并没有枷锁,比如Channel,Filter,Tags
部分简单配置是可以动态更新的,比如Level降级,可用于临时调试

3:关于Context

通过Context可以透传RequestID,LogID,UID等信息,Log第一个参数都强制要求传入Ctx,但可以为nil
Logger本身并不知道如何处理Ctx,因此需要初始化时手动添加Filter用来解析Context

func NewDefault

func NewDefault() Logger

NewDefault 创建默认的Logger,默认只包含Console的输出通路

func NewLogger

func NewLogger(config *Config) Logger

NewLogger 创建默认的Logger

type Node

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

func NewNode

func NewNode(e *Entry) *Node

NewNode 创建Node,使用完需要Free

func (*Node) Free

func (n *Node) Free()

type Queue

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

Queue 单向非循环队列,非线程安全

func (*Queue) Clear

func (q *Queue) Clear()

func (*Queue) Empty

func (q *Queue) Empty() bool

func (*Queue) Iterator

func (q *Queue) Iterator() Iter

func (*Queue) Len

func (q *Queue) Len() int

func (*Queue) Pop

func (q *Queue) Pop() *Entry

func (*Queue) Push

func (q *Queue) Push(e *Entry)

type Sampler

type Sampler interface {
	Check(msg *Entry) bool
}

Sampler 日志采样,对于高频的日志可以限制发送频率

type SortedMap

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

SortedMap 有序的kv结构,按照key排序,保证map的排序稳定,查询时使用二分查找 通常用于Tag列表

func (*SortedMap) Empty

func (m *SortedMap) Empty() bool

func (*SortedMap) Fill

func (m *SortedMap) Fill(dict map[string]string)

Fill 构建map

func (*SortedMap) Get

func (m *SortedMap) Get(k string) (string, bool)

Get 通过Key查询Value

func (*SortedMap) GetAt

func (m *SortedMap) GetAt(index int) (string, string)

func (*SortedMap) GetKey

func (m *SortedMap) GetKey(idx int) string

func (*SortedMap) GetValue

func (m *SortedMap) GetValue(idx int) string

func (*SortedMap) Len

func (m *SortedMap) Len() int

type SyslogLevel

type SyslogLevel int8

SyslogLevel syslog日志级别

const (
	SLEmergency SyslogLevel = iota
	SLAlert
	SLCritical
	SLError
	SLWarning
	SLNotice
	SLInformational
	SLDebug
)

Jump to

Keyboard shortcuts

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