Documentation
¶
Index ¶
- func ContextWithRequestID(ctx context.Context, requestID string) context.Context
- func ContextWithTraceID(ctx context.Context, traceID string) context.Context
- func DPanic(msg string, fields ...zap.Field)
- func Debug(msg string, fields ...zap.Field)
- func Error(msg string, fields ...zap.Field)
- func Fatal(msg string, fields ...zap.Field)
- func FromContext(ctx context.Context) *zap.Logger
- func GetLogger() *zap.Logger
- func GinErrorWriter() io.Writer
- func GinWriter() io.Writer
- func Info(msg string, fields ...zap.Field)
- func Init(opts ...Option)
- func New(opts *Options) *zap.Logger
- func Panic(msg string, fields ...zap.Field)
- func RequestIDFromContext(ctx context.Context) string
- func Sync() error
- func TraceIDFromContext(ctx context.Context) string
- func Warn(msg string, fields ...zap.Field)
- type Config
- func (c *Config) GetCompress() bool
- func (c *Config) GetDevelopment() bool
- func (c *Config) GetDisableCaller() bool
- func (c *Config) GetDisableStacktrace() bool
- func (c *Config) GetErrorOutputPaths() []string
- func (c *Config) GetFilename() string
- func (c *Config) GetFormat() string
- func (c *Config) GetLevel() string
- func (c *Config) GetMaxAge() int
- func (c *Config) GetMaxBackups() int
- func (c *Config) GetMaxSize() int
- func (c *Config) GetOutputPaths() []string
- func (c *Config) ToOptions() *Options
- func (c *Config) Validate() error
- type LogConfigProvider
- type Option
- func WithCompress(compress bool) Option
- func WithDevelopment(development bool) Option
- func WithDisableCaller(disable bool) Option
- func WithDisableStacktrace(disable bool) Option
- func WithErrorOutputPaths(paths []string) Option
- func WithFilename(filename string) Option
- func WithFormat(format string) Option
- func WithLevel(level string) Option
- func WithMaxAge(maxAge int) Option
- func WithMaxBackups(maxBackups int) Option
- func WithMaxSize(maxSize int) Option
- func WithOutputPaths(paths []string) Option
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextWithRequestID ¶
ContextWithRequestID 返回一个包含 requestID 的新 context.
func ContextWithTraceID ¶
ContextWithTraceID 返回一个包含 traceID 的新 context.
func FromContext ¶
FromContext 从 context 中提取 traceID 和 requestID,返回一个包含这些字段的 Logger 实例。 如果上下文中没有这些值,它会返回全局的 logger。 traceID 优先从 OpenTelemetry span 中提取,如果没有则从自定义 context key 中提取。
func GinErrorWriter ¶
GinErrorWriter 返回一个 io.Writer,用于将 gin 的错误日志输出到 zap logger. 这个 Writer 会将所有写入的内容作为 Error 级别的日志记录.
func GinWriter ¶
GinWriter 返回一个 io.Writer,用于将 gin 的日志输出到 zap logger. 这个 Writer 会将所有写入的内容作为 Info 级别的日志记录.
func RequestIDFromContext ¶
RequestIDFromContext 从 context 中提取 requestID
func TraceIDFromContext ¶
TraceIDFromContext 从 context 中提取 traceID 优先从 OpenTelemetry span 中获取,如果没有则从自定义 context key 中获取
Types ¶
type Config ¶
type Config struct {
Level string `yaml:"level" env:"LOG_LEVEL" default:"info"`
Format string `yaml:"format" env:"LOG_FORMAT" default:"console"`
OutputPaths []string `yaml:"output_paths" env:"LOG_OUTPUT_PATHS"`
ErrorOutputPaths []string `yaml:"error_output_paths" env:"LOG_ERROR_OUTPUT_PATHS"`
DisableCaller bool `yaml:"disable_caller" env:"LOG_DISABLE_CALLER" default:"false"`
DisableStacktrace bool `yaml:"disable_stacktrace" env:"LOG_DISABLE_STACKTRACE" default:"false"`
Filename string `yaml:"filename" env:"LOG_FILENAME"`
MaxSize int `yaml:"max_size" env:"LOG_MAX_SIZE" default:"100"`
MaxAge int `yaml:"max_age" env:"LOG_MAX_AGE" default:"7"`
MaxBackups int `yaml:"max_backups" env:"LOG_MAX_BACKUPS" default:"3"`
Compress bool `yaml:"compress" env:"LOG_COMPRESS" default:"false"`
Development bool `yaml:"development" env:"LOG_DEVELOPMENT" default:"false"`
}
Config 日志配置结构体 用于从配置文件加载日志配置
func (*Config) GetCompress ¶
func (*Config) GetDevelopment ¶
func (*Config) GetDisableCaller ¶
func (*Config) GetDisableStacktrace ¶
func (*Config) GetErrorOutputPaths ¶
func (*Config) GetFilename ¶
func (*Config) GetMaxBackups ¶
func (*Config) GetMaxSize ¶
func (*Config) GetOutputPaths ¶
type LogConfigProvider ¶
type LogConfigProvider interface {
GetLevel() string
GetFormat() string
GetOutputPaths() []string
GetErrorOutputPaths() []string
GetDisableCaller() bool
GetDisableStacktrace() bool
GetFilename() string
GetMaxSize() int
GetMaxAge() int
GetMaxBackups() int
GetCompress() bool
GetDevelopment() bool
}
LogConfigProvider 日志配置提供者接口 用于统一不同包的 LogConfig 类型转换为 log.Options
type Option ¶
type Option func(*Options)
Option 是一个将配置项应用于 Options 的函数.
func WithDisableStacktrace ¶
WithDisableStacktrace 禁止堆栈跟踪.
func WithErrorOutputPaths ¶
WithErrorOutputPaths 设置错误日志输出路径.
type Options ¶
type Options struct {
// OutputPaths 是一个输出路径的列表,可以是 stdout, stderr, 或者文件路径
// 默认为 ["stdout"].
OutputPaths []string
// ErrorOutputPaths 是一个错误日志输出路径的列表,用于记录 logger 内部的错误
// 默认为 ["stderr"].
ErrorOutputPaths []string
// Level 是最低的日志记录级别.
// 可选值: "debug", "info", "warn", "error", "dpanic", "panic", "fatal"
// 默认为 "info".
Level string
// Format 指定日志的输出格式.
// 可选值: "json", "console". 默认为 "console".
Format string
// DisableCaller 禁止在日志中记录调用者的文件名和行号.
// 默认为 false.
DisableCaller bool
// DisableStacktrace 禁止自动捕获堆栈跟踪.
// 默认情况下,在开发环境中,WarnLevel 及更高级别的日志会捕获堆栈,
// 在生产环境中,ErrorLevel 及更高级别的日志会捕获堆栈.
DisableStacktrace bool
// Filename 是要写入日志的文件名,用于日志轮转.
Filename string
// MaxSize 是日志文件在轮转之前的最大大小(以MB为单位).
MaxSize int
// MaxAge 是根据文件名中的时间戳保留旧日志文件的最大天数.
MaxAge int
// MaxBackups 是要保留的旧日志文件的最大数量.
MaxBackups int
// Compress 决定是否压缩轮转后的日志文件.
// 默认为 false.
Compress bool
// Development 是否为开发模式.
// 开发模式下会自动启用更详细的日志输出和堆栈跟踪.
// 默认为 false.
Development bool
}
Options 定义了日志记录器的配置项
func ToLogOptionsFromConfig ¶
func ToLogOptionsFromConfig(cfg LogConfigProvider) *Options
ToLogOptionsFromConfig 从 LogConfigProvider 转换为 log.Options 这是一个通用函数,用于统一处理不同包的 LogConfig 类型