Documentation ¶
Overview ¶
Package logs 日志系统
格式 ¶
提供了 Handler 接口用于处理输出的日志格式,用户可以自己实现, 系统也提供了几种常用的供用户选择。
Logger ¶
Logger 为实际的日志输出接口,提供多种 Logger 的实现。
- Logs.ERROR 等为普通的日志对象;
- Logs.With 返回的是带固定参数的日志对象;
Index ¶
- Constants
- func Caller(l *Logs)
- func Created(l *Logs)
- func IsValidLevel(l Level) bool
- type HandleFunc
- type Handler
- func MergeHandler(w ...Handler) Handler
- func NewDispatchHandler(d map[Level]Handler) Handler
- func NewJSONHandler(timeLayout string, w ...io.Writer) Handler
- func NewNopHandler() Handler
- func NewTermHandler(timeLayout string, w io.Writer, foreColors map[Level]colors.Color) Handler
- func NewTextHandler(timeLayout string, w ...io.Writer) Handler
- type Level
- type Logger
- type Logs
- func (logs *Logs) DEBUG() Logger
- func (logs *Logs) ERROR() Logger
- func (logs *Logs) Enable(level ...Level)
- func (logs *Logs) FATAL() Logger
- func (logs *Logs) HasCaller() bool
- func (logs *Logs) HasCreated() 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) SetCaller(v bool)
- func (logs *Logs) SetCreated(v bool)
- func (logs *Logs) SetHandler(h Handler)
- 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 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 ¶
MergeHandler 将多个 Handler 合并成一个 Handler 接口对象
func NewDispatchHandler ¶
func NewJSONHandler ¶
NewJSONHandler 返回将 Record 以 JSON 的形式写入 w 的对象
NOTE: 如果向 w 输出内容时出错,会将错误信息输出到终端作为最后的处理方式。
func NewTermHandler ¶
NewTermHandler 返回将 Record 写入终端的对象
timeLayout 表示输出的时间格式,遵守 time.Format 的参数要求; w 表示终端的接口,可以是 os.Stderr 或是 os.Stdout, 如果是其它的实现者则会带控制字符一起输出; foreColors 表示各类别信息的字符颜色,背景始终是默认色,未指定的颜色会从 [defaultTermColors] 获取;
NOTE: 如果向 w 输出内容时出错,将会导致 panic。
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) 有更好的性能。 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) SLog ¶ added in v5.1.0
SLog 将 Logs 作为 slog.Logger 的后端
func (*Logs) SLogHandler ¶ added in v5.1.0
SLogHandler 将 logs 转换为 slog.Handler 接口
所有的 group 会作为普通 attr 的名称前缀,但是不影响 Level、Message 等字段。
func (*Logs) SetHandler ¶
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 ¶
DepthError 输出 error 类型的内容到日志
depth 表示调用,1 表示调用此方法的位置;
如果 Logs.HasCaller 为 false,那么 depth 将不起实际作用。
func (*Record) DepthPrint ¶
DepthPrint 输出任意类型的内容到日志
depth 表示调用,1 表示调用此方法的位置;
如果 Logs.HasCaller 为 false,那么 depth 将不起实际作用。
func (*Record) DepthPrintf ¶
DepthPrintf 输出任意类型的内容到日志
depth 表示调用,1 表示调用此方法的位置;
如果 Logs.HasCaller 为 false,那么 depth 将不起实际作用。
func (*Record) DepthPrintln ¶
DepthPrintln 输出任意类型的内容到日志
depth 表示调用,1 表示调用此方法的位置;
如果 Logs.HasCaller 为 false,那么 depth 将不起实际作用。
func (*Record) DepthString ¶
DepthString 输出字符串类型的内容到日志
depth 表示调用,1 表示调用此方法的位置;
如果 Logs.HasCaller 为 false,那么 depth 将不起实际作用。