Documentation ¶
Index ¶
- Constants
- Variables
- func Debug(a ...interface{})
- func DebugWithContext(ctx context.Context, a ...interface{})
- func Debugf(format string, a ...interface{})
- func DebugfWithContext(ctx context.Context, format string, a ...interface{})
- func Debugw(keyvals ...interface{})
- func DebugwWithContext(ctx context.Context, keyvals ...interface{})
- func DefaultLoggerKey() map[LoggerKey]string
- func Error(a ...interface{})
- func ErrorWithContext(ctx context.Context, a ...interface{})
- func Errorf(format string, a ...interface{})
- func ErrorfWithContext(ctx context.Context, format string, a ...interface{})
- func Errorw(keyvals ...interface{})
- func ErrorwWithContext(ctx context.Context, keyvals ...interface{})
- func Fatal(a ...interface{})
- func FatalWithContext(ctx context.Context, a ...interface{})
- func Fatalf(format string, a ...interface{})
- func FatalfWithContext(ctx context.Context, format string, a ...interface{})
- func Fatalw(keyvals ...interface{})
- func FatalwWithContext(ctx context.Context, keyvals ...interface{})
- func Info(a ...interface{})
- func InfoWithContext(ctx context.Context, a ...interface{})
- func Infof(format string, a ...interface{})
- func InfofWithContext(ctx context.Context, format string, a ...interface{})
- func Infow(keyvals ...interface{})
- func InfowWithContext(ctx context.Context, keyvals ...interface{})
- func Log(level log.Level, keyvals ...interface{})
- func LogWithContext(ctx context.Context, level log.Level, keyvals ...interface{})
- func NewDummyLogger() (log.Logger, error)
- func NewMultiLogger(logs ...log.Logger) log.Logger
- func NewNopLogger() log.Logger
- func ParseLevel(s string) log.Level
- func Print(a ...interface{})
- func Printf(format string, a ...interface{})
- func Println(a ...interface{})
- func Printw(keyvals ...interface{})
- func SetZapLoggerKeys(encoderConfig *zapcore.EncoderConfig, loggerKeys map[LoggerKey]string)
- func Setup(logger log.Logger)
- func ToZapLevel(lv log.Level) zapcore.Level
- func Warn(a ...interface{})
- func WarnWithContext(ctx context.Context, a ...interface{})
- func Warnf(format string, a ...interface{})
- func WarnfWithContext(ctx context.Context, format string, a ...interface{})
- func Warnw(keyvals ...interface{})
- func WarnwWithContext(ctx context.Context, keyvals ...interface{})
- type AsyncWriter
- type ConfigFile
- type ConfigStd
- type File
- type LoggerKey
- type MultiLogger
- type Option
- type Std
Examples ¶
Constants ¶
const ( // DefaultCallerSkip 日志 runtime caller skip DefaultCallerSkip = 3 // DefaultCallerValuer log.With 之 log.Caller DefaultCallerValuer = 4 )
const ( DefaultRotationStorageAge = time.Hour * 24 * 30 // 30天 DefaultRotationSize = 50 << 20 // 50M )
轮转日志参数
Variables ¶
var ( // CallerSkipForMiddleware 中间件日志 CallerSkipForMiddleware = DefaultCallerSkip // CallerSkipForLogger 基础日志 CallerSkipForLogger = DefaultCallerSkip + 1 // CallerSkipForHelper 日志助手 CallerSkipForHelper = DefaultCallerSkip + 2 )
Functions ¶
func DebugWithContext ¶
DebugWithContext logs a message at debug level.
func DebugfWithContext ¶
DebugfWithContext logs a message at debug level.
func DebugwWithContext ¶
DebugwWithContext logs a message at debug level.
func ErrorWithContext ¶
ErrorWithContext logs a message at error level.
func ErrorfWithContext ¶
ErrorfWithContext logs a message at error level.
func ErrorwWithContext ¶
ErrorwWithContext logs a message at error level.
func FatalWithContext ¶
FatalWithContext logs a message at fatal level.
func FatalfWithContext ¶
FatalfWithContext logs a message at fatal level.
func FatalwWithContext ¶
FatalwWithContext logs a message at fatal level.
func InfoWithContext ¶
InfoWithContext logs a message at info level.
func InfofWithContext ¶
InfofWithContext logs a message at info level.
func InfowWithContext ¶
InfowWithContext logs a message at info level.
func LogWithContext ¶
LogWithContext Print log by level and keyvals.
func NewMultiLogger ¶
NewMultiLogger wraps multi logger.
Example ¶
// 查看 TestNewMultiLogger(nil) // std logger stdLoggerConfig := &ConfigStd{ Level: log.LevelDebug, CallerSkip: DefaultCallerSkip + 1, } stdLogger, err := NewStdLogger(stdLoggerConfig) if err != nil { panic(err) } defer func() { _ = stdLogger.Close() }() // file logger fileLoggerConfig := &ConfigFile{ Level: log.LevelDebug, CallerSkip: DefaultCallerSkip + 1, } fileLogger, err := NewFileLogger(fileLoggerConfig) if err != nil { panic(err) } defer func() { _ = fileLogger.Close() }() logger := NewMultiLogger(stdLogger, fileLogger) logHandler := log.NewHelper(logger) logHandler.Error("error")
Output:
func SetZapLoggerKeys ¶
func SetZapLoggerKeys(encoderConfig *zapcore.EncoderConfig, loggerKeys map[LoggerKey]string)
SetZapLoggerKeys 设置日志key
func Setup ¶
Setup 启动 @Param loggers 请注意 ConfigStd.CallerSkip 的值 @Param loggers 请注意 ConfigFile.CallerSkip 的值
此处 CallerSkip = logutil.DefaultCallerSkip + 2
func WarnWithContext ¶
WarnWithContext logs a message at warn level.
func WarnfWithContext ¶
WarnfWithContext logs a message at warnf level.
func WarnwWithContext ¶
WarnwWithContext logs a message at warnf level.
Types ¶
type AsyncWriter ¶
type AsyncWriter struct {
// contains filtered or unexported fields
}
AsyncWriter ...
func NewAsyncWriter ¶
func NewAsyncWriter(writer io.Writer, size int) *AsyncWriter
NewAsyncWriter ...
func (*AsyncWriter) Close ¶
func (w *AsyncWriter) Close()
type ConfigFile ¶
type ConfigFile struct { // Level 日志级别 Level log.Level // CallerSkip 日志 runtime caller skips CallerSkip int // 存储位置 // Dir 文件夹 Dir string // Filename 文件名(默认:${filename}_app.%Y%m%d.log) Filename string // 轮询规则:默认为:RotateTime // RotateTime 轮询规则:n久(默认:86400s # 86400s = 1天) RotateTime time.Duration // RotateSize 轮询规则:按文件大小RotateSize(默认:52428800 # 50<<20 = 50M) RotateSize int64 // 存储规则:默认为:StorageAge // StorageAge 存储:n久(默认:30天) StorageAge time.Duration // StorageCounter 存储:n个(默认:10086个) StorageCounter uint }
ConfigFile 输出到文件
type ConfigStd ¶
type ConfigStd struct { // Level 日志级别 Level log.Level // CallerSkip 日志 runtime caller skips CallerSkip int UseJSONEncoder bool }
ConfigStd 标准输出
type File ¶
type File struct {
// contains filtered or unexported fields
}
File 输出到文件
func NewFileLogger ¶
func NewFileLogger(conf *ConfigFile, opts ...Option) (*File, error)
NewFileLogger 输出到文件
Example ¶
// 查看 TestNewFileLogger(nil) // file logger fileLoggerConfig := &ConfigFile{ Level: log.LevelDebug, // CallerSkip = DefaultCallerSkip CallerSkip: DefaultCallerSkip, Dir: "./../bin/", Filename: "rotation", //RotateTime: time.Second * 1, RotateSize: 50 << 20, // 50M : 50 << 20 StorageCounter: 2, //StorageAge: time.Hour, } fileLogger, err := NewFileLogger(fileLoggerConfig) if err != nil { panic(err) } defer func() { _ = fileLogger.Close() }() logHandler := log.NewHelper(fileLogger) logHandler.Error("error")
Output:
type LoggerKey ¶
type LoggerKey string
LoggerKey 日志消息key;例:time=2022-03-17T20:11:32.031, msg=testing
const ( // LoggerKeyMessage zapcore.EncoderConfig keys LoggerKeyMessage LoggerKey = "msg" LoggerKeyLevel LoggerKey = "level" LoggerKeyTime LoggerKey = "time" LoggerKeyName LoggerKey = "name" LoggerKeyCaller LoggerKey = "caller" LoggerKeyFunction LoggerKey = "func" LoggerKeyStacktrace LoggerKey = "stack" // DefaultTimeFormat 日志时间格式 DefaultTimeFormat = timepkg.YmdHmsMLogger )
type MultiLogger ¶
type MultiLogger struct {
// contains filtered or unexported fields
}
MultiLogger 多日志
type Option ¶
type Option func(*options)
Option is config option.
func WithLoggerKey ¶
WithLoggerKey with config writer.
func WithTimeFormat ¶
WithTimeFormat 时间格式 DefaultTimeFormat "2006-01-02T15:04:05.999"
type Std ¶
type Std struct {
// contains filtered or unexported fields
}
Std 标准输出
func NewStdLogger ¶
NewStdLogger 输出到控制台
Example ¶
// 查看 TestNewStdLogger(nil) // std logger stdLoggerConfig := &ConfigStd{ Level: log.LevelDebug, // CallerSkip = DefaultCallerSkip CallerSkip: DefaultCallerSkip, } stdLogger, err := NewStdLogger(stdLoggerConfig) if err != nil { panic(err) } defer func() { _ = stdLogger.Close() }() logHandler := log.NewHelper(stdLogger) logHandler.Error("error")
Output:
func (*Std) InitLogger ¶
InitLogger .