log

package
v0.6.9 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2022 License: BSD-3-Clause Imports: 11 Imported by: 0

README

日志API设计

日志API需求
  1. 日志分级;
  2. 日志存档(文件,ELK等);
  3. 日志API
日志API实现
  1. log包提供一个对外部稳定的API,外部不必关注包内部是如何实现(内部目前使用uber zap);
  2. 业务package大部分场合只需要import log即可使用,不必在new其它对象;
  3. 通过不同的API名称实现日志分级
接口 作用
log.Setup() 设置参数
log.Debugf() 调试日志
log.Infof() 信息日志
log.Warnf() 警告日志
log.Errorf() 错误日志
log.Fatalf() 错误日志,程序不能继续执行

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppFileErrorLog

func AppFileErrorLog(format string, a ...interface{}) error

func Debugf

func Debugf(format string, args ...interface{})

func Debugw

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

func Errorf

func Errorf(format string, args ...interface{})

func Errorw

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

func Fatalf

func Fatalf(format string, args ...interface{})

func Fatalw

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

func HookSystemLog

func HookSystemLog(args map[string]string) zap.Option

func Infof

func Infof(format string, args ...interface{})

func Infow

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

func Logger added in v0.2.3

func Logger() *zap.Logger

func NewLogger

func NewLogger(cfg Config) *zap.Logger

func Panicf

func Panicf(format string, args ...interface{})

func SetLogger added in v0.3.3

func SetLogger(l *zap.Logger)

func SetupLogger added in v0.3.3

func SetupLogger(cfg Config)

func Sugared added in v0.2.3

func Sugared() *zap.SugaredLogger

func Warnf

func Warnf(format string, args ...interface{})

func Warnw

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

func WriteFileLog

func WriteFileLog(filename, format string, a ...interface{}) error

Types

type Config

type Config struct {
	// Level is the minimum enabled logging level. Note that this is a dynamic
	// level, so calling Config.Level.SetLevel will atomically change the log
	// level of all loggers descended from this config.
	Level string `json:"level" yaml:"level"`

	// Development puts the logger in development mode, which changes the
	// behavior of DPanicLevel and takes stacktraces more liberally.
	Development bool `json:"development" yaml:"development"`

	// Encoding sets the logger's encoding. Valid values are "json" and "console"
	Encoding string `json:"encoding" yaml:"encoding"`

	// Filename is the file to write logs to.  Backup log files will be retained
	// in the same directory.  It uses <processname>-lumberjack.log in
	// os.TempDir() if empty.
	Filename string `json:"filename" yaml:"filename"`

	// ErrorFilename is the file to write error logs to.  Backup log files will be retained
	// in the same directory.
	ErrorFilename string `json:"error_filename" yaml:"error_filename"`

	// MaxSize is the maximum size in megabytes of the log file before it gets
	// rotated. It defaults to 100 megabytes.
	MaxSize int `json:"max_size" yaml:"max_size"`

	// MaxAge is the maximum number of days to retain old log files based on the
	// timestamp encoded in their filename.  Note that a day is defined as 24
	// hours and may not exactly correspond to calendar days due to daylight
	// savings, leap seconds, etc. The default is not to remove old log files
	// based on age.
	MaxAge int `json:"max_age" yaml:"max_age"`

	// MaxBackups is the maximum number of old log files to retain.  The default
	// is to retain all old log files (though MaxAge may still cause them to get
	// deleted.)
	MaxBackups int `json:"max_backups" yaml:"max_backups"`

	// LocalTime determines if the time used for formatting the timestamps in
	// backup files is the computer's local time.  The default is to use UTC
	// time.
	LocalTime bool `json:"localtime" yaml:"localtime"`

	// Compress determines if the rotated log files should be compressed
	// using gzip. The default is not to perform compression.
	Compress bool `json:"compress" yaml:"compress"`
}

配置参数

func NewConfig

func NewConfig() Config

func (*Config) Build

func (c *Config) Build() *zap.Logger

func (*Config) CreateEncoder added in v0.3.3

func (c *Config) CreateEncoder() zapcore.Encoder

func (*Config) CreateLogCore added in v0.3.3

func (c *Config) CreateLogCore() zapcore.Core

func (*Config) CreateLogWriter added in v0.3.3

func (c *Config) CreateLogWriter(filename, console string) *Writer

type Hooker

type Hooker interface {
	Name() string
	Fire(entry zapcore.Entry) error
}

func NewSyslogHook

func NewSyslogHook(network, raddr string, priority syslog.Priority, tag string) Hooker

Creates a hook to be added to an instance of logger. This is called with `hook, err := NewSyslogHook("udp", "localhost:514", syslog.LOG_DEBUG, "")`

type SyslogHook

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

SyslogHook to send logs via syslog.

func (*SyslogHook) Fire

func (h *SyslogHook) Fire(entry zapcore.Entry) error

func (*SyslogHook) Name

func (h *SyslogHook) Name() string

type Writer added in v0.3.3

type Writer struct {
	lumberjack.Logger
	// contains filtered or unexported fields
}

func NewWriter added in v0.3.3

func NewWriter(filename, console string, maxSize, maxBackup, maxAge int, localtime, compress bool) *Writer

func (*Writer) Write added in v0.3.3

func (w *Writer) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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