logs

package module
v5.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2023 License: MIT Imports: 17 Imported by: 2

README

logs Go PkgGoDev Go version codecov

高性能日志库

cpu: Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
BenchmarkLogsTextPositive     	70519216	       178.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogsTextPositive-2   	100000000	       120.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogsTextPositive-4   	81085801	       141.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogsTextNegative     	484125342	        24.35 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogsTextNegative-2   	1000000000	        14.10 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogsTextNegative-4   	1000000000	         9.033 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogsJSONNegative     	461385842	        25.81 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogsJSONNegative-2   	886811012	        13.65 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogsJSONNegative-4   	1000000000	        10.70 ns/op	       0 B/op	       0 allocs/op
BenchmarkLogsJSONPositive     	20922458	       580.0 ns/op	      40 B/op	       2 allocs/op
BenchmarkLogsJSONPositive-2   	37168568	       318.2 ns/op	      40 B/op	       2 allocs/op
BenchmarkLogsJSONPositive-4   	36571064	       315.3 ns/op	      40 B/op	       2 allocs/op
import "github.com/issue9/logs/v5"

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/v5

版权

本项目采用 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 Caller

func Caller(l *Logs)

Caller 是否显示记录的定位信息

func Created

func Created(l *Logs)

Created 是否显示记录的创建时间

func IsValidLevel

func IsValidLevel(l Level) bool

Types

type HandleFunc

type HandleFunc func(*Record)

func (HandleFunc) Handle

func (w HandleFunc) Handle(e *Record)

type Handler

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

Handler Record 的处理接口

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(timeLayout string, w ...io.Writer) Handler

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

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

func NewNopHandler

func NewNopHandler() Handler

NewNopHandler 空的 Handler 接口实现

func NewTermHandler

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

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

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

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

func NewTextHandler

func NewTextHandler(timeLayout string, w ...io.Writer) Handler

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

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

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) 有更好的性能。
	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) 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) HasCaller

func (logs *Logs) HasCaller() bool

HasCaller 是否包含定位信息

func (*Logs) HasCreated

func (logs *Logs) HasCreated() bool

HasCreated 是否包含时间信息

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 added in v5.1.0

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

SLog 将 Logs 作为 slog.Logger 的后端

func (*Logs) SLogHandler added in v5.1.0

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

SLogHandler 将 logs 转换为 slog.Handler 接口

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

func (*Logs) SetCaller

func (logs *Logs) SetCaller(v bool)

SetCaller 是否显示位置信息

func (*Logs) SetCreated

func (logs *Logs) SetCreated(v bool)

SetCreated 是否显示时间信息

func (*Logs) SetHandler

func (logs *Logs) SetHandler(h Handler)

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)

type Pair

type Pair struct {
	K string
	V any
}

type Record

type Record struct {
	Level   Level
	Created time.Time // 日志的生成时间

	// 日志的实际内容
	//
	// 如果要改变此值,请使用 Depth* 系列的方法。
	Message string

	// 以下表示日志的定位信息
	Path string
	Line int

	// 额外的数据保存在此,比如由 [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.HasCaller 为 false,那么 depth 将不起实际作用。

func (*Record) DepthPrint

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

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

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

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

func (*Record) DepthPrintf

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

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

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

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

func (*Record) DepthPrintln

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

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

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

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

func (*Record) DepthString

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

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

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

如果 Logs.HasCaller 为 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