logging

package
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// GinLogLevel gin 日志级别
	GinLogLevel = "warn"
	// GormLogLevel gorm 日志级别
	GormLogLevel = "warn"
)

Gin, Gorm 默认日志等级为 warn,目的是避免记录过多无关日志 开发者可根据需求自行调整,可选值:debug、info、warn、error

Variables

This section is empty.

Functions

func Debug

func Debug(ctx context.Context, msg string)

Debug 打印 debug 日志

func Debugf

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

Debugf 打印 debug 日志

func Error

func Error(ctx context.Context, msg string)

Error 打印 error 日志

func Errorf

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

Errorf 打印 error 日志

func ExtractSpanID

func ExtractSpanID(ctx context.Context) (slog.Value, bool)

ExtractSpanID 从 context 中提取 Span ID,若不存在则返回空 ref: https://github.com/samber/slog-gin/blob/4d5fc6c3f623d0fc9b0a7860894c025b1a33fe8c/middleware.go#L289

func ExtractTraceID

func ExtractTraceID(ctx context.Context) (slog.Value, bool)

ExtractTraceID 从 context 中提取 traceID,若不存在则返回空 ref: https://github.com/samber/slog-gin/blob/4d5fc6c3f623d0fc9b0a7860894c025b1a33fe8c/middleware.go#L289

func Fatal

func Fatal(msg string)

Fatal 打印 fatal 日志到标准输出并退出程序

func Fatalf

func Fatalf(format string, vars ...any)

Fatalf 打印 fatal 日志到标准输出并退出程序 Q:为什么 Fatalf 是强制使用 stderr 而非 slog.Default() ? A:调用 Fatalf 意味着程序即将退出,此时往标准输出而不是文件打日志是更合理的(避免 Pod 崩溃导致日志无法采集)

func GetLogger

func GetLogger(name string) *slog.Logger

GetLogger 获取指定 Logger

func Info

func Info(ctx context.Context, msg string)

Info 打印 info 日志

func Infof

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

Infof 打印 info 日志

func InitLogger

func InitLogger(name string, opts *Options) (err error)

InitLogger ...

func Logf

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

Logf 打印日志 ref: https://github.com/golang/go/blob/fc9f02c7aec81bcfcc95434d2529e0bb0bc03d66/src/log/slog/example_wrap_test.go#L19 注:该方法只能在 logging 包及其子包(如 logging/slogresty)中使用,不得在业务逻辑中直接使用

func Warn

func Warn(ctx context.Context, msg string)

Warn 打印 warn 日志

func Warnf

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

Warnf 打印 warn 日志

Types

type Options

type Options struct {
	// 日志级别
	Level string
	// 日志内容 Handler,支持 text 和 json
	HandlerName string
	// io.Writer, 支持 stdout、stderr、file
	WriterName string
	// Writer 配置
	WriterConfig map[string]string
}

Options Logger 配置

Directories

Path Synopsis
Package slogresty 实现 resty.Logger 接口
Package slogresty 实现 resty.Logger 接口

Jump to

Keyboard shortcuts

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