logger

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

日志类

用于抽象日志处理层

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(logger Logger, desc string, args ...*LogArg)

func DefaultLogger

func DefaultLogger() *defaultLogger

创建 logger 默认实现对象

func Error

func Error(logger Logger, desc string, err interface{})

func ErrorOnlyArg

func ErrorOnlyArg(logger Logger, desc string, args ...*LogArg)

func ErrorWithArg

func ErrorWithArg(logger Logger, desc string, err interface{}, args ...*LogArg)

func Fatal

func Fatal(logger Logger, desc string, err interface{})

func FatalOnlyArg

func FatalOnlyArg(logger Logger, desc string, args ...*LogArg)

func FatalWithArg

func FatalWithArg(logger Logger, desc string, err interface{}, args ...*LogArg)

func Info

func Info(logger Logger, desc string, args ...*LogArg)

func NewZapLogger

func NewZapLogger(config *Config) *zapLogger

func Panic

func Panic(logger Logger, desc string, err interface{})

func PanicOnlyArg

func PanicOnlyArg(logger Logger, desc string, args ...*LogArg)

func PanicWithArg

func PanicWithArg(logger Logger, desc string, err interface{}, args ...*LogArg)

func Warn

func Warn(logger Logger, desc string, err interface{})

func WarnOnlyArg

func WarnOnlyArg(logger Logger, desc string, args ...*LogArg)

func WarnWithArg

func WarnWithArg(logger Logger, desc string, err interface{}, args ...*LogArg)

Types

type Config

type Config struct {
	// 当前配置名称
	Name string
	// 日志存放的绝对路径
	//	路径中支持的变量
	//		年份	%y	强制2位	%Y 4位
	//		月份	%m	自然位	%M 补齐2位
	//		日期	%d	自然位	%D 补齐2位
	//		小时	%h	自然位	%H 补齐2位
	//		分钟	%mi	自然位	%Mi 补齐2位
	//		秒数	%s	自然位	%S 补齐2位
	LogPath string
	// TODO 针对不同级别日志文件路径的配置
	// 日志是否输出到控制台
	ConsoleLog bool
	// 是否延迟写入
	//	0 表示实时写入,不会做延迟处理
	//	n(>0) 表示延迟 n 毫秒写入,可以降低 IO 写入次数
	WriteDelay int
	// 是否显示在顶层中
	// 需要显示在顶层级别的字段名
	ShowInTopLevel []string
}

type Level

type Level int8

日志级别

const (
	// 调试级别
	// 调试级别的日志体积会比较大,一般生成环境下禁用
	DebugLevel Level = iota - 1
	// InfoLevel is the default logging priority.
	InfoLevel
	// WarnLevel logs are more important than Info, but don't need individual
	// human review.
	WarnLevel
	// ErrorLevel logs are high-priority. If an application is running smoothly,
	// it shouldn't generate any error-level logs.
	ErrorLevel
	// DPanicLevel logs are particularly important errors. In development the
	// logger panics after writing the message.
	DPanicLevel
	// PanicLevel logs a message, then panics.
	PanicLevel
	// FatalLevel logs a message, then calls os.Exit(1).
	FatalLevel
)

func (Level) CapitalString

func (l Level) CapitalString() string

CapitalString returns an all-caps ASCII representation of the log level.

Example
level := DebugLevel

fmt.Println(level.CapitalString())
Output:

DEBUG

func (Level) String

func (l Level) String() string

返回日志级别的小写字符串

Example
level := DebugLevel

fmt.Println(level.String())
Output:

debug

type LogArg

type LogArg struct {
	Key        string      // 参数名
	IsTopLevel bool        // 是否在顶层输出
	Value      interface{} // 值
}

func ArgAny

func ArgAny(key string, val interface{}) *LogArg

func ArgToken

func ArgToken(token string) *LogArg

func ArgUserId

func ArgUserId(userid string) *LogArg

type LogArgKey

type LogArgKey string

常见 KEY 设置

const (
	KeyUserid LogArgKey = "userid"
	KeyToken  LogArgKey = "token"
)

内置 Key 预设

func (LogArgKey) String

func (key LogArgKey) String() string

type Logger

type Logger interface {
	Debug(desc string, args ...*LogArg)
	Error(desc string, args ...*LogArg)
	Fatal(desc string, args ...*LogArg)
	Info(desc string, args ...*LogArg)
	Panic(desc string, args ...*LogArg)
	Warn(desc string, args ...*LogArg)
}

日志类接口

type TimeWriter

type TimeWriter struct {
	CurrentPath  string
	PathTemplate string
	// contains filtered or unexported fields
}

func NewTimeWriter

func NewTimeWriter(logTemplate string) (obj *TimeWriter)

func (*TimeWriter) Close

func (l *TimeWriter) Close() error

func (*TimeWriter) Write

func (l *TimeWriter) Write(p []byte) (n int, err error)

type ZapBody

type ZapBody struct {
	// 日志等级
	LogLevel Level
	// 描述信息
	Desc string
	// 顶级数据
	TopLevelData map[string]interface{}
	// 附加数据
	Data map[string]interface{}
}

Jump to

Keyboard shortcuts

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