logger

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2024 License: Apache-2.0 Imports: 18 Imported by: 7

Documentation

Index

Constants

View Source
const (
	WriterConsole = "console"
	WriterFile    = "file"
)
View Source
const (
	WriteSync  = 1 // 同步写入
	WriteAsync = 2 // 异步写入
	WriteFast  = 3 // 快速写入(队列满的时候会丢弃日志)
)
View Source
const (
	LevelDebug = "debug"
	LevelInfo  = "info"
	LevelWarn  = "warn"
	LevelError = "error"
	LevelFatal = "fatal"
)

Enums log level constants.

Variables

View Source
var Levels = map[string]zapcore.Level{
	"":         zapcore.DebugLevel,
	LevelDebug: zapcore.DebugLevel,
	LevelInfo:  zapcore.InfoLevel,
	LevelWarn:  zapcore.WarnLevel,
	LevelError: zapcore.ErrorLevel,
	LevelFatal: zapcore.FatalLevel,
}

Levels is the map from string to zapcore.Level.

Functions

func CreateDefaultLogger

func CreateDefaultLogger(cfg []*Config)

CreateDefaultLogger 根据配置创建默认 DefaultLogger

func DefaultTimeFormat

func DefaultTimeFormat(t time.Time) []byte

DefaultTimeFormat 默认时间编码格式 2006-01-02 15:04:05.000

func GetLogEncoderKey

func GetLogEncoderKey(defKey, key string) string

GetLogEncoderKey gets user defined log output name, uses defKey if empty.

func NewTimeEncoder

func NewTimeEncoder(format string) zapcore.TimeEncoder

NewTimeEncoder 时间编码格式

func RegisterWriter

func RegisterWriter(name string, writer Writer)

RegisterWriter registers log output writer. Writer may have multiple implementations.

func Set

func Set(name string, logger Logger)

Set 设置自定义日志打印器

func Sync

func Sync()

Sync 同步所有日志

Types

type AsyncFileWriter

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

AsyncFileWriter 异步写文件,实现 zapcore.WriteSyncer 接口。

func NewAsyncFileWriter

func NewAsyncFileWriter(logger io.WriteCloser, dropLog bool) *AsyncFileWriter

NewAsyncFileWriter create a new AsyncFileWriter.

func (*AsyncFileWriter) Close

func (w *AsyncFileWriter) Close() error

Close 关闭当前写的日志,实现 io.Closer 接口。

func (*AsyncFileWriter) Sync

func (w *AsyncFileWriter) Sync() error

Sync 日志同步,实现 zapcore.WriteSyncer 接口。

func (*AsyncFileWriter) Write

func (w *AsyncFileWriter) Write(data []byte) (int, error)

Write 实现 io.Writer 接口。

type AsyncOptions

type AsyncOptions struct {
	QueueSize     int  // 日志队列大小,默认 10000
	WriteSize     int  // 日志写入阈值, 默认 16K
	WriteInterval int  // 异步写日志时间间隔,(单位 ms)默认 100ms
	DropLog       bool // 队列满的时候是否丢弃日志,默认 false
}

AsyncOptions 异步写文件日志配置

type Config

type Config struct {
	Writer string `yaml:"writer"` // 日志输出,例如 console 、file、esfile
	Level  string `yaml:"level"`  // 日志级别,例如 debug、info、warn、error、fatal

	Encoder       string        `yaml:"encoder"`        // 日志编码格式,比如 console、json
	EncoderConfig EncoderConfig `yaml:"encoder_config"` // 格式配置
	Field         []string      `yaml:"field"`          // 当采用 separator 的时候,fields 是按顺序提取的字段数据,各个数据用分隔符隔开。
	Escape        bool          `yaml:"escape"`         // 内容是否转义,性能原因默认关闭,true开启

	FileConfig       FileConfig `yaml:"file_config"`        // 文件日志配置
	ThirdPartyConfig yaml.Node  `yaml:"third_party_config"` // 第三方日志组件配置。它是由业务定义的,应该由第三方模块注册。
}

Config 日志输出配置,包括 console, file 和 third_party.

type ConsoleWriter

type ConsoleWriter struct{} // console writer

func (*ConsoleWriter) Setup

func (f *ConsoleWriter) Setup(cfg *Config) (zapcore.Core, error)

Setup 加载注册 ConsoleWriter

type EncoderConfig

type EncoderConfig struct {
	TimeFmt       string `yaml:"time_fmt"`       // 日志输出的时间格式,默认 "2006-01-02 15:04:05.000"
	TimeKey       string `yaml:"time_key"`       // 日志输出的 时间 key,默认 "time"
	LevelKey      string `yaml:"level_key"`      // 日志输出的 级别 key,默认 "level"
	MessageKey    string `yaml:"message_key"`    // 日志输出的 消息 key,默认 "msg"
	NameKey       string `yaml:"name_key"`       // 日志输出的 名字 key,默认 ""
	FunctionKey   string `yaml:"function_key"`   // 日志输出的 函数 key,默认"",表示不打印函数名
	CallerKey     string `yaml:"caller_key"`     // 日志输出的 caller key,默认 ""
	StacktraceKey string `yaml:"stacktrace_key"` // 日志输出的 stack trace key,默认 ""
}

EncoderConfig 编码配置

type Field

type Field struct {
	Key   string
	Value interface{}
}

type FileConfig

type FileConfig struct {
	LogPath    string `yaml:"log_path"`    // 日志路径,如 /usr/local/server/log/
	Filename   string `yaml:"filename"`    // 日志文件名,如 server.log
	WriteMode  int    `yaml:"write_mode"`  // 日志写入模式,1: sync, 2: async, 3: fast(队列满会丢弃日志).
	Compress   bool   `yaml:"compress"`    // 是否压缩
	LocalTime  bool   `yaml:"local_time"`  // 是否本地时间
	MaxDay     int    `yaml:"max_day"`     // 日志最大过期天数
	MaxBackups int    `yaml:"max_backups"` // 最大日志文件数
	MaxSize    int    `yaml:"max_size"`    // 本地文件滚动日志的大小 单位 MB
}

FileConfig 文件输出配置

type FileWriter

type FileWriter struct{} // file writer

func (*FileWriter) Setup

func (f *FileWriter) Setup(cfg *Config) (zapcore.Core, error)

Setup 加载注册 FileWriter

type Logger

type Logger interface {
	Debug(msg string, fields ...Field)
	Info(msg string, fields ...Field)
	Warn(msg string, fields ...Field)
	Error(msg string, fields ...Field)
	Fatal(msg string, fields ...Field) // 所有 Fatal 日志会调用 os.Exit(1) 函数退出系统

	// 系统日志,无 field 信息
	Debugf(msg string, args ...interface{})
	Infof(msg string, args ...interface{})
	Warnf(msg string, args ...interface{})
	Errorf(msg string, args ...interface{})
	Fatalf(msg string, args ...interface{})

	Sync() error //刷新所有缓冲的日志,系统必须在退出之前调用 Sync

	With(fields ...Field) Logger
}

Logger is the underlying logging work for framework.

var (
	DefaultLogger Logger
)

func Get

func Get(name string) Logger

Get 获取自定义日志打印器

type Writer

type Writer interface {
	Setup(cfg *Config) (zapcore.Core, error) // 根据配置setup writer
}

func GetWriter

func GetWriter(name string) Writer

GetWriter gets log output writer, returns nil if not exist.

Jump to

Keyboard shortcuts

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