Documentation
¶
Overview ¶
Package log 提供了全局日志功能,支持多种日志后端的统一管理。
Package log 提供了一个统一的日志接口和多种日志实现。
这个包的主要特性包括:
- 支持多种日志后端(标准输出、Logrus)。
- 提供统一的日志接口。
- 支持结构化日志记录。
- 支持多个日志级别。
- 支持文件和标准输出。
- 支持函数式配置选项。
基本使用示例:
// 使用默认配置初始化日志
if err := log.InitLogger(); err != nil {
panic(err)
}
// 使用自定义配置初始化日志
if err := log.InitLogger(
log.WithLogType(log.LogTypeLogrus),
log.WithLevel(log.DebugLevel),
log.WithOutput("/var/log/app.log"),
); err != nil {
panic(err)
}
// 使用日志功能
log.Info("应用启动")
log.WithField("user", "admin").Info("用户登录")
也可以直接创建日志实例:
logger, err := log.NewLogger(
log.WithLogType(log.LogTypeStd),
log.WithLevel(log.DebugLevel),
)
if err != nil {
panic(err)
}
logger.Info("使用独立的日志实例")
更多示例请参考 example/log 目录。
Package log 提供了基于 Logrus 的日志实现。
Package log 提供了基于标准库的日志实现。
Index ¶
- func Debug(args ...interface{})
- func Debugf(format string, args ...interface{})
- func Error(args ...interface{})
- func Errorf(format string, args ...interface{})
- func Fatal(args ...interface{})
- func Fatalf(format string, args ...interface{})
- func Info(args ...interface{})
- func Infof(format string, args ...interface{})
- func InitLogger(options ...Option) error
- func SetLevel(level Level)
- func SetLogger(logger Logger)
- func Warn(args ...interface{})
- func Warnf(format string, args ...interface{})
- type Level
- type LogType
- type Logger
- type LoggerFormatType
- type LoggerOptions
- type LogrusLogger
- func (l *LogrusLogger) Debug(args ...interface{})
- func (l *LogrusLogger) Debugf(format string, args ...interface{})
- func (l *LogrusLogger) Error(args ...interface{})
- func (l *LogrusLogger) Errorf(format string, args ...interface{})
- func (l *LogrusLogger) Fatal(args ...interface{})
- func (l *LogrusLogger) Fatalf(format string, args ...interface{})
- func (l *LogrusLogger) GetLevel() Level
- func (l *LogrusLogger) Info(args ...interface{})
- func (l *LogrusLogger) Infof(format string, args ...interface{})
- func (l *LogrusLogger) SetLevel(level Level)
- func (l *LogrusLogger) Warn(args ...interface{})
- func (l *LogrusLogger) Warnf(format string, args ...interface{})
- func (l *LogrusLogger) WithField(key string, value interface{}) Logger
- func (l *LogrusLogger) WithFields(fields map[string]interface{}) Logger
- type LogrusLoggerOptions
- type LogrusOption
- func WithDirMode(mode os.FileMode) LogrusOption
- func WithFileMode(mode os.FileMode) LogrusOption
- func WithFormatter(formatter logrus.Formatter) LogrusOption
- func WithJSONFormatter(timestampFormat string, prettyPrint bool) LogrusOption
- func WithLogrusEnableRotate(enable bool) LogrusOption
- func WithLogrusLevel(level Level) LogrusOption
- func WithLogrusMaxAge(duration time.Duration) LogrusOption
- func WithLogrusRotateTime(duration time.Duration) LogrusOption
- func WithOutputPath(path string) LogrusOption
- func WithTextFormatter(timestampFormat string, fullTimestamp bool, disableColors bool) LogrusOption
- type Option
- func WithEnableRotate(enable bool) Option
- func WithFormatType(formatType LoggerFormatType) Option
- func WithLevel(level Level) Option
- func WithLogType(logType LogType) Option
- func WithMaxAge(duration time.Duration) Option
- func WithOutput(output string) Option
- func WithRotateTime(duration time.Duration) Option
- type StdLogger
- func (l *StdLogger) Debug(args ...interface{})
- func (l *StdLogger) Debugf(format string, args ...interface{})
- func (l *StdLogger) Error(args ...interface{})
- func (l *StdLogger) Errorf(format string, args ...interface{})
- func (l *StdLogger) Fatal(args ...interface{})
- func (l *StdLogger) Fatalf(format string, args ...interface{})
- func (l *StdLogger) GetLevel() Level
- func (l *StdLogger) Info(args ...interface{})
- func (l *StdLogger) Infof(format string, args ...interface{})
- func (l *StdLogger) SetLevel(level Level)
- func (l *StdLogger) Warn(args ...interface{})
- func (l *StdLogger) Warnf(format string, args ...interface{})
- func (l *StdLogger) WithField(key string, value interface{}) Logger
- func (l *StdLogger) WithFields(fields map[string]interface{}) Logger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fatalf ¶
func Fatalf(format string, args ...interface{})
Fatalf 使用全局日志实例记录格式化的致命错误级别日志。 记录日志后会导致程序退出。
func InitLogger ¶
InitLogger 初始化全局日志实例。 使用可选的配置选项来配置日志行为。 如果没有提供任何选项,将使用默认配置:
- 日志类型:LogTypeStd
- 日志级别:InfoLevel
- 输出路径:标准输出
返回初始化过程中可能发生的错误。
Types ¶
type Level ¶
type Level int
Level 定义了日志的级别类型,用于控制日志的输出粒度。
const ( // DebugLevel 表示调试级别,用于记录详细的调试信息。 // 这个级别的日志通常只在开发环境启用。 DebugLevel Level = iota // InfoLevel 表示信息级别,用于记录正常的操作信息。 // 这个级别的日志用于跟踪应用的正常运行状态。 InfoLevel // WarnLevel 表示警告级别,用于记录可能的问题或异常情况。 // 这个级别的日志表示出现了值得注意的情况,但不影响系统的正常运行。 WarnLevel // ErrorLevel 表示错误级别,用于记录错误信息。 // 这个级别的日志表示出现了影响系统正常运行的错误。 ErrorLevel // FatalLevel 表示致命错误级别,记录后会导致程序退出。 // 这个级别的日志表示出现了无法恢复的严重错误。 FatalLevel )
type Logger ¶
type Logger interface {
// SetLevel 设置日志级别。
// 只有大于或等于设置级别的日志才会被记录。
SetLevel(level Level)
// GetLevel 获取当前的日志级别。
GetLevel() Level
// Debug 记录调试级别的日志。
// 参数 args 支持任意类型的值,这些值会被转换为字符串并连接。
// 调试日志应该包含有助于诊断问题的详细信息。
Debug(args ...interface{})
// Debugf 记录格式化的调试级别日志。
// 参数 format 是格式化字符串,args 是对应的参数。
// 支持标准的 Printf 风格的格式化。
Debugf(format string, args ...interface{})
// Info 记录信息级别的日志。
// 参数 args 支持任意类型的值,这些值会被转换为字符串并连接。
// 信息日志应该记录系统的正常运行状态。
Info(args ...interface{})
// Infof 记录格式化的信息级别日志。
// 参数 format 是格式化字符串,args 是对应的参数。
// 支持标准的 Printf 风格的格式化。
Infof(format string, args ...interface{})
// Warn 记录警告级别的日志。
// 参数 args 支持任意类型的值,这些值会被转换为字符串并连接。
// 警告日志应该包含可能导致问题的情况。
Warn(args ...interface{})
// Warnf 记录格式化的警告级别日志。
// 参数 format 是格式化字符串,args 是对应的参数。
// 支持标准的 Printf 风格的格式化。
Warnf(format string, args ...interface{})
// Error 记录错误级别的日志。
// 参数 args 支持任意类型的值,这些值会被转换为字符串并连接。
// 错误日志应该包含错误的详细信息和上下文。
Error(args ...interface{})
// Errorf 记录格式化的错误级别日志。
// 参数 format 是格式化字符串,args 是对应的参数。
// 支持标准的 Printf 风格的格式化。
Errorf(format string, args ...interface{})
// Fatal 记录致命错误级别的日志。
// 参数 args 支持任意类型的值,这些值会被转换为字符串并连接。
// 记录日志后会导致程序以状态码 1 退出。
// 这个方法应该只在程序无法继续运行时使用。
Fatal(args ...interface{})
// Fatalf 记录格式化的致命错误级别日志。
// 参数 format 是格式化字符串,args 是对应的参数。
// 支持标准的 Printf 风格的格式化。
// 记录日志后会导致程序以状态码 1 退出。
Fatalf(format string, args ...interface{})
// WithField 添加一个字段到日志上下文。
// 参数 key 是字段名,value 是字段值。
// 返回一个新的 Logger 实例,原实例不会被修改。
// 这个方法用于添加结构化的上下文信息到日志中。
WithField(key string, value interface{}) Logger
// WithFields 添加多个字段到日志上下文。
// 参数 fields 是要添加的字段映射。
// 返回一个新的 Logger 实例,原实例不会被修改。
// 这个方法用于一次性添加多个结构化字段。
WithFields(fields map[string]interface{}) Logger
}
Logger 定义了统一的日志接口。 这个接口提供了基本的日志记录功能和结构化日志支持,可以通过不同的实现来支持不同的日志后端。
func NewLogrusLogger ¶
func NewLogrusLogger(opts ...LogrusOption) (Logger, error)
NewLogrusLogger 创建一个新的 LogrusLogger 实例。 opts:可选的配置选项列表,用于自定义日志记录器的行为。
func NewStdLogger ¶
NewStdLogger 创建一个新的 StdLogger 实例。 参数 output 指定日志文件的路径,如果为空则输出到标准输出。 返回一个实现了 Logger 接口的实例和可能的错误。
func WithFields ¶
WithFields 使用全局日志实例添加多个结构化字段。 返回一个新的 Logger 实例,包含添加的字段。
type LoggerFormatType ¶ added in v0.0.4
type LoggerFormatType string
LoggerFormatType 定义了日志输出格式的类型。
const ( // TextFormat 表示文本格式的日志输出。 TextFormat LoggerFormatType = "text" // JSONFormat 表示 JSON 格式的日志输出。 JSONFormat LoggerFormatType = "json" )
type LoggerOptions ¶ added in v0.0.2
type LoggerOptions struct {
// Type 指定日志实现类型。
Type LogType
// Level 指定日志级别。
Level Level
// Output 指定日志输出路径。
Output string
// EnableRotate 是否启用日志滚动。
EnableRotate bool
// RotateTime 日志滚动时间间隔。
RotateTime time.Duration
// MaxAge 日志保留时间。
MaxAge time.Duration
// FormatType 指定日志输出格式类型。
FormatType LoggerFormatType
}
LoggerOptions 定义了日志配置选项。
type LogrusLogger ¶
type LogrusLogger struct {
// contains filtered or unexported fields
}
LogrusLogger 实现了 Logger 接口,使用 Logrus 作为底层日志库。 这个实现提供了丰富的日志功能,包括: - 结构化日志记录。 - 多种输出格式(文本、JSON)。 - 灵活的日志级别控制。 - 支持同时输出到多个目标。
func (*LogrusLogger) Debug ¶
func (l *LogrusLogger) Debug(args ...interface{})
Debug 实现 Logger 接口的调试级别日志记录。
func (*LogrusLogger) Debugf ¶
func (l *LogrusLogger) Debugf(format string, args ...interface{})
Debugf 实现 Logger 接口的格式化调试级别日志记录。
func (*LogrusLogger) Error ¶
func (l *LogrusLogger) Error(args ...interface{})
Error 实现 Logger 接口的错误级别日志记录。
func (*LogrusLogger) Errorf ¶
func (l *LogrusLogger) Errorf(format string, args ...interface{})
Errorf 实现 Logger 接口的格式化错误级别日志记录。
func (*LogrusLogger) Fatal ¶
func (l *LogrusLogger) Fatal(args ...interface{})
Fatal 实现 Logger 接口的致命错误级别日志记录。 记录日志后会导致程序以状态码 1 退出。
func (*LogrusLogger) Fatalf ¶
func (l *LogrusLogger) Fatalf(format string, args ...interface{})
Fatalf 实现 Logger 接口的格式化致命错误级别日志记录。 记录日志后会导致程序以状态码 1 退出。
func (*LogrusLogger) GetLevel ¶
func (l *LogrusLogger) GetLevel() Level
GetLevel 实现 Logger 接口的日志级别获取方法。
func (*LogrusLogger) Info ¶
func (l *LogrusLogger) Info(args ...interface{})
Info 实现 Logger 接口的信息级别日志记录。
func (*LogrusLogger) Infof ¶
func (l *LogrusLogger) Infof(format string, args ...interface{})
Infof 实现 Logger 接口的格式化信息级别日志记录。
func (*LogrusLogger) SetLevel ¶
func (l *LogrusLogger) SetLevel(level Level)
SetLevel 实现 Logger 接口的日志级别设置方法。
func (*LogrusLogger) Warn ¶
func (l *LogrusLogger) Warn(args ...interface{})
Warn 实现 Logger 接口的警告级别日志记录。
func (*LogrusLogger) Warnf ¶
func (l *LogrusLogger) Warnf(format string, args ...interface{})
Warnf 实现 Logger 接口的格式化警告级别日志记录。
func (*LogrusLogger) WithField ¶
func (l *LogrusLogger) WithField(key string, value interface{}) Logger
WithField 实现 Logger 接口的单字段添加方法。 返回一个包含新字段的新 Logger 实例。
func (*LogrusLogger) WithFields ¶
func (l *LogrusLogger) WithFields(fields map[string]interface{}) Logger
WithFields 实现 Logger 接口的多字段添加方法。 返回一个包含新字段的新 Logger 实例。
type LogrusLoggerOptions ¶ added in v0.0.2
type LogrusLoggerOptions struct {
// 输出文件路径。
OutputPath string
// 日志格式化器。
Formatter logrus.Formatter
// 日志级别。
Level logrus.Level
// 文件权限。
FileMode os.FileMode
// 目录权限。
DirMode os.FileMode
// 是否启用日志滚动。
EnableRotate bool
// 日志滚动时间间隔。
RotateTime time.Duration
// 日志保留时间。
MaxAge time.Duration
}
LogrusLoggerOptions 包含了 LogrusLogger 的所有配置选项。
type LogrusOption ¶ added in v0.0.2
type LogrusOption func(*LogrusLoggerOptions)
LogrusOption 定义了 LogrusLogger 的配置选项函数类型。
func WithDirMode ¶ added in v0.0.2
func WithDirMode(mode os.FileMode) LogrusOption
WithDirMode 设置日志目录权限。 mode:日志目录的权限模式,使用八进制表示,例如:0755。
func WithFileMode ¶ added in v0.0.2
func WithFileMode(mode os.FileMode) LogrusOption
WithFileMode 设置日志文件权限。 mode:日志文件的权限模式,使用八进制表示,例如:0666。
func WithFormatter ¶ added in v0.0.2
func WithFormatter(formatter logrus.Formatter) LogrusOption
WithFormatter 设置日志格式化器。 formatter:自定义的日志格式化器实例,用于控制日志的输出格式。
func WithJSONFormatter ¶ added in v0.0.4
func WithJSONFormatter(timestampFormat string, prettyPrint bool) LogrusOption
WithJSONFormatter 设置 JSON 格式化器的选项。 timestampFormat:时间戳的格式化模板,例如:"2006-01-02 15:04:05"。 prettyPrint:是否美化 JSON 输出格式,true 表示美化,false 表示单行输出。
func WithLogrusEnableRotate ¶ added in v0.0.2
func WithLogrusEnableRotate(enable bool) LogrusOption
WithLogrusEnableRotate 设置是否启用日志滚动。 enable:是否启用日志滚动功能,true 表示启用,false 表示禁用。
func WithLogrusLevel ¶ added in v0.0.2
func WithLogrusLevel(level Level) LogrusOption
WithLogrusLevel 设置日志级别。 level:日志输出的级别,可选值包括 DebugLevel、InfoLevel、WarnLevel、ErrorLevel、FatalLevel。
func WithLogrusMaxAge ¶ added in v0.0.2
func WithLogrusMaxAge(duration time.Duration) LogrusOption
WithLogrusMaxAge 设置日志保留时间。 duration:日志文件的最大保留时间,超过这个时间的日志文件会被自动删除。
func WithLogrusRotateTime ¶ added in v0.0.2
func WithLogrusRotateTime(duration time.Duration) LogrusOption
WithLogrusRotateTime 设置日志滚动时间间隔。 duration:日志滚动的时间间隔,例如:time.Hour 表示每小时滚动一次。
func WithOutputPath ¶ added in v0.0.2
func WithOutputPath(path string) LogrusOption
WithOutputPath 设置日志输出路径。 path:日志文件的输出路径,支持绝对路径和相对路径。
func WithTextFormatter ¶ added in v0.0.4
func WithTextFormatter(timestampFormat string, fullTimestamp bool, disableColors bool) LogrusOption
WithTextFormatter 设置文本格式化器的选项。 timestampFormat:时间戳的格式化模板,例如:"2006-01-02 15:04:05"。 fullTimestamp:是否显示完整时间戳,true 表示显示完整时间戳,false 表示显示相对时间。 disableColors:是否禁用控制台颜色输出,true 表示禁用颜色,false 表示启用颜色。
type Option ¶ added in v0.0.2
type Option func(*LoggerOptions)
Option 定义了日志配置的函数选项。
func WithEnableRotate ¶ added in v0.0.2
WithEnableRotate 设置是否启用日志滚动。
func WithFormatType ¶ added in v0.0.4
func WithFormatType(formatType LoggerFormatType) Option
WithFormatType 设置日志输出格式类型。 formatType:日志输出格式类型,可选值包括 TextFormat、JSONFormat。
func WithMaxAge ¶ added in v0.0.2
WithMaxAge 设置日志保留时间。
func WithRotateTime ¶ added in v0.0.2
WithRotateTime 设置日志滚动时间间隔。
type StdLogger ¶
type StdLogger struct {
// contains filtered or unexported fields
}
StdLogger 实现了 Logger 接口,使用 Go 标准库的 log 包作为底层实现。 这个实现提供了基本的日志功能: - 支持不同的日志级别。 - 支持结构化字段。 - 支持文件输出。 - 支持格式化日志。
func (*StdLogger) Debug ¶
func (l *StdLogger) Debug(args ...interface{})
Debug 实现 Logger 接口的调试级别日志记录。
func (*StdLogger) Error ¶
func (l *StdLogger) Error(args ...interface{})
Error 实现 Logger 接口的错误级别日志记录。
func (*StdLogger) Fatal ¶
func (l *StdLogger) Fatal(args ...interface{})
Fatal 实现 Logger 接口的致命错误级别日志记录。 记录日志后会导致程序以状态码 1 退出。
func (*StdLogger) WithField ¶
WithField 实现 Logger 接口的单字段添加方法。 创建一个新的日志实例,包含当前实例的所有字段和新添加的字段。 返回包含所有字段的新 Logger 实例。
func (*StdLogger) WithFields ¶
WithFields 实现 Logger 接口的多字段添加方法。 创建一个新的日志实例,包含当前实例的所有字段和新添加的字段。 返回包含所有字段的新 Logger 实例。