log

package module
v0.0.0-...-501ba56 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2023 License: MIT Imports: 4 Imported by: 0

README

log 日志

安装: go get git.blauwelle.com/go/crate/log

简单使用方式

package main

import (
	"context"

	"git.blauwelle.com/go/crate/log"
	"git.blauwelle.com/go/crate/log/logsdk"
	"git.blauwelle.com/go/crate/log/logsdk/logjson"
)

func main() {
	log.Logger().AddProcessor(logsdk.AllLevels, logjson.New()) // 添加日志处理器, 默认没有处理器(日志生成后会被忽略)
	log.Info(context.Background(), "hello world")              // 打印日志, Context 会被传递给日志处理器
}

log 模块包含日志处理的代码, 由 3 个包组成:

  1. logsdk: 日志实现;
  2. logjson: 控制台 JSON 日志处理器(Processor);
  3. log: 根目录, 提供全局 Logger, 把 Logger / Entry 相关的方法封装成函数.

基本概念

logsdkLogger, Entry, Processor 3 部分组成, 其中只有 Processor 是接口类型. Logger 主要负责全局的日志配置, Logger 可以通过内嵌的 Entry 值对象实现日志处理. Entry 负责保存局部的的日志配置, 比如覆盖 LoggerCaller 开关或设置日志的时间属性; Entry 生成单条日志(ReadonlyEntry), 并调用 Processor 输出日志.

使用方式

获取全局 Logger 对象

一般只用来配置全局 Logger 对象(通过 Set*AddProcessor 方法)

log.Logger()
修改 Logger 配置

Logger 提供以下方法用来修改全局配置

  • AddProcessor 新增处理器;
  • SetLevel 设置全局日志等级, 只有小于 Logger 上设置的日志等级的日志才会被生成;
  • SetCallerSkip 设置从 runtime 包获取调用信息时的 skip 参数, 为了方便使用, 0 表示调用 Logger / Entry 的日志方法处;
  • SetReportCaller 设置生成调用信息;
  • SetReportStack 设置生成调用栈;
  • SetReportStackLevel 当日志等级小于设定值时强制生成调用栈;
  • Reset 把 Logger 恢复到初始状态;
  • AddBeforeExit 新增调用 Logger.Exit 时优先执行的函数, 先增加的后执行;
  • SetExit 设置 Logger 的退出函数(Logger.Exit), 当日志等级为 LevelFatal 时调用这个函数;
日志生成

Entry 负责生成日志, Logger 通过内嵌 Entry 值对象的方式获得 Entry 的方法. Entry 提供以下方法来修改局部配置, log 包提供了对应的函数调用

  • AddCallerSkip 增加通过 runtime 获取调用信息时的 skip 值, skip 可以是负值;
  • WithField 添加1组键值对;
  • WithFields 添加键值对;
  • WithTime 设置生成的日志的时间;
  • WithReportCaller 覆盖 Logger 上的生成调用信息的设置;
  • WithReportStack 覆盖 Logger 上的生成调用栈的设置;

Entry 提供以下方法来生成日志, 所有方法的第 1 个参数都是 context.Context, Log 方法的参数包含日志等级和日志消息, 其他方法的参数是日志消息. 所有方法除了 Log 外日志等级从高到底(严重程度从低到高), 这些方法有对应的格式化方法(如 InfoInfof). log 包提供了对应的函数调用

  • Log
  • Trace
  • Debug
  • Info
  • Warn
  • Error
  • Fatal
  • Panic
其他用法

关闭日志生成, log.Logger().SetLevel(logsdk.LevelDisabled);

关闭强制生成调用栈, log.Logger().SetReportStackLevel(logsdk.LevelDisabled);

记录 panic, 在 defer v = recover() 后执行 log.Z(context.Background(), v), 其中 Z 需要是附加调用栈的等级;

mock, 实现 mock 日志处理器对生成的日志进行处理;


日志处理器

Documentation

Index

Constants

View Source
const (
	LevelPanic = logsdk.LevelPanic
	LevelFatal = logsdk.LevelFatal
	LevelError = logsdk.LevelError
	LevelWarn  = logsdk.LevelWarn
	LevelInfo  = logsdk.LevelInfo
	LevelDebug = logsdk.LevelDebug
	LevelTrace = logsdk.LevelTrace
)

Variables

This section is empty.

Functions

func AddCallerSkip

func AddCallerSkip(n int) logsdk.Entry

AddCallerSkip 增加调用 runtime.Callers 时的 skip 参数, 当通过装饰器等方式封装 Entry 导致增加调用 Entry 方法的深度时使用 AddCallerSkip 调整 skip, 直接在需要日志的地方调用 Entry 的方法时不需要 AddCallerSkip.

func Debug

func Debug(ctx context.Context, args ...any)

Debug 输出 LevelDebug 等级的日志

func Debugf

func Debugf(ctx context.Context, format string, args ...any)

Debugf 格式化输出 LevelDebug 等级的日志

func Error

func Error(ctx context.Context, args ...any)

Error 输出 LevelError 等级的日志

func Errorf

func Errorf(ctx context.Context, format string, args ...any)

Errorf 格式化输出 LevelError 等级的日志

func Exit

func Exit(code int)

Exit 调用全局 logger 的 Exit

func Fatal

func Fatal(ctx context.Context, args ...any)

Fatal 输出 LevelFatal 等级的日志并调用 Logger.Exit

func Fatalf

func Fatalf(ctx context.Context, format string, args ...any)

Fatalf 格式化输出 LevelFatal 等级的日志并调用 Logger.Exit

func Field

func Field(key string, value any) logsdk.KV

Field 返回键值对

func Info

func Info(ctx context.Context, args ...any)

Info 输出 LevelInfo 等级的日志

func Infof

func Infof(ctx context.Context, format string, args ...any)

Infof 格式化输出 LevelInfo 等级的日志

func Log

func Log(ctx context.Context, level Level, args ...any)

Log 输出日志

func Logf

func Logf(ctx context.Context, level Level, format string, args ...any)

Logf 格式化输出日志

func Logger

func Logger() *logsdk.Logger

Logger 返回全局 logger, 通常用来在程序启动时对全局 logger 进行配置, 业务代码处理日志时直接使用这个包里定义的日志函数.

func Panic

func Panic(ctx context.Context, args ...any)

Panic 输出 LevelPanic 等级的日志后执行 panic, 即使 Logger 日志等级高于 LevelPanic 也会 panic.

func Panicf

func Panicf(ctx context.Context, format string, args ...any)

Panicf 格式化输出 LevelPanic 等级的日志, 即使 Logger 日志等级高于 LevelPanic 也会 panic.

func Trace

func Trace(ctx context.Context, args ...any)

Trace 输出 LevelTrace 等级的日志

func Tracef

func Tracef(ctx context.Context, format string, args ...any)

Tracef 格式化输出 LevelTrace 等级的日志

func Warn

func Warn(ctx context.Context, args ...any)

Warn 输出 LevelWarn 等级的日志

func Warnf

func Warnf(ctx context.Context, format string, args ...any)

Warnf 格式化输出 LevelWarn 等级的日志

func WithField

func WithField(key string, value any) logsdk.Entry

WithField 增加1组键值对

func WithFields

func WithFields(fields ...logsdk.KV) logsdk.Entry

WithFields 增加键值对

func WithReportCaller

func WithReportCaller(reportCaller bool) logsdk.Entry

WithReportCaller 设置 logsdk.Entry 是否收集调用记录

func WithReportStack

func WithReportStack(reportStack bool) logsdk.Entry

WithReportStack 设置 logsdk.Entry 是否收集调用栈

func WithTime

func WithTime(t time.Time) logsdk.Entry

WithTime 设置日志的时间, Entry 默认使用调用 Log 等最终方法的时间作为日志的时间.

Types

type Level

type Level = logsdk.Level

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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