alog

package
v0.0.0-...-012d1c6 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2021 License: Apache-2.0 Imports: 17 Imported by: 0

README

alog mean async logging system

logging系统

支持的特性

  • 多输出模板,可插件扩展(terminal,file,udp,elastic等)
  • 自定义Format(text,json)
  • 异步
  • 自定义Field

参考

Documentation

Index

Constants

View Source
const (
	// 默认最大堆积消息数,超出则丢弃
	DefaultMsgMax = 10000
)
View Source
const DefaultTextLayout = "%D [%L] [%F] %m"

Variables

View Source
var (
	ErrNotReady   = fmt.Errorf("channel not ready")
	ErrNotSupport = fmt.Errorf("not support")
)

Functions

func Debug

func Debug(args ...interface{})

func Debugf

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

func Error

func Error(args ...interface{})

func Errorf

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

func Fatal

func Fatal(args ...interface{})

func Fatalf

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

func Info

func Info(args ...interface{})

func Infof

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

func NewFormatterID

func NewFormatterID() int

func Trace

func Trace(args ...interface{})

func Tracef

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

func Warn

func Warn(args ...interface{})

func Warnf

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

Types

type BaseChannel

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

func (*BaseChannel) Close

func (c *BaseChannel) Close() error

func (*BaseChannel) GetFormatter

func (c *BaseChannel) GetFormatter() Formatter

func (*BaseChannel) GetLevel

func (c *BaseChannel) GetLevel() Level

func (*BaseChannel) GetProperty

func (c *BaseChannel) GetProperty(key string) string

func (*BaseChannel) Open

func (c *BaseChannel) Open() error

func (*BaseChannel) SetFormatter

func (c *BaseChannel) SetFormatter(formatter Formatter)

func (*BaseChannel) SetLevel

func (c *BaseChannel) SetLevel(lv Level)

func (*BaseChannel) SetLogger

func (c *BaseChannel) SetLogger(l *Logger)

func (*BaseChannel) SetProperty

func (c *BaseChannel) SetProperty(key string, value string) error

type Builder

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

func WithFields

func WithFields(fields map[string]string) *Builder

func (*Builder) Debug

func (b *Builder) Debug(args ...interface{})

func (*Builder) Debugf

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

func (*Builder) Error

func (b *Builder) Error(args ...interface{})

func (*Builder) Errorf

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

func (*Builder) Fatal

func (b *Builder) Fatal(args ...interface{})

func (*Builder) Fatalf

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

func (*Builder) Info

func (b *Builder) Info(args ...interface{})

func (*Builder) Infof

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

func (*Builder) Log

func (b *Builder) Log(lv Level, args ...interface{})

func (*Builder) Logf

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

func (*Builder) Trace

func (b *Builder) Trace(args ...interface{})

func (*Builder) Tracef

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

func (*Builder) Warn

func (b *Builder) Warn(args ...interface{})

func (*Builder) Warnf

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

func (*Builder) WithFields

func (b *Builder) WithFields(fields map[string]string) *Builder

type Channel

type Channel interface {
	Configurable
	SetLogger(l *Logger)
	Name() string
	Open() error
	Close() error
	Write(msg *Entry)
}

func NewTerminal

func NewTerminal() Channel

type Cond

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

func (*Cond) Init

func (c *Cond) Init()

func (*Cond) Lock

func (c *Cond) Lock()

func (*Cond) Signal

func (c *Cond) Signal()

func (*Cond) Unlock

func (c *Cond) Unlock()

func (*Cond) Wait

func (c *Cond) Wait()

type Configurable

type Configurable interface {
	SetLevel(lv Level)
	GetLevel() Level
	SetFormatter(formatter Formatter)
	GetFormatter() Formatter
	SetProperty(key string, value string) error
	GetProperty(key string) string
}

type DateFormat

type DateFormat struct {
	Tokens []dfToken
}

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

func (*DateFormat) Format

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

func (*DateFormat) Parse

func (d *DateFormat) Parse(layout string)

type ElasticChannel

type ElasticChannel struct {
	BaseChannel
	URL      string
	Username string
	Password string
	Timeout  time.Duration // 发送超时,默认10s
	Retry    int           // 失败重试次数,默认不重试,直接丢弃
	Bulk     int           // 用于配置一个批次发送多少条日志,默认1条
	Index    string        // 索引名,按照日期分类?
	// contains filtered or unexported fields
}

官方的API: https://github.com/elastic/go-elasticsearch 但是比较厚重,这里只需要Index https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html TODO:支持Bulk模式,需要ndjson编码 formatter编码格式必须是json格式

func (*ElasticChannel) Close

func (c *ElasticChannel) Close() error

func (*ElasticChannel) Name

func (c *ElasticChannel) Name() string

func (*ElasticChannel) Open

func (c *ElasticChannel) Open() error

func (*ElasticChannel) SetProperty

func (c *ElasticChannel) SetProperty(key string, value string) error

func (*ElasticChannel) Write

func (c *ElasticChannel) Write(msg *Entry)

处理速度可能比较慢,放到单独一个队列中处理

type Entry

type Entry struct {
	*runtime.Frame

	Time   time.Time
	Level  Level
	Text   string
	Fields map[string]string
	// contains filtered or unexported fields
}

func (*Entry) FileName

func (e *Entry) FileName() string

返回文件名,不包含名字

func (*Entry) Format

func (e *Entry) Format(f Formatter) []byte

func (*Entry) FuncLine

func (e *Entry) FuncLine() string

func (*Entry) FuncName

func (e *Entry) FuncName() string

调用方法名

func (*Entry) GetField

func (e *Entry) GetField(key string) string

func (*Entry) Reset

func (e *Entry) Reset()

重置数据,复用

func (*Entry) Source

func (e *Entry) Source(short bool) string

type FileChannel

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

文件输出,rotate strategy

func (*FileChannel) Close

func (c *FileChannel) Close() error

func (*FileChannel) Name

func (c *FileChannel) Name() string

func (*FileChannel) Open

func (c *FileChannel) Open() error

func (*FileChannel) SetProperty

func (c *FileChannel) SetProperty(key string, value string) error

func (*FileChannel) Write

func (c *FileChannel) Write(msg *Entry)

type Formatter

type Formatter interface {
	ID() int // 唯一ID,用于服用格式化结果
	Name() string
	Parse(layout string) error
	Format(entry *Entry) ([]byte, error)
}

func NewFormatter

func NewFormatter(name string) Formatter

func NewJsonFormatter

func NewJsonFormatter(layout string, indent string) (Formatter, error)

func NewTextFormatter

func NewTextFormatter(layout string) (Formatter, error)

type JsonFormatter

type JsonFormatter struct {
	Indent string
	Layout string
	// contains filtered or unexported fields
}

格式配置,例如:time=%D message=%m level=%l

func (*JsonFormatter) Format

func (f *JsonFormatter) Format(entry *Entry) ([]byte, error)

func (*JsonFormatter) ID

func (f *JsonFormatter) ID() int

func (*JsonFormatter) Name

func (f *JsonFormatter) Name() string

func (*JsonFormatter) Parse

func (f *JsonFormatter) Parse(layout string) error

type Layout

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

func NewLayout

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

func (*Layout) Format

func (l *Layout) Format(msg *Entry) string

func (*Layout) Parse

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

type Level

type Level uint8
const (
	LevelTrace Level = iota
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
	LevelFatal
	LevelOff
)

func ParseLevel

func ParseLevel(value string) (Level, error)

func (Level) String

func (l Level) String() string

type Logger

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

Logger 支持同步或者异步日志输出,默认使用同步控制台输出

func New

func New() *Logger

func (*Logger) AddChannel

func (l *Logger) AddChannel(c Channel)

func (*Logger) AddField

func (l *Logger) AddField(key, value string)

func (*Logger) Debug

func (l *Logger) Debug(args ...interface{})

func (*Logger) Debugf

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

func (*Logger) Error

func (l *Logger) Error(args ...interface{})

func (*Logger) Errorf

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

func (*Logger) Fatal

func (l *Logger) Fatal(args ...interface{})

func (*Logger) Fatalf

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

func (*Logger) Formatter

func (l *Logger) Formatter() Formatter

func (*Logger) GetField

func (l *Logger) GetField(key string) string

func (*Logger) Info

func (l *Logger) Info(args ...interface{})

func (*Logger) Infof

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

func (*Logger) Level

func (l *Logger) Level() Level

func (*Logger) Log

func (l *Logger) Log(lv Level, args ...interface{})

TODO: fmt.Sprint 会紧凑的合并到一起,期望能自动添加分隔符

func (*Logger) Logf

func (l *Logger) Logf(lv Level, format string, args ...interface{})

func (*Logger) Max

func (l *Logger) Max() int

func (*Logger) Push

func (l *Logger) Push(e *Entry)

func (*Logger) Run

func (l *Logger) Run()

func (*Logger) SetFormatter

func (l *Logger) SetFormatter(f Formatter)

func (*Logger) SetLevel

func (l *Logger) SetLevel(lv Level)

func (*Logger) SetMax

func (l *Logger) SetMax(max int)

func (*Logger) SetSync

func (l *Logger) SetSync(s bool)

func (*Logger) Start

func (l *Logger) Start()

func (*Logger) Stop

func (l *Logger) Stop()

func (*Logger) Sync

func (l *Logger) Sync() bool

func (*Logger) Trace

func (l *Logger) Trace(args ...interface{})

func (*Logger) Tracef

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

func (*Logger) Warn

func (l *Logger) Warn(args ...interface{})

func (*Logger) Warnf

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

func (*Logger) WithFields

func (l *Logger) WithFields(fields map[string]string) *Builder

func (*Logger) Write

func (l *Logger) Write(lv Level, fields map[string]string, skipFrames int, text string)

type Queue

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

func (*Queue) Empty

func (q *Queue) Empty() bool

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)

func (*Queue) Swap

func (q *Queue) Swap(o *Queue)

type TerminalChannel

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

控制台输出

func (*TerminalChannel) Name

func (c *TerminalChannel) Name() string

func (*TerminalChannel) SetProperty

func (c *TerminalChannel) SetProperty(key string, value string) error

func (*TerminalChannel) Write

func (c *TerminalChannel) Write(msg *Entry)

type TextFormatter

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

func (*TextFormatter) Format

func (f *TextFormatter) Format(entry *Entry) ([]byte, error)

func (*TextFormatter) ID

func (f *TextFormatter) ID() int

func (*TextFormatter) Name

func (f *TextFormatter) Name() string

func (*TextFormatter) Parse

func (f *TextFormatter) Parse(layout string) error

type UDPChannel

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

UDP协议

func (*UDPChannel) Close

func (c *UDPChannel) Close() error

func (*UDPChannel) Name

func (c *UDPChannel) Name() string

func (*UDPChannel) Open

func (c *UDPChannel) Open() error

func (*UDPChannel) SetProperty

func (c *UDPChannel) SetProperty(key string, value string) error

func (*UDPChannel) Write

func (c *UDPChannel) Write(msg *Entry)

Jump to

Keyboard shortcuts

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