log

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2022 License: MIT Imports: 9 Imported by: 0

README

log

zap框架二次封装

使用
func main() {
    // 新建句柄
    field := zapcore.Field{
        Key:    "module_name",
        Type:   zapcore.StringType,
        String: "serverName",
    }
    bizLog := New("default",
        WithPath("./storage/logs"),
        WithTimeFormat("2006-01-02 15:04:05"),
        WithLevel(zapcore.InfoLevel),
        WithPreName("biz_"),
        WithEncoderJSON(false),
        WithFields(field))
    
    // 记录日志
    bizLog.Debug("this is SugarDebug")
    bizLog.Info("this is SugarInfo")
    bizLog.Warn("this is SugarWarn")
    bizLog.Error("this is SugarError")
    
    bizLog.Debugf("this is %s", "Debugf")
    bizLog.Infof("this is %s", "Infof")
    bizLog.Warnf("this is %s", "Warn")
    bizLog.Errorf("this is %s", "Errorf")
    
    bizLog.Debugw("this is Debugw", "k", "Debugw")
    bizLog.Infow("this is Infow", "k", "Infow")
    bizLog.Warnw("this is Warnw", "k", "Warnw")
    bizLog.Errorw("this is Errorw", "k", "Errorw")
}


全局配置

修改默认配置,之后新建的日志,将默认使用这些配置

SetGlobalConf(options ...ConfigOption), 配置项使用WithXxx()

  • WithPath 日志路径,默认为 .
  • WithLevel 最低打印级别,默认为 debug
  • WithLogInConsole 是否打印到控制台,默认为 false
  • WithFields 该日志都加的字段,默认为空
  • WithEncoderJSON 是否为json格式日志,默认为 true
  • WithTimeFormat 时间格式,默认为 2006-01-02T15:04:05Z07:00
  • WithPreName 日志前缀,默认为 biz_
func main() {
    field := zapcore.Field{
        Key:    "module_name",
        Type:   zapcore.StringType,
        String: "helloLog",
    }
    // 设置默认配置后,之后新建的所有日志都默认使用该配置
    SetDefaultConf(
    	WithPath("./storage/logs"),
        WithTimeFormat("2006-01-02 15:04:05"),
        WithLevel(zapcore.InfoLevel),
        WithPreName("biz_"),
        WithEncoderJSON(false),
        WithFields(field))
    
    // 新建日志
    // ……
}
日志记录上下文信息
func main() {
    traceIdKey := "trace_id"
	c := context.WithValue(context.Background(), traceIdKey, "r61f0ed0d70098_Zw8R1aoyl4tGeB4HMV")

	// 告知log从上下文获取trace_id并记录到每个日志
	SetDefaultConf(WithCtxField(traceIdKey))

	l := New("trace")
	l.DebugWithCtx(c,"this is SugarDebug")
	l.InfoWithCtx(c,"this is SugarInfo")
	l.WarnWithCtx(c,"this is SugarWarn")
	l.ErrorWithCtx(c,"this is SugarError")

	l.DebugfWithCtx(c,"this is %s", "Debugf")
	l.InfofWithCtx(c,"this is %s", "Infof")
	l.WarnfWithCtx(c,"this is %s", "Warn")
	l.ErrorfWithCtx(c,"this is %s", "Errorf")

	l.DebugwWithCtx(c,"this is Debugw", "k", "Debugw")
	l.InfowWithCtx(c,"this is Infow", "k", "Infow")
	l.WarnwWithCtx(c,"this is Warnw", "k", "Warnw")
	l.ErrorwWithCtx(c,"this is Errorw", "k", "Errorw")
}

Documentation

Index

Constants

View Source
const (
	DebugLevel = "debug"
	InfoLevel  = "info"
	WarnLevel  = "warn"
	ErrorLevel = "error"
)

Variables

View Source
var (
	Sugar = std.Sugar

	Debug  = std.Debug
	Info   = std.Info
	Warn   = std.Warn
	Error  = std.Error
	DPanic = std.DPanic
	Panic  = std.Panic
	Fatal  = std.Fatal

	Debugf  = std.Debugf
	Infof   = std.Infof
	Warnf   = std.Warnf
	Errorf  = std.Errorf
	DPanicf = std.DPanicf
	Panicf  = std.Panicf
	Fatalf  = std.Fatalf

	Debugw  = std.Debugw
	Infow   = std.Infow
	Warnw   = std.Warnw
	Errorw  = std.Errorw
	DPanicw = std.DPanicw
	Panicw  = std.Panicw
	Fatalw  = std.Fatalw

	DebugWithCtx   = std.DebugWithCtx
	InfoWithCtx    = std.InfoWithCtx
	WarnWithCtx    = std.WarnWithCtx
	ErrorWithCtx   = std.ErrorWithCtx
	DPanicWithCtx  = std.DPanicWithCtx
	PanicWithCtx   = std.PanicWithCtx
	FatalWithCtx   = std.FatalWithCtx
	DebugfWithCtx  = std.DebugfWithCtx
	InfofWithCtx   = std.InfofWithCtx
	WarnfWithCtx   = std.WarnfWithCtx
	ErrorfWithCtx  = std.ErrorfWithCtx
	DPanicfWithCtx = std.DPanicfWithCtx
	PanicfWithCtx  = std.PanicfWithCtx
	FatalfWithCtx  = std.FatalfWithCtx
	DebugwWithCtx  = std.DebugwWithCtx
	InfowWithCtx   = std.InfowWithCtx
	WarnwWithCtx   = std.WarnwWithCtx
	ErrorwWithCtx  = std.ErrorwWithCtx
	DPanicwWithCtx = std.DPanicwWithCtx
	PanicwWithCtx  = std.PanicwWithCtx
	FatalwWithCtx  = std.FatalwWithCtx
)

Functions

func Init

func Init(options ...ConfigOption)

func ResetDefault

func ResetDefault(l *Logger)

not safe for concurrent use

func SetDefaultConf

func SetDefaultConf(options ...ConfigOption)

SetDefaultConf 设置默认日志配置

Types

type Conf

type Conf struct {
	Name          string
	Path          string          // 日志路径,默认为 .
	IsDistinguish bool            // 区分正常日志和错误日志
	LowLevel      zapcore.Level   // 最低打印级别,默认为 debug
	LogInConsole  bool            // 是否打印到控制台,默认为 false
	Fields        []zapcore.Field // 该日志都加的字段,默认为空
	IsJSONEncode  bool            // 是否为json格式日志,默认为 true
	TimeFormat    string          // 时间格式,默认为 2006-01-02T15:04:05Z07:00
	PreName       string          // 日志前缀
	CtxFields     []string        // 打印上下文字段
}

func (*Conf) LogPath

func (c *Conf) LogPath() string

type ConfigOption

type ConfigOption func(conf *Conf)

func WithCtxField added in v0.0.16

func WithCtxField(fieldsKey ...string) ConfigOption

添加需要打印的上下文字段, 新建的日志将会是新配置,已经建立的日志配置不变

func WithDistinguish

func WithDistinguish(isDistinguish bool) ConfigOption

WithDistinguish 是否区分日志级别,<=info和>=err分开记录 @param isDistinguish @return ConfigOption

func WithEncoderJSON

func WithEncoderJSON(isJSONEncoder bool) ConfigOption

WithEncoderJSON 是否设置为json格式日志 @param isJSONEncoder @return ConfigOption

func WithFields

func WithFields(fields ...zapcore.Field) ConfigOption

WithFields 添加全局日志的新字段, 新建的日志将会是新配置,已经建立的日志配置不变 @param field 日志字段

func WithLevel

func WithLevel(level string) ConfigOption

WithLevel 设置服务记录的最低日志级别 修改后,新建的日志将会是新配置,已经建立的日志配置不变 @param l 日志级别(debug、info、warn、error)

func WithLogInConsole

func WithLogInConsole(isLogInConsole bool) ConfigOption

WithLogInConsole 是否输出到控制台 修改后,新建的日志将会是新配置,已经建立的日志配置不变 @param isLogInConsole

func WithPath

func WithPath(path string) ConfigOption

WithPath 设置日志路径, 修改后,新建的日志将会是新配置,已经建立的日志配置不变 @param path 路径

func WithPreName

func WithPreName(pre string) ConfigOption

WithPreName 设置日志前缀 @param pre 前缀 @return ConfigOption

func WithTimeFormat

func WithTimeFormat(format string) ConfigOption

WithTimeFormat 设置时间格式 @param format 时间格式 @return ConfigOption

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func Default

func Default() *Logger

func New

func New(name string, options ...ConfigOption) *Logger

New 创建新日志文件句柄,使用默认配置 @param name 日志名 @param options 日志配置,将覆盖默认配置 @return *Logger

func (*Logger) DPanic

func (l *Logger) DPanic(msg string, fields ...zapcore.Field)

DPanic logs a message at DPanicLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

If the logger is in development mode, it then panics (DPanic means "development panic"). This is useful for catching errors that are recoverable, but shouldn't ever happen.

func (*Logger) DPanicWithCtx added in v0.0.16

func (l *Logger) DPanicWithCtx(c context.Context, msg string, fields ...zapcore.Field)

DPanic logs a message at DPanicLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

If the logger is in development mode, it then panics (DPanic means "development panic"). This is useful for catching errors that are recoverable, but shouldn't ever happen.

func (*Logger) DPanicf

func (l *Logger) DPanicf(template string, args ...interface{})

DPanicf uses fmt.Sprintf to log a templated message. In development, the logger then panics. (See DPanicLevel for details.)

func (*Logger) DPanicfWithCtx added in v0.0.16

func (l *Logger) DPanicfWithCtx(c context.Context, template string, args ...interface{})

DPanicf uses fmt.Sprintf to log a templated message. In development, the logger then panics. (See DPanicLevel for details.)

func (*Logger) DPanicw

func (l *Logger) DPanicw(msg string, keysAndValues ...interface{})

DPanicw logs a message with some additional context. In development, the logger then panics. (See DPanicLevel for details.) The variadic key-value pairs are treated as they are in With.

func (*Logger) DPanicwWithCtx added in v0.0.16

func (l *Logger) DPanicwWithCtx(c context.Context, msg string, keysAndValues ...interface{})

DPanicw logs a message with some additional context. In development, the logger then panics. (See DPanicLevel for details.) The variadic key-value pairs are treated as they are in With.

func (*Logger) Debug

func (l *Logger) Debug(msg string, fields ...zapcore.Field)

Debug logs a message at DebugLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*Logger) DebugWithCtx added in v0.0.16

func (l *Logger) DebugWithCtx(c context.Context, msg string, fields ...zapcore.Field)

Debug logs a message at DebugLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*Logger) Debugf

func (l *Logger) Debugf(template string, args ...interface{})

Debugf uses fmt.Sprintf to log a templated message.

func (*Logger) DebugfWithCtx added in v0.0.16

func (l *Logger) DebugfWithCtx(c context.Context, template string, args ...interface{})

Debugf uses fmt.Sprintf to log a templated message.

func (*Logger) Debugw

func (l *Logger) Debugw(msg string, keysAndValues ...interface{})

Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

When debug-level logging is disabled, this is much faster than

s.With(keysAndValues).Debug(msg)

func (*Logger) DebugwWithCtx added in v0.0.16

func (l *Logger) DebugwWithCtx(c context.Context, msg string, keysAndValues ...interface{})

Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

When debug-level logging is disabled, this is much faster than

s.With(keysAndValues).Debug(msg)

func (*Logger) Error

func (l *Logger) Error(msg string, fields ...zapcore.Field)

Error logs a message at ErrorLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*Logger) ErrorWithCtx added in v0.0.16

func (l *Logger) ErrorWithCtx(c context.Context, msg string, fields ...zapcore.Field)

Error logs a message at ErrorLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*Logger) Errorf

func (l *Logger) Errorf(template string, args ...interface{})

Errorf uses fmt.Sprintf to log a templated message.

func (*Logger) ErrorfWithCtx added in v0.0.16

func (l *Logger) ErrorfWithCtx(c context.Context, template string, args ...interface{})

Errorf uses fmt.Sprintf to log a templated message.

func (*Logger) Errorw

func (l *Logger) Errorw(msg string, keysAndValues ...interface{})

Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func (*Logger) ErrorwWithCtx added in v0.0.16

func (l *Logger) ErrorwWithCtx(c context.Context, msg string, keysAndValues ...interface{})

Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, fields ...zapcore.Field)

Fatal logs a message at FatalLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

The logger then calls os.Exit(1), even if logging at FatalLevel is disabled.

func (*Logger) FatalWithCtx added in v0.0.16

func (l *Logger) FatalWithCtx(c context.Context, msg string, fields ...zapcore.Field)

Fatal logs a message at FatalLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

The logger then calls os.Exit(1), even if logging at FatalLevel is disabled.

func (*Logger) Fatalf

func (l *Logger) Fatalf(template string, args ...interface{})

Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.

func (*Logger) FatalfWithCtx added in v0.0.16

func (l *Logger) FatalfWithCtx(c context.Context, template string, args ...interface{})

Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.

func (*Logger) Fatalw

func (l *Logger) Fatalw(msg string, keysAndValues ...interface{})

Fatalw logs a message with some additional context, then calls os.Exit. The variadic key-value pairs are treated as they are in With.

func (*Logger) FatalwWithCtx added in v0.0.16

func (l *Logger) FatalwWithCtx(c context.Context, msg string, keysAndValues ...interface{})

Fatalw logs a message with some additional context, then calls os.Exit. The variadic key-value pairs are treated as they are in With.

func (*Logger) Info

func (l *Logger) Info(msg string, fields ...zapcore.Field)

Info logs a message at InfoLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*Logger) InfoWithCtx added in v0.0.16

func (l *Logger) InfoWithCtx(c context.Context, msg string, fields ...zapcore.Field)

Info logs a message at InfoLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*Logger) Infof

func (l *Logger) Infof(template string, args ...interface{})

Infof uses fmt.Sprintf to log a templated message.

func (*Logger) InfofWithCtx added in v0.0.16

func (l *Logger) InfofWithCtx(c context.Context, template string, args ...interface{})

Infof uses fmt.Sprintf to log a templated message.

func (*Logger) Infow

func (l *Logger) Infow(msg string, keysAndValues ...interface{})

Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func (*Logger) InfowWithCtx added in v0.0.16

func (l *Logger) InfowWithCtx(c context.Context, msg string, keysAndValues ...interface{})

Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func (*Logger) Log

func (l *Logger) Log() *zap.Logger

func (*Logger) Name

func (l *Logger) Name() string

func (*Logger) Panic

func (l *Logger) Panic(msg string, fields ...zapcore.Field)

Panic logs a message at PanicLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

The logger then panics, even if logging at PanicLevel is disabled.

func (*Logger) PanicWithCtx added in v0.0.16

func (l *Logger) PanicWithCtx(c context.Context, msg string, fields ...zapcore.Field)

Panic logs a message at PanicLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

The logger then panics, even if logging at PanicLevel is disabled.

func (*Logger) Panicf

func (l *Logger) Panicf(template string, args ...interface{})

Panicf uses fmt.Sprintf to log a templated message, then panics.

func (*Logger) PanicfWithCtx added in v0.0.16

func (l *Logger) PanicfWithCtx(c context.Context, template string, args ...interface{})

Panicf uses fmt.Sprintf to log a templated message, then panics.

func (*Logger) Panicw

func (l *Logger) Panicw(msg string, keysAndValues ...interface{})

Panicw logs a message with some additional context, then panics. The variadic key-value pairs are treated as they are in With.

func (*Logger) PanicwWithCtx added in v0.0.16

func (l *Logger) PanicwWithCtx(c context.Context, msg string, keysAndValues ...interface{})

Panicw logs a message with some additional context, then panics. The variadic key-value pairs are treated as they are in With.

func (*Logger) Printf

func (l *Logger) Printf(template string, args ...interface{})

func (*Logger) Sugar

func (l *Logger) Sugar() *zap.SugaredLogger

func (*Logger) Sync

func (l *Logger) Sync() error

func (*Logger) Warn

func (l *Logger) Warn(msg string, fields ...zapcore.Field)

Warn logs a message at WarnLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*Logger) WarnWithCtx added in v0.0.16

func (l *Logger) WarnWithCtx(c context.Context, msg string, fields ...zapcore.Field)

Warn logs a message at WarnLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.

func (*Logger) Warnf

func (l *Logger) Warnf(template string, args ...interface{})

Warnf uses fmt.Sprintf to log a templated message.

func (*Logger) WarnfWithCtx added in v0.0.16

func (l *Logger) WarnfWithCtx(c context.Context, template string, args ...interface{})

Warnf uses fmt.Sprintf to log a templated message.

func (*Logger) Warnw

func (l *Logger) Warnw(msg string, keysAndValues ...interface{})

Warnw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func (*Logger) WarnwWithCtx added in v0.0.16

func (l *Logger) WarnwWithCtx(c context.Context, msg string, keysAndValues ...interface{})

Warnw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

Jump to

Keyboard shortcuts

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