Documentation ¶
Overview ¶
Package logs 日志系统
格式 ¶
提供了 Handler 接口用于处理输出的日志格式,用户可以自己实现, 系统也提供了几种常用的供用户选择。
Logger ¶
Logger 为实际的日志输出接口,提供多种 Logger 的实现。
- Logs.ERROR 等为普通的日志对象;
- Logs.With 返回的是带固定参数的日志对象;
Index ¶
- Constants
- func IsValidLevel(l Level) bool
- type AppendFunc
- type Buffer
- func (w *Buffer) Append(v ...any) *Buffer
- func (w *Buffer) AppendBuffer(f func(b *Buffer)) *Buffer
- func (w *Buffer) AppendBytes(b ...byte) *Buffer
- func (w *Buffer) AppendFloat(n float64, fmt byte, prec, bitSize int) *Buffer
- func (w *Buffer) AppendFunc(f AppendFunc) *Buffer
- func (w *Buffer) AppendInt(n int64, base int) *Buffer
- func (w *Buffer) AppendString(s string) *Buffer
- func (w *Buffer) AppendTime(t time.Time, layout string) *Buffer
- func (w *Buffer) AppendUint(n uint64, base int) *Buffer
- func (w *Buffer) Appendf(format string, v ...any) *Buffer
- func (w *Buffer) Appendln(v ...any) *Buffer
- func (w *Buffer) Bytes() []byte
- func (w *Buffer) Detail() bool
- func (w *Buffer) Free()
- func (w *Buffer) Print(v ...any)
- func (w *Buffer) Printf(f string, v ...any)
- func (w *Buffer) Println(v ...any)
- func (w *Buffer) Reset(detail bool) *Buffer
- func (w *Buffer) Write(b []byte) (int, error)
- type Handler
- type HandlerFunc
- type Level
- type Logger
- type Logs
- func (logs *Logs) CreatedFormat() string
- func (logs *Logs) DEBUG() Logger
- func (logs *Logs) ERROR() Logger
- func (logs *Logs) Enable(level ...Level)
- func (logs *Logs) FATAL() Logger
- func (logs *Logs) HasLocation() bool
- func (logs *Logs) INFO() Logger
- func (logs *Logs) IsEnable(l Level) bool
- func (logs *Logs) Logger(lv Level) Logger
- func (logs *Logs) NewRecord(lv Level) *Record
- func (l *Logs) SLog() *slog.Logger
- func (l *Logs) SLogHandler() slog.Handler
- func (logs *Logs) SetCreated(v string)
- func (logs *Logs) SetHandler(h Handler)
- func (logs *Logs) SetLocation(v bool)
- func (logs *Logs) TRACE() Logger
- func (logs *Logs) WARN() Logger
- func (logs *Logs) With(lv Level, params map[string]any) Logger
- type Option
- type Pair
- type Record
- func (e *Record) DepthError(depth int, err error)
- func (e *Record) DepthPrint(depth int, v ...any)
- func (e *Record) DepthPrintf(depth int, format string, v ...any)
- func (e *Record) DepthPrintln(depth int, v ...any)
- func (e *Record) DepthString(depth int, s string)
- func (e *Record) Error(err error)
- func (e *Record) Logs() *Logs
- func (e *Record) Print(v ...any)
- func (e *Record) Printf(format string, v ...any)
- func (e *Record) Println(v ...any)
- func (e *Record) StdLogger() *log.Logger
- func (e *Record) String(s string)
- func (e *Record) With(name string, val any) Logger
Constants ¶
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 ¶
Types ¶
type AppendFunc ¶
type AppendFunc = func(*Buffer)
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer []byte 复用对象池
同时实现了 xerrors.Printer 接口。
func (*Buffer) AppendBuffer ¶
func (*Buffer) AppendBytes ¶
func (*Buffer) AppendFloat ¶
func (*Buffer) AppendFunc ¶
func (w *Buffer) AppendFunc(f AppendFunc) *Buffer
func (*Buffer) AppendString ¶
type Handler ¶
type Handler interface { // Handle 将 [Record] 写入日志 // // [Record] 中各个字段的名称由处理器自行决定。 // // NOTE: 此方法应该保证输出内容是以换行符作为结尾。 Handle(*Record) }
Handler 日志后端的处理接口
func MergeHandler ¶
MergeHandler 将多个 Handler 合并成一个 Handler 接口对象
func NewDispatchHandler ¶
func NewJSONHandler ¶
NewJSONHandler 返回将 Record 以 JSON 的形式写入 w 的对象
NOTE: 如果向 w 输出内容时出错,会将错误信息输出到终端作为最后的处理方式。
func NewTermHandler ¶
NewTermHandler 返回将 Record 写入终端的对象
w 表示终端的接口,可以是 os.Stderr 或是 os.Stdout, 如果是其它的实现者则会带控制字符一起输出; foreColors 表示各类别信息的字符颜色,背景始终是默认色,未指定的颜色会从 [defaultTermColors] 获取;
NOTE: 如果向 w 输出内容时出错,将会导致 panic。
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 (Level) MarshalText ¶
func (*Level) UnmarshalText ¶
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 (*Logs) SLogHandler ¶
SLogHandler 将 logs 转换为 slog.Handler 接口
所有的 group 会作为普通 attr 的名称前缀,但是不影响 Level、Message 等字段。
func (*Logs) SetHandler ¶
type Option ¶
type Option func(*Logs)
func WithDetail ¶
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 类型参数
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 ¶
DepthError 输出 error 类型的内容到日志
depth 表示调用,1 表示调用此方法的位置;
如果 Logs.HasLocation 为 false,那么 depth 将不起实际作用。
func (*Record) DepthPrint ¶
DepthPrint 输出任意类型的内容到日志
depth 表示调用,1 表示调用此方法的位置;
如果 Logs.HasLocation 为 false,那么 depth 将不起实际作用。
func (*Record) DepthPrintf ¶
DepthPrintf 输出任意类型的内容到日志
depth 表示调用,1 表示调用此方法的位置;
如果 Logs.HasLocation 为 false,那么 depth 将不起实际作用。
func (*Record) DepthPrintln ¶
DepthPrintln 输出任意类型的内容到日志
depth 表示调用,1 表示调用此方法的位置;
如果 Logs.HasLocation 为 false,那么 depth 将不起实际作用。
func (*Record) DepthString ¶
DepthString 输出字符串类型的内容到日志
depth 表示调用,1 表示调用此方法的位置;
如果 Logs.HasLocation 为 false,那么 depth 将不起实际作用。