log

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

framework-log

Go framework package for log.

Installation

go get github.com/go-anyway/framework-log@v1.0.0

Usage

See documentation for usage examples.

License

Apache License 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithRequestID

func ContextWithRequestID(ctx context.Context, requestID string) context.Context

ContextWithRequestID 返回一个包含 requestID 的新 context.

func ContextWithTraceID

func ContextWithTraceID(ctx context.Context, traceID string) context.Context

ContextWithTraceID 返回一个包含 traceID 的新 context.

func DPanic

func DPanic(msg string, fields ...zap.Field)

DPanic 记录一条 dpanic 级别的日志. 在开发模式下会 panic.

func Debug

func Debug(msg string, fields ...zap.Field)

Debug 记录一条 debug 级别的日志.

func Error

func Error(msg string, fields ...zap.Field)

Error 记录一条 error 级别的日志.

func Fatal

func Fatal(msg string, fields ...zap.Field)

Fatal 记录一条 fatal 级别的日志,然后调用 os.Exit(1).

func FromContext

func FromContext(ctx context.Context) *zap.Logger

FromContext 从 context 中提取 traceID 和 requestID,返回一个包含这些字段的 Logger 实例。 如果上下文中没有这些值,它会返回全局的 logger。 traceID 优先从 OpenTelemetry span 中提取,如果没有则从自定义 context key 中提取。

func GetLogger

func GetLogger() *zap.Logger

GetLogger 返回当前的全局日志记录器. 这在需要传递 logger 实例而不是使用全局函数时很有用.

func GinErrorWriter

func GinErrorWriter() io.Writer

GinErrorWriter 返回一个 io.Writer,用于将 gin 的错误日志输出到 zap logger. 这个 Writer 会将所有写入的内容作为 Error 级别的日志记录.

func GinWriter

func GinWriter() io.Writer

GinWriter 返回一个 io.Writer,用于将 gin 的日志输出到 zap logger. 这个 Writer 会将所有写入的内容作为 Info 级别的日志记录.

func Info

func Info(msg string, fields ...zap.Field)

Info 记录一条 info 级别的日志.

func Init

func Init(opts ...Option)

Init 使用给定的选项初始化或重新初始化全局日志记录器. 这个函数是线程安全的.

func New

func New(opts *Options) *zap.Logger

New 根据给定的选项创建一个新的日志记录器.

func Panic

func Panic(msg string, fields ...zap.Field)

Panic 记录一条 panic 级别的日志,然后调用 panic().

func RequestIDFromContext

func RequestIDFromContext(ctx context.Context) string

RequestIDFromContext 从 context 中提取 requestID

func Sync

func Sync() error

Sync 将所有缓冲的日志条目刷新到磁盘. 应用程序在退出前应调用此方法.

func TraceIDFromContext

func TraceIDFromContext(ctx context.Context) string

TraceIDFromContext 从 context 中提取 traceID 优先从 OpenTelemetry span 中获取,如果没有则从自定义 context key 中获取

func Warn

func Warn(msg string, fields ...zap.Field)

Warn 记录一条 warn 级别的日志.

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 (c *Config) GetCompress() bool

func (*Config) GetDevelopment

func (c *Config) GetDevelopment() bool

func (*Config) GetDisableCaller

func (c *Config) GetDisableCaller() bool

func (*Config) GetDisableStacktrace

func (c *Config) GetDisableStacktrace() bool

func (*Config) GetErrorOutputPaths

func (c *Config) GetErrorOutputPaths() []string

func (*Config) GetFilename

func (c *Config) GetFilename() string

func (*Config) GetFormat

func (c *Config) GetFormat() string

func (*Config) GetLevel

func (c *Config) GetLevel() string

实现 LogConfigProvider 接口

func (*Config) GetMaxAge

func (c *Config) GetMaxAge() int

func (*Config) GetMaxBackups

func (c *Config) GetMaxBackups() int

func (*Config) GetMaxSize

func (c *Config) GetMaxSize() int

func (*Config) GetOutputPaths

func (c *Config) GetOutputPaths() []string

func (*Config) ToOptions

func (c *Config) ToOptions() *Options

ToOptions 将 Config 转换为 log.Options

func (*Config) Validate

func (c *Config) Validate() error

Validate 验证日志配置

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 WithCompress

func WithCompress(compress bool) Option

WithCompress 设置是否压缩轮转后的日志文件.

func WithDevelopment

func WithDevelopment(development bool) Option

WithDevelopment 设置是否为开发模式.

func WithDisableCaller

func WithDisableCaller(disable bool) Option

WithDisableCaller 禁止调用者信息记录.

func WithDisableStacktrace

func WithDisableStacktrace(disable bool) Option

WithDisableStacktrace 禁止堆栈跟踪.

func WithErrorOutputPaths

func WithErrorOutputPaths(paths []string) Option

WithErrorOutputPaths 设置错误日志输出路径.

func WithFilename

func WithFilename(filename string) Option

WithFilename 设置用于轮转的日志文件名.

func WithFormat

func WithFormat(format string) Option

WithFormat 设置日志格式.

func WithLevel

func WithLevel(level string) Option

WithLevel 设置日志级别. 如果提供的级别无效,将使用默认的 "info" 级别.

func WithMaxAge

func WithMaxAge(maxAge int) Option

WithMaxAge 设置轮转日志的最大保留天数.

func WithMaxBackups

func WithMaxBackups(maxBackups int) Option

WithMaxBackups 设置轮转日志的最大备份数.

func WithMaxSize

func WithMaxSize(maxSize int) Option

WithMaxSize 设置轮转日志的最大大小.

func WithOutputPaths

func WithOutputPaths(paths []string) Option

WithOutputPaths 设置日志输出路径.

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 NewOptions

func NewOptions() *Options

NewOptions 创建一个带有默认值的新 Options 对象.

func ToLogOptionsFromConfig

func ToLogOptionsFromConfig(cfg LogConfigProvider) *Options

ToLogOptionsFromConfig 从 LogConfigProvider 转换为 log.Options 这是一个通用函数,用于统一处理不同包的 LogConfig 类型

func (*Options) Apply

func (o *Options) Apply(opts ...Option)

Apply 将给定的选项应用于此 Options.

Jump to

Keyboard shortcuts

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