Documentation ¶
Index ¶
- Constants
- Variables
- func Debug(msg string, fields ...Field)
- func Debugf(format string, args ...interface{})
- func Error(msg string, fields ...Field)
- func Errorf(format string, args ...interface{})
- func Fatal(msg string, fields ...Field)
- func Fatalf(format string, args ...interface{})
- func Info(msg string, fields ...Field)
- func Infof(format string, args ...interface{})
- func IsTerminal(fd int) bool
- func SetDefault(l Logger)
- func Trace(msg string, fields ...Field)
- func Tracef(format string, args ...interface{})
- func Warn(msg string, fields ...Field)
- func Warnf(format string, args ...interface{})
- type Action
- type BaseChannel
- type BatchChannel
- type Buffer
- func (b *Buffer) AppendBool(v bool)
- func (b *Buffer) AppendByte(v byte)
- func (b *Buffer) AppendComplex128(val complex128)
- func (b *Buffer) AppendComplex64(val complex64)
- func (b *Buffer) AppendFloat32(f float32)
- func (b *Buffer) AppendFloat64(f float64)
- func (b *Buffer) AppendInt(i int64)
- func (b *Buffer) AppendString(s string)
- func (b *Buffer) AppendTime(t time.Time, layout string)
- func (b *Buffer) AppendUint(i uint64)
- func (b *Buffer) Appendf(format string, args ...interface{})
- func (b *Buffer) At(i int) byte
- func (b *Buffer) Bytes() []byte
- func (b *Buffer) Cap() int
- func (b *Buffer) Empty() bool
- func (b *Buffer) Free()
- func (b *Buffer) Grow(n int)
- func (b *Buffer) Last() byte
- func (b *Buffer) Len() int
- func (b *Buffer) Put(min int, max int, data string)
- func (b *Buffer) Putf(min int, max int, format string, args ...interface{})
- func (b *Buffer) String() string
- func (b *Buffer) Write(p []byte) (int, error)
- type Builder
- func (b *Builder) Debug(msg string, fields ...Field)
- func (b *Builder) Debugf(format string, args ...interface{})
- func (b *Builder) Error(msg string, fields ...Field)
- func (b *Builder) Errorf(format string, args ...interface{})
- func (b *Builder) Fatal(msg string, fields ...Field)
- func (b *Builder) Fatalf(format string, args ...interface{})
- func (b *Builder) Info(msg string, fields ...Field)
- func (b *Builder) Infof(format string, args ...interface{})
- func (b *Builder) Log(lv Level, text string, fields ...Field)
- func (b *Builder) Logf(lv Level, format string, args ...interface{})
- func (b *Builder) Trace(msg string, fields ...Field)
- func (b *Builder) Tracef(format string, args ...interface{})
- func (b *Builder) Tracew(format string, args ...interface{})
- func (b *Builder) Warn(msg string, fields ...Field)
- func (b *Builder) Warnf(format string, args ...interface{})
- func (b *Builder) WithCtx(ctx context.Context) *Builder
- func (b *Builder) WithDepth(depth int) *Builder
- func (b *Builder) WithFields(fields ...Field) *Builder
- type Channel
- type ChannelOption
- func WithBatch(b int) ChannelOption
- func WithCompress(ct CompressType, level int) ChannelOption
- func WithCompressLevel(level int) ChannelOption
- func WithCompressType(ct CompressType) ChannelOption
- func WithFormatter(f Formatter) ChannelOption
- func WithHttpClient(c *http.Client) ChannelOption
- func WithIndexName(index string) ChannelOption
- func WithLayout(l string) ChannelOption
- func WithLevel(lv Level) ChannelOption
- func WithLocalIP(ip string) ChannelOption
- func WithRetry(retry int) ChannelOption
- func WithURL(url string) ChannelOption
- type ChannelOptions
- type Color
- type CompressType
- type Config
- type DateFormat
- type Entry
- type Field
- func Any(key string, value interface{}) Field
- func Bool(key string, val bool) Field
- func Byte(key string, val byte) Field
- func Float32(key string, val float32) Field
- func Float64(key string, val float64) Field
- func Int(key string, val int) Field
- func Int16(key string, val int16) Field
- func Int32(key string, val int32) Field
- func Int64(key string, val int64) Field
- func Int8(key string, val int8) Field
- func String(key string, val string) Field
- func Uint(key string, val uint) Field
- func Uint16(key string, val uint16) Field
- func Uint32(key string, val uint32) Field
- func Uint64(key string, val uint64) Field
- func Uint8(key string, val uint8) Field
- type FieldType
- type Filter
- type Formatter
- type Iter
- type JsonEncoder
- func (enc *JsonEncoder) AddBool(key string, val bool)
- func (enc *JsonEncoder) AddComplex128(key string, val complex128)
- func (enc *JsonEncoder) AddField(f *Field, fn func(string) string)
- func (enc *JsonEncoder) AddFloat32(key string, val float32)
- func (enc *JsonEncoder) AddFloat64(key string, val float64)
- func (enc *JsonEncoder) AddInt(key string, val int64)
- func (enc *JsonEncoder) AddString(key string, val string)
- func (enc *JsonEncoder) AddUint(key string, val uint64)
- func (enc *JsonEncoder) AddValidString(key string, val string)
- func (enc *JsonEncoder) Begin()
- func (enc *JsonEncoder) Bytes() []byte
- func (enc *JsonEncoder) End()
- type KV
- type Layout
- type Level
- type Logger
- type Node
- type Queue
- type Sampler
- type SortedMap
- func (m *SortedMap) Empty() bool
- func (m *SortedMap) Fill(dict map[string]string)
- func (m *SortedMap) Get(k string) (string, bool)
- func (m *SortedMap) GetAt(index int) (string, string)
- func (m *SortedMap) GetKey(idx int) string
- func (m *SortedMap) GetValue(idx int) string
- func (m *SortedMap) Len() int
- type SyslogLevel
Constants ¶
const ( DefaultMax = 10000 // 默认日志最大条数 DefaultCallDepth = 3 // 堆栈深度,忽略log相关的堆栈 )
Variables ¶
var (
DefaultFormatter = MustNewTextFormatter(defaultTextLayout)
)
var (
ErrNotReady = fmt.Errorf("channel not ready")
)
Functions ¶
func IsTerminal ¶
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 ¶
BatchChannel 以batch形式发送,有需要的可以
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer ...
func (*Buffer) AppendBool ¶
func (*Buffer) AppendByte ¶
func (*Buffer) AppendComplex128 ¶
func (b *Buffer) AppendComplex128(val complex128)
func (*Buffer) AppendComplex64 ¶
func (*Buffer) AppendFloat32 ¶
func (*Buffer) AppendFloat64 ¶
func (*Buffer) AppendString ¶
func (*Buffer) AppendUint ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder 可通过With等方法构建Logger
func WithFields ¶
func (*Builder) WithFields ¶
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 ¶
NewAsyncChannel 创建异步队列
func NewConsoleChannel ¶
func NewConsoleChannel(opts ...ChannelOption) Channel
NewConsoleChannel 创建控制台输出Channel
func NewElasticChannel ¶
func NewElasticChannel(opts ...ChannelOption) Channel
NewElasticChannel ...
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 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 (*Config) AddChannels ¶
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) 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 一条输出日志
type Field ¶
func (*Field) AppendValueToBuffer ¶
type Formatter ¶
Formatter 用于格式化Entry 相同的Formatter对每个Entry只会格式化一次
func MustNewJsonFormatter ¶
MustNewJsonFormatter ...
func MustNewTextFormatter ¶
MustNewTextFormatter 通过Layout创建Formatter,若失败则抛出异常
func NewJsonFormatter ¶
NewJsonForamtter 通过Layout创建Json Formatter
func NewTextFormatter ¶
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 }
type JsonEncoder ¶
type JsonEncoder struct {
// contains filtered or unexported fields
}
JsonEncoder 简单的Json编码器,通常只需要一级 TODO:支持多级
func (*JsonEncoder) AddBool ¶
func (enc *JsonEncoder) AddBool(key string, val bool)
func (*JsonEncoder) AddComplex128 ¶
func (enc *JsonEncoder) AddComplex128(key string, val complex128)
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 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
type Level ¶
type Level int8
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
type SortedMap ¶
type SortedMap struct {
// contains filtered or unexported fields
}
SortedMap 有序的kv结构,按照key排序,保证map的排序稳定,查询时使用二分查找 通常用于Tag列表
type SyslogLevel ¶
type SyslogLevel int8
SyslogLevel syslog日志级别
const ( SLEmergency SyslogLevel = iota SLAlert SLCritical SLError SLWarning SLNotice SLInformational SLDebug )
Source Files ¶
- buffer.go
- channel_async.go
- channel_base.go
- channel_console.go
- channel_elastic.go
- channel_file.go
- channel_graylog.go
- channel_options.go
- entry.go
- field.go
- formatter_json.go
- formatter_text.go
- json_encoder.go
- layout.go
- layout_date.go
- level.go
- logger.go
- logger_builder.go
- logging.go
- os.go
- queue.go
- sorted_map.go
- utils.go