Documentation
¶
Overview ¶
Package log 日志打印输出
Example Usage
log.MustSetup(c) // 使用配置初始化日志
log.Info("hello %s", nickname) // Info 日志
log.Error("error %s", err.String()) // Error 日志
log.WithContext(ctx) // 设置 trace span 等附加信息
Index ¶
- Constants
- Variables
- func Debug(message string, param ...interface{})
- func Error(message string, param ...interface{})
- func Info(message string, param ...interface{})
- func MustSetup(c *Config)
- func RegisterLoggerType(name string, f NewLoggerFunc)
- func SetLevel(level Level)
- func SetLogger(logger Logger)
- func Setup(c *Config) error
- func Warning(message string, param ...interface{})
- func WithContext(ctx context.Context)
- type Config
- type Level
- type Logger
- type NewLoggerFunc
Constants ¶
View Source
const ( DefaultLevel = INFO DefaultService = "Unknown" DefaultTimeFormat = "15:04:05.000" )
View Source
const (
ModuleName = "Default"
)
Variables ¶
View Source
var ( // ErrInvalidConfig 不支持的日志设置 ErrInvalidConfig = errors.New("invalid log config") )
Functions ¶
func MustSetup ¶
func MustSetup(c *Config)
MustSetup 通过配置初始化日志,失败会使当前线程崩溃
在大多数场景下,推荐使用这个函数初始化/改动日志设置
func RegisterLoggerType ¶
func RegisterLoggerType(name string, f NewLoggerFunc)
RegisterLoggerType 注册一个新的日志类型
通过这个函数,可以轻松扩展这个库
Types ¶
type Config ¶
type Config struct {
Type string `json:",default=default,options=default|zero"`
Level string `json:",default=info,options=info|debug|warn|error"`
ServiceName string `json:",optional"`
TimeFormat string `json:",optional"`
}
Config 日志配置
我们推荐用配置文件的方式设置日志
type Logger ¶
type Logger interface {
// Debug 打印 Debug 信息
Debug(string, ...interface{})
// Info 打印 Info 信息
Info(string, ...interface{})
// Warning 打印 Warning 信息
Warning(string, ...interface{})
// Error 打印 Error 信息
Error(string, ...interface{})
// SetLevel 设置日志等级
SetLevel(Level)
// WithContext 设置日志上下文(通常是用作设置 trace span 信息,可以用作扩展功能的信息传递)
WithContext(ctx context.Context)
}
Logger 日志处理抽象
type NewLoggerFunc ¶
NewLoggerFunc 创建日志实例的函数
Click to show internal directories.
Click to hide internal directories.