logs

package module
v6.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2023 License: MIT Imports: 18 Imported by: 2

README

logs Go PkgGoDev Go version codecov

高性能日志库

goos: darwin
goarch: amd64
pkg: github.com/imkira/go-loggers-bench
cpu: Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
BenchmarkLogsTextPositive-4   	100000000	       320.9 ns/op	      40 B/op	       2 allocs/op
BenchmarkLogsTextNegative-4   	1000000000	         9.407 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogsJSONNegative-4   	1000000000	        11.42 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogsJSONPositive-4   	65887180	       578.6 ns/op	      40 B/op	       2 allocs/op
import "github.com/issue9/logs/v6"

l := logs.New(logs.NewTextHandler(...))
l.DEBUG().Print("debug start...")

erro := l.With(logs.LevelError, map[string]interface{}{"k1":"v1"})
erro.Printf("带默认参数 k1=v1") // 不用 With 指定 k1,err 全都自动带上此参数

安装

go get github.com/issue9/logs/v6

版权

本项目采用 MIT 开源授权许可证,完整的授权说明可在 LICENSE 文件中找到。

Documentation

Overview

Package logs 日志系统

格式

提供了 Handler 接口用于处理输出的日志格式,用户可以自己实现, 系统也提供了几种常用的供用户选择。

Logger

Logger 为实际的日志输出接口,提供多种 Logger 的实现。

  • Logs.ERROR 等为普通的日志对象;
  • Logs.With 返回的是带固定参数的日志对象;

Index

Constants

View Source
const (
	DateMilliLayout = "2006-01-02T15:04:05.000"
	DateMicroLayout = "2006-01-02T15:04:05.000000"
	DateNanoLayout  = "2006-01-02T15:04:05.000000000"

	MilliLayout = "15:04:05.000"
	MicroLayout = "15:04:05.000000"
	NanoLayout  = "15:04:05.000000000"
)

常用的日志时间格式

Variables

This section is empty.

Functions

func IsValidLevel

func IsValidLevel(l Level) bool

Types

type AppendFunc

type AppendFunc = func(*Buffer)

type Buffer

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

Buffer []byte 复用对象池

同时实现了 xerrors.Printer 接口。

func NewBuffer

func NewBuffer(detail bool) *Buffer

NewBuffer 声明 Buffer 对象

detail 是否打印错误信息的调用堆栈;

func (*Buffer) Append

func (w *Buffer) Append(v ...any) *Buffer

func (*Buffer) AppendBuffer

func (w *Buffer) AppendBuffer(f func(b *Buffer)) *Buffer

func (*Buffer) AppendBytes

func (w *Buffer) AppendBytes(b ...byte) *Buffer

func (*Buffer) AppendFloat

func (w *Buffer) AppendFloat(n float64, fmt byte, prec, bitSize int) *Buffer

func (*Buffer) AppendFunc

func (w *Buffer) AppendFunc(f AppendFunc) *Buffer

func (*Buffer) AppendInt

func (w *Buffer) AppendInt(n int64, base int) *Buffer

func (*Buffer) AppendString

func (w *Buffer) AppendString(s string) *Buffer

func (*Buffer) AppendTime

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

func (*Buffer) AppendUint

func (w *Buffer) AppendUint(n uint64, base int) *Buffer

func (*Buffer) Appendf

func (w *Buffer) Appendf(format string, v ...any) *Buffer

func (*Buffer) Appendln

func (w *Buffer) Appendln(v ...any) *Buffer

func (*Buffer) Bytes

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

func (*Buffer) Detail

func (w *Buffer) Detail() bool

func (*Buffer) Free

func (w *Buffer) Free()

func (*Buffer) Print

func (w *Buffer) Print(v ...any)

func (*Buffer) Printf

func (w *Buffer) Printf(f string, v ...any)

func (*Buffer) Println

func (w *Buffer) Println(v ...any)

func (*Buffer) Reset

func (w *Buffer) Reset(detail bool) *Buffer

func (*Buffer) Write

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

type Handler

type Handler interface {
	// Handle 将 [Record] 写入日志
	//
	// [Record] 中各个字段的名称由处理器自行决定。
	//
	// NOTE: 此方法应该保证输出内容是以换行符作为结尾。
	Handle(*Record)
}

Handler 日志后端的处理接口

func MergeHandler

func MergeHandler(w ...Handler) Handler

MergeHandler 将多个 Handler 合并成一个 Handler 接口对象

func NewDispatchHandler

func NewDispatchHandler(d map[Level]Handler) Handler

NewDispatchHandler 根据 Level 派发到不同的 Handler 对象

func NewJSONHandler

func NewJSONHandler(w ...io.Writer) Handler

NewJSONHandler 返回将 Record 以 JSON 的形式写入 w 的对象

NOTE: 如果向 w 输出内容时出错,会将错误信息输出到终端作为最后的处理方式。

func NewNopHandler

func NewNopHandler() Handler

NewNopHandler 空的 Handler 接口实现

func NewTermHandler

func NewTermHandler(w io.Writer, foreColors map[Level]colors.Color) Handler

NewTermHandler 返回将 Record 写入终端的对象

w 表示终端的接口,可以是 os.Stderr 或是 os.Stdout, 如果是其它的实现者则会带控制字符一起输出; foreColors 表示各类别信息的字符颜色,背景始终是默认色,未指定的颜色会从 [defaultTermColors] 获取;

NOTE: 如果向 w 输出内容时出错,将会导致 panic。

func NewTextHandler

func NewTextHandler(w ...io.Writer) Handler

NewTextHandler 返回将 Record 以普通文本的形式写入 w 的对象

NOTE: 如果向 w 输出内容时出错,会将错误信息输出到终端作为最后的处理方式。

type HandlerFunc

type HandlerFunc func(*Record)

func (HandlerFunc) Handle

func (w HandlerFunc) Handle(e *Record)

type Level

type Level int8
const (
	LevelInfo Level
	LevelTrace
	LevelDebug
	LevelWarn
	LevelError
	LevelFatal
)

目前支持的日志类型

func ParseLevel

func ParseLevel(s string) (Level, error)

func (Level) MarshalText

func (l Level) MarshalText() ([]byte, error)

func (Level) String

func (l Level) String() string

func (*Level) UnmarshalText

func (l *Level) UnmarshalText(data []byte) error

type Logger

type Logger interface {
	// With 为日志提供额外的参数
	//
	// 返回值是当前对象。
	With(name string, val any) Logger

	// Error 将一条错误信息作为一条日志输出
	//
	// 这是 Print 的特化版本,在已知类型为 error 时,
	// 采用此方法会比 Print(err) 有更好的性能。
	//
	// 如果 err 实现了 [xerrors.FormatError] 接口,同时也会打印调用信息。
	Error(err error)

	// String 将字符串作为一条日志输出
	//
	// 这是 Print 的特化版本,在已知类型为字符串时,
	// 采用此方法会比 Print(s) 有更好的性能。
	String(s string)

	// 输出一条日志信息
	Print(v ...any)
	Println(v ...any)
	Printf(format string, v ...any)

	// StdLogger 将当前对象转换成标准库的日志对象
	//
	// NOTE: 不要设置返回对象的 Prefix 和 Flag,这些配置项与当前模块的功能有重叠。
	// [log.Logger] 应该仅作为向 [Logger] 输入 [Record.Message] 内容使用。
	StdLogger() *log.Logger
}

Logger 日志接口

type Logs

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

func New

func New(h Handler, o ...Option) *Logs

New 声明 Logs 对象

h 如果为 nil,则表示采用 NewNopHandler

func (*Logs) CreatedFormat

func (logs *Logs) CreatedFormat() string

CreatedFormat created 的时间格式

如果返回空值,表示禁用在日志中显示时间信息。

func (*Logs) DEBUG

func (logs *Logs) DEBUG() Logger

func (*Logs) ERROR

func (logs *Logs) ERROR() Logger

func (*Logs) Enable

func (logs *Logs) Enable(level ...Level)

Enable 允许的日志通道

调用此函数之后,所有不在 level 参数的通道都将被关闭。

func (*Logs) FATAL

func (logs *Logs) FATAL() Logger

func (*Logs) HasLocation

func (logs *Logs) HasLocation() bool

HasLocation 是否包含定位信息

func (*Logs) INFO

func (logs *Logs) INFO() Logger

func (*Logs) IsEnable

func (logs *Logs) IsEnable(l Level) bool

func (*Logs) Logger

func (logs *Logs) Logger(lv Level) Logger

Logger 返回指定级别的日志接口

func (*Logs) NewRecord

func (logs *Logs) NewRecord(lv Level) *Record

func (*Logs) SLog

func (l *Logs) SLog() *slog.Logger

SLog 将 Logs 作为 slog.Logger 的后端

func (*Logs) SLogHandler

func (l *Logs) SLogHandler() slog.Handler

SLogHandler 将 logs 转换为 slog.Handler 接口

所有的 group 会作为普通 attr 的名称前缀,但是不影响 Level、Message 等字段。

func (*Logs) SetCreated

func (logs *Logs) SetCreated(v string)

SetCreated 指定日期的格式

如果 v 为空将会禁用日期显示。

func (*Logs) SetHandler

func (logs *Logs) SetHandler(h Handler)

func (*Logs) SetLocation

func (logs *Logs) SetLocation(v bool)

SetLocation 设置是否输出位置信息

func (*Logs) TRACE

func (logs *Logs) TRACE() Logger

func (*Logs) WARN

func (logs *Logs) WARN() Logger

func (*Logs) With

func (logs *Logs) With(lv Level, params map[string]any) Logger

With 创建带有指定参数的日志对象

params 自动添加的参数,每条日志都将自动带上这些参数;

type Option

type Option func(*Logs)

func WithCreated

func WithCreated(layout string) Option

WithCreated 指定日期的格式

如果 layout 为空将会禁用日期显示。

func WithDetail

func WithDetail(v bool) Option

WithDetail 错误信息的调用堆栈

如果向日志输出的是类型为 err 的信息,是否显示其调用堆栈。

NOTE: 该设置仅对 [Logger.Error] 方法有效果, 如果将 err 传递给 [Logger.Printf] 等方法,则遵照 fmt.Appendf 进行处理。

func WithLocale

func WithLocale(p *localeutil.Printer) Option

WithLocale 指定本地化信息

如果为 nil,那么将禁用本地化输出。

设置了此值为影响以下几个方法中实现了 localeutil.Stringer 的参数:

  • Logger.Error 中的 error 类型参数;
  • Logger.Print/Printf/Println 中的 any 类型参数;
  • Logger.With 中的 any 类型参数

func WithLocation

func WithLocation(v bool) Option

WithLocation 是否显示定位信息

type Pair

type Pair struct {
	K string
	V any
}

type Record

type Record struct {
	Level Level

	// AppendCreated 添加字符串类型的日志创建时间
	//
	// 可能为空,根据 [Logs.CreatedFormat] 是否为空决定。
	AppendCreated AppendFunc

	// AppendMessage 向日志中添加字符串类型的日志消息
	//
	// 这是每一条日志的主消息,不会为空。
	// 内容是根据 Depth* 系列方法生成的。
	AppendMessage AppendFunc

	// AppendLocation 添加字符串类型的日志触发位置信息
	//
	// 可能为空,根据 [Logs.HasLocation] 决定。
	AppendLocation AppendFunc

	// 额外的数据,比如由 [Logger.With] 添加的数据。
	Params []Pair
	// contains filtered or unexported fields
}

Record 单条日志产生的数据

func (*Record) DepthError

func (e *Record) DepthError(depth int, err error)

DepthError 输出 error 类型的内容到日志

depth 表示调用,1 表示调用此方法的位置;

如果 Logs.HasLocation 为 false,那么 depth 将不起实际作用。

func (*Record) DepthPrint

func (e *Record) DepthPrint(depth int, v ...any)

DepthPrint 输出任意类型的内容到日志

depth 表示调用,1 表示调用此方法的位置;

如果 Logs.HasLocation 为 false,那么 depth 将不起实际作用。

func (*Record) DepthPrintf

func (e *Record) DepthPrintf(depth int, format string, v ...any)

DepthPrintf 输出任意类型的内容到日志

depth 表示调用,1 表示调用此方法的位置;

如果 Logs.HasLocation 为 false,那么 depth 将不起实际作用。

func (*Record) DepthPrintln

func (e *Record) DepthPrintln(depth int, v ...any)

DepthPrintln 输出任意类型的内容到日志

depth 表示调用,1 表示调用此方法的位置;

如果 Logs.HasLocation 为 false,那么 depth 将不起实际作用。

func (*Record) DepthString

func (e *Record) DepthString(depth int, s string)

DepthString 输出字符串类型的内容到日志

depth 表示调用,1 表示调用此方法的位置;

如果 Logs.HasLocation 为 false,那么 depth 将不起实际作用。

func (*Record) Error

func (e *Record) Error(err error)

func (*Record) Logs

func (e *Record) Logs() *Logs

func (*Record) Print

func (e *Record) Print(v ...any)

func (*Record) Printf

func (e *Record) Printf(format string, v ...any)

func (*Record) Println

func (e *Record) Println(v ...any)

func (*Record) StdLogger

func (e *Record) StdLogger() *log.Logger

func (*Record) String

func (e *Record) String(s string)

func (*Record) With

func (e *Record) With(name string, val any) Logger

Directories

Path Synopsis
Package writers 提供了一组实现 io.Writer 接口的结构
Package writers 提供了一组实现 io.Writer 接口的结构
rotate
Package rotate 提供一个可以按文件大小进行分割的 io.Writer 实例
Package rotate 提供一个可以按文件大小进行分割的 io.Writer 实例

Jump to

Keyboard shortcuts

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