Documentation ¶
Index ¶
- Variables
- func Debug(ctx context.Context, format string, args ...any)
- func Enable(level Level) bool
- func Error(ctx context.Context, format string, args ...any)
- func Fatal(ctx context.Context, format string, args ...any)
- func Info(ctx context.Context, format string, args ...any)
- func Log(ctx context.Context, level Level, format string, args ...any)
- func Notice(ctx context.Context, format string, args ...any)
- func Panic(ctx context.Context, format string, args ...any)
- func SetDefault(l Logger)
- func Trace(ctx context.Context, format string, args ...any)
- func Warn(ctx context.Context, format string, args ...any)
- type FormatFun
- type Handler
- type Handlers
- type Level
- type LevelProvider
- type Logger
- type Option
- func WithColor() Option
- func WithFile(name string) Option
- func WithFormat(format string) Option
- func WithFormatFun(fn FormatFun) Option
- func WithJSON() Option
- func WithJson(b bool) Option
- func WithLevel(level Level) Option
- func WithLevels(levelConfig LevelProvider) Option
- func WithName(name string) Option
- func WithNoColor() Option
- func WithWriter(w io.Writer) Option
- type Record
- type SlogHandler
- func (s *SlogHandler) Enabled(_ context.Context, l slog.Level) bool
- func (s *SlogHandler) GetLogger() Logger
- func (s *SlogHandler) Handle(ctx context.Context, r slog.Record) error
- func (s *SlogHandler) SetLogger(logger Logger) *SlogHandler
- func (s *SlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (s *SlogHandler) WithGroup(name string) slog.Handler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var CtxKeyRecord ctxKeyRecord
Functions ¶
func SetDefault ¶
func SetDefault(l Logger)
Types ¶
type FormatFun ¶ added in v0.0.4
FormatFun format a log Record to string. the return string should ends with a '\n' as usual.
格式化一条日志记录. 通常, 返回的字符串应当以换行 '\n' 符结尾.
type Handler ¶
type Handler interface { // Output output the log Record. // // 输出日志. Output(Record) Enable(level Level, pc uintptr) bool }
Handler handles log record, output it to somewhere.
处理日志的接口.
func CombineHandlers ¶ added in v0.0.2
func NewHandler ¶
NewHandler create a new Handler with Info level by default.
创建一个日志处理器, 默认日志级别是 Info.
type LevelProvider ¶ added in v0.0.2
type Logger ¶
type Logger interface { With(key, value any) Logger Trace(ctx context.Context, format string, args ...any) Debug(ctx context.Context, format string, args ...any) Info(ctx context.Context, format string, args ...any) Notice(ctx context.Context, format string, args ...any) Warn(ctx context.Context, format string, args ...any) Error(ctx context.Context, format string, args ...any) Panic(ctx context.Context, format string, args ...any) Fatal(ctx context.Context, format string, args ...any) // Log 打印日志接口 // callDepth: 0=caller position Log(ctx context.Context, callDepth int, level Level, format string, args ...any) Enable(level Level) bool EnableDepth(level Level, callDepth int) bool }
type Option ¶
type Option func(*handler)
Option Handler options.
日志处理器的配置选项.
func WithFormat ¶ added in v0.0.2
WithFormat set the log format. [experimental] implements by regular expressions, performance may not be very good.
[实验性]设置日志格式. 使用正则表达式实现, 性能可能不是很好.
placeholder args describe %n or %N N/A print a newline %l or %level (-?\d+)? print the log level; the args set print width %F or %FILE N/A print the file name %File or %file %L N/A print the line number %fun N/A print the function name %P / %P[Kk][Gg] N/A print the package name %path N/A print the file path %T (date-format) print time with date-format %t ([num]?s)? print timestampt %X (key-name) print the Attr %X N/A print all Attr %Attr {KV}{prefix}{jointer}{suffix} range print Attr %K print the key %V or %Vjson print the value or json format of the value %M or %m N/A print the log message {left}%or{right} if left is empty then print right %Q or %q (str) print the quote form for str JSON format: {"ts":%t(ns),"time":%Q(%T(2006-01-02T15:04:05.000000000-07:00)),"level":%Q(%level), "pkg":%Q(%Pkg),"fun":%Q(%fun),"path":%Q(%path),"file":%Q(%F),"line":%L, %Attr{%Q(%K):%Vjson}{}{,}{,}"msg":%Q(%m)}%n String format: %T(2006-01-02T15:04:05.000-07:00) %level(-5) {%Pkg}%or{?}.{%fun}%or{?} {%path}%or{?}/{%F}%or{???}:%L %X %m%n
func WithJSON ¶ added in v0.0.2
func WithJSON() Option
WithJSON output the log as json format.
JSON 格式输出.
func WithJson ¶
WithJson is a option. output the log as json format if this option is true.
是否以 json 格式输出.
@deprecated use WithJSON instand.
func WithLevel ¶
WithLevel set the default log level. If you want set deffirent level for diffenrent package, consider use `WithLevels`
设置默认的日志输出级别. 如需为不同包设置不同的日志级别, 请参见 `WithLevels` 选项.
func WithLevels ¶
func WithLevels(levelConfig LevelProvider) Option
WithLevels set log level by package name. if this option is set, the `WithLevel` option would be ignored.
设置不同包的日志级别. 如果设置了该选项,`WithLevel` 选项将被忽略.
@param LevelProvider is a interface, which has a method called `Search`. It returns a log level for a given input package name. 参数 LevelProvider 是一个接口, 为指定的包名返回日志级别.
@see 参见 [trie.Tree](pkg/trie/tree.go) 前缀树
func WithNoColor ¶ added in v0.0.2
func WithNoColor() Option
WithNoColor disable output color.
禁用日志颜色.
type Record ¶
type Record struct { Ctx context.Context Time time.Time Level Level PC uintptr // see pkg/caller package caller.GetFrame 获取调用栈 Format string // message format Args []any // message args Attr []any // key-value pair of this log. With+ctx 上的 kv }
Record is log record.
一条具体的日志.
type SlogHandler ¶ added in v0.0.5
type SlogHandler struct {
// contains filtered or unexported fields
}
SlogHandler 实现 slog.Handler 适配 logs
import "code.gopub.tech/logs" // use logs.Default() slog.SetDefault(slog.New(logs.NewSlogHandler())) slog.Info("Hello, Log", "key", "value") // ... h := logs.NewHandler(logs.WithWriter(os.Stderr), logs.WithJSON()) logger := logs.NewLogger(h) sh := logs.NewSlogHandler().SetLogger(logger) slog.SetDefault(slog.New(sh))
Example ¶
slog.SetDefault(slog.New(NewSlogHandler())) // key=value Hello, World slog.Info("Hello, World", "key", "value") // k1=v1 group=[k2=true k3=false name=[k=v]] TextMsg slog.With("k1", "v1").WithGroup("group").With("k2", true).Info("TextMsg", "k3", false, slog.Group("name", "k", "v")) slog.SetDefault(slog.New(NewSlogHandler().SetLogger(NewLogger(NewHandler(WithWriter(os.Stderr), WithJSON()))))) // {"group":{"k2":true,"name":{"a":"b"}},"msg":"JSONMsg"} slog.Default().WithGroup("group").With("k2", true).Info("JSONMsg", slog.Group("name", "a", "b")) // {"a":"b","G":{"c":"d","H":{"e":"f"}},"msg":"msg"} slog.With("a", "b").WithGroup("G").With("c", "d").WithGroup("H").Info("msg", "e", "f") // {"a":"b","G":{"c":"d"},"msg":"msg"} slog.With("a", "b").WithGroup("G").With("c", "d").WithGroup("H").Info("msg")
Output:
func NewSlogHandler ¶ added in v0.0.5
func NewSlogHandler() *SlogHandler
NewSlogHandler 新建一个实现了 slog.Handler 的实例
func (*SlogHandler) GetLogger ¶ added in v0.0.5
func (s *SlogHandler) GetLogger() Logger
GetLogger 获取 SlogHandler 上关联的 Logger (如果已有 Attrs 也会一并返回,就像在 Logger 上调用了 With 一样)
func (*SlogHandler) SetLogger ¶ added in v0.0.5
func (s *SlogHandler) SetLogger(logger Logger) *SlogHandler
SetLogger 设置关联的 Logger