log

package
v1.1.3 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))
    
    // 新建日志
    // ……
}
带有请求id的日志
func main() {
    c := context.WithValue(context.Background(), RequestIdKey(), "r61f0ed0d70098_Zw8R1aoyl4tGeB4HMV")
    
    l := New("trace")
    l.DebugWithTrace(c,"this is SugarDebug")
    l.InfoWithTrace(c,"this is SugarInfo")
    l.WarnWithTrace(c,"this is SugarWarn")
    l.ErrorWithTrace(c,"this is SugarError")
    
    l.DebugfWithTrace(c,"this is %s", "Debugf")
    l.InfofWithTrace(c,"this is %s", "Infof")
    l.WarnfWithTrace(c,"this is %s", "Warn")
    l.ErrorfWithTrace(c,"this is %s", "Errorf")
    
    l.DebugwWithTrace(c,"this is Debugw", "k", "Debugw")
    l.InfowWithTrace(c,"this is Infow", "k", "Infow")
    l.WarnwWithTrace(c,"this is Warnw", "k", "Warnw")
    l.ErrorwWithTrace(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

	DebugWithTrace   = std.DebugWithTrace
	InfoWithTrace    = std.InfoWithTrace
	WarnWithTrace    = std.WarnWithTrace
	ErrorWithTrace   = std.ErrorWithTrace
	DPanicWithTrace  = std.DPanicWithTrace
	PanicWithTrace   = std.PanicWithTrace
	FatalWithTrace   = std.FatalWithTrace
	DebugfWithTrace  = std.DebugfWithTrace
	InfofWithTrace   = std.InfofWithTrace
	WarnfWithTrace   = std.WarnfWithTrace
	ErrorfWithTrace  = std.ErrorfWithTrace
	DPanicfWithTrace = std.DPanicfWithTrace
	PanicfWithTrace  = std.PanicfWithTrace
	FatalfWithTrace  = std.FatalfWithTrace
	DebugwWithTrace  = std.DebugwWithTrace
	InfowWithTrace   = std.InfowWithTrace
	WarnwWithTrace   = std.WarnwWithTrace
	ErrorwWithTrace  = std.ErrorwWithTrace
	DPanicwWithTrace = std.DPanicwWithTrace
	PanicwWithTrace  = std.PanicwWithTrace
	FatalwWithTrace  = std.FatalwWithTrace
)

Functions

func Init

func Init(options ...ConfigOption)

func RequestIdKey

func RequestIdKey() string

RequestIdKey 获取请求日志id键

func RequestIdVal

func RequestIdVal(c context.Context) string

RequestIdVal 获取请求日志id值

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          // 日志前缀
}

func (*Conf) LogPath

func (c *Conf) LogPath() string

type ConfigOption

type ConfigOption func(conf *Conf)

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) DPanicWithTrace

func (l *Logger) DPanicWithTrace(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) DPanicfWithTrace

func (l *Logger) DPanicfWithTrace(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) DPanicwWithTrace

func (l *Logger) DPanicwWithTrace(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) DebugWithTrace

func (l *Logger) DebugWithTrace(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) DebugfWithTrace

func (l *Logger) DebugfWithTrace(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) DebugwWithTrace

func (l *Logger) DebugwWithTrace(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) ErrorWithTrace

func (l *Logger) ErrorWithTrace(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) ErrorfWithTrace

func (l *Logger) ErrorfWithTrace(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) ErrorwWithTrace

func (l *Logger) ErrorwWithTrace(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) FatalWithTrace

func (l *Logger) FatalWithTrace(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) FatalfWithTrace

func (l *Logger) FatalfWithTrace(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) FatalwWithTrace

func (l *Logger) FatalwWithTrace(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) InfoWithTrace

func (l *Logger) InfoWithTrace(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) InfofWithTrace

func (l *Logger) InfofWithTrace(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) InfowWithTrace

func (l *Logger) InfowWithTrace(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) PanicWithTrace

func (l *Logger) PanicWithTrace(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) PanicfWithTrace

func (l *Logger) PanicfWithTrace(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) PanicwWithTrace

func (l *Logger) PanicwWithTrace(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) WarnWithTrace

func (l *Logger) WarnWithTrace(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) WarnfWithTrace

func (l *Logger) WarnfWithTrace(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) WarnwWithTrace

func (l *Logger) WarnwWithTrace(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