zaplog

package
v0.0.0-...-79697a8 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetConfigEncoder

func GetConfigEncoder(cfg FormatConfig) zapcore.Encoder

通过config获取

func GetConsoleEncoder

func GetConsoleEncoder(cfg FormatConfig) zapcore.Encoder

控制台日志格式,大写带颜色

func GetCoreSimple

func GetCoreSimple(cfg LogConfig) []zapcore.Core

GetCoreSimple 获取Encoder的 zapcore.Core 只分为 info 和 error两个等级

func GetDefaultZapLogger

func GetDefaultZapLogger() *zap.Logger

func GetEncoderCore

func GetEncoderCore(cfg LogConfig) []zapcore.Core

func GetLevelLowThan

func GetLevelLowThan(level zapcore.Level) zap.LevelEnablerFunc

比等级低才打印

func GetLevelPriority

func GetLevelPriority(level zapcore.Level) zap.LevelEnablerFunc

GetLevelPriority 根据 zapcore.Level 获取 zap.LevelEnablerFunc 相等才发布

func GetLevelUpThan

func GetLevelUpThan(level zapcore.Level) zap.LevelEnablerFunc

比日志等级高才打印

func GetSizeWriter

func GetSizeWriter(cfg WriterConfig, level string) *lumberjack.Logger

GetSizeWriter 按文件大小分割日志文件

func GetTimeWriter

func GetTimeWriter(cfg WriterConfig, level string) *rotatelogs.RotateLogs

GetTimeWriter 按时间分割日志文件 runtime/log/2022-11 rotate库于2022.11.9开始不再更新

func NewZapLogger

func NewZapLogger(skip int, cfg LogConfig) *zap.Logger

默认的zap不需要增加skip

Types

type FormatConfig

type FormatConfig struct {
	Prefix        string //前缀
	Format        string
	StacktraceKey string
	LogInConsole  bool
	ShowLine      bool
	EncodeLevel   zapcore.LevelEncoder
}

负责设置 encoding 的日志格式

func (FormatConfig) PrefixTimeEncoder

func (m FormatConfig) PrefixTimeEncoder(t time.Time, encoder zapcore.PrimitiveArrayEncoder)

PrefixTimeEncoder 自定义带前缀日志输出时间格式

type LogConfig

type LogConfig struct {
	Level         string `mapstructure:"level" json:"level" yaml:"level"`                            // 级别
	Prefix        string `mapstructure:"prefix" json:"prefix" yaml:"prefix"`                         // 日志前缀
	Format        string `mapstructure:"format" json:"format" yaml:"format"`                         // 输出
	Director      string `mapstructure:"director" json:"director"  yaml:"director"`                  // 日志文件夹
	EncodeLevel   string `mapstructure:"encode-level" json:"encode-level" yaml:"encode-level"`       // 编码级
	StacktraceKey string `mapstructure:"stacktrace-key" json:"stacktrace-key" yaml:"stacktrace-key"` // 栈名

	MaxAge       int  `mapstructure:"max-age" json:"max-age" yaml:"max-age"`                      // 日志留存时间
	ShowLine     bool `mapstructure:"show-line" json:"show-line" yaml:"show-line"`                // 显示行
	LogInConsole bool `mapstructure:"log-in-console" json:"log-in-console" yaml:"log-in-console"` // 输出控制台
}

func GetDefaultConfig

func GetDefaultConfig() LogConfig

func (*LogConfig) GetEncoderConfig

func (cfg *LogConfig) GetEncoderConfig() FormatConfig

func (*LogConfig) GetWriterConfig

func (cfg *LogConfig) GetWriterConfig() WriterConfig

func (*LogConfig) TransportLevel

func (cfg *LogConfig) TransportLevel() zapcore.Level

TransportLevel 根据字符串转化为 zapcore.Level Author [SliverHorn](https://github.com/SliverHorn)

func (*LogConfig) ZapEncodeLevel

func (cfg *LogConfig) ZapEncodeLevel() zapcore.LevelEncoder

ZapEncodeLevel 根据 EncodeLevel 返回 zapcore.LevelEncoder Author [SliverHorn](https://github.com/SliverHorn)

type WriterConfig

type WriterConfig struct {
	FileDir    string //日志文件的路径
	MaxSize    int    //在进行切割之前,日志文件的最大大小(以MB为单位)
	MaxBackups int    //保留旧文件的最大个数
	MaxAge     int    //保留旧文件的最大天数
	Compress   bool   //是否压缩/归档旧文件

}

日志按大小切割和备份个数、文件有效期,一般选zap+lumberjack 日志按天或者小时切割,一般选zap+file-rotatelogs

Jump to

Keyboard shortcuts

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