zap_wrap

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2020 License: MIT Imports: 11 Imported by: 0

README

zap-wrap

zap-wrap intergrates zap and log rotate, make zap easier to use

zap is excellent, but its configuration is too complicated. Though zap provides a default configuration, it's not enough for most projects.

use case

If you:

  • want to rotate the log file by time
  • want clear old log files routinely
  • want to write normal log to access.log and error info to error.log, just like nginx

this is for you!

quick start
[
  {
    "log_dir": "/home/deen/test",
    "filename": "access.log",
    "suffix": "%Y%m%d%H",
    "rotate_duration": "1h",
    "retain_age": "168h",
    "soft_link": true,
    "level": "debug"
  },
  {
    "log_dir": "/home/deen/test",
    "filename": "error.log",
    "suffix": "%Y%m%d%H",
    "rotate_duration": "1h",
    "retain_age": "168h",
    "soft_link": true,
    "level": "warn"
  }
]

config above means:

  • rotate once an hour
  • old log file will be named as access.log.2019121420
    • %Y: year
    • %m: Month
    • %d: day
    • %H: hour
    • %M: minute
  • clear old log files which exist over 168 hours
  • create symbol link named access.log to access.log.{current}, so that you can find the current log right away
  • level >= warn -> error.log
  • level >= debug && level < warn -> access.log

then

func main() {
	var cfgs []zap_wrap.FileConfig
	_ = json.Unmarshal(jsonConfig, &cfgs)

        // So easy
	logger, _ := zap_wrap.NewLogger(cfgs...)

	logger.Debug("123", zap.Int("rand", rand.Intn(30)))
        // In access.log: {"level":"debug","time":"2019-12-14T19:52:56+08:00","caller":"triple/main.go:28","msg":"123","rand":20}

	logger.Error("ttt", zap.Int("rand", rand.Intn(30)))
        // In error.log: {"level":"warn","time":"2019-12-14T19:52:56+08:00","caller":"triple/main.go:28","msg":"ttt","rand":5}

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(addCaller bool, cfgs ...FileConfig) (*zap.Logger, error)

NewLogger combines multiple writers and creates an uniform zap.Logger

Types

type Duration

type Duration time.Duration

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) error

type FileConfig

type FileConfig struct {
	// LogDir absolute dir path
	LogDir string `json:"log_dir"`
	// FileName such as access.log etc.
	FileName string `json:"filename"`
	// Suffix if set "%Y%m%d%H%M"(year month day hour minute), the rotated file
	// will be named as "filename.201912141922".
	Suffix string `json:"suffix"`
	// RotatePeriod time period to rotate the file
	RotatePeriod Duration `json:"rotate_duration,omitempty"`
	// RetainMaxAge, this will purge old files those whom exceeds this duration
	RetainMaxAge Duration `json:"retain_age,omitempty"`
	// SoftLink if set softlink to current log file, so you can always tail the same file
	SoftLink bool `json:"soft_link"`
	// Level, set log level
	Level LogLevel `json:"level"`
	// Caller, set if log caller info
	Caller bool `json:"caller"`
	// EncodeCfg, ignore this if you don't know about it. This lib provides a default config that may meet most
	// users' requirement
	EncodeCfg *zapcore.EncoderConfig
}

type LogLevel

type LogLevel struct {
	zapcore.Level
}

func (*LogLevel) UnmarshalJSON

func (l *LogLevel) UnmarshalJSON(data []byte) error

Directories

Path Synopsis
exapmple

Jump to

Keyboard shortcuts

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