log

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

README

log

基于zap封装的log库

logrus 是 golang 一款非常优秀的日志框架, 其优点非常明显:

  • 优雅的代码框架设计
  • 使用简单
  • 组件化的开发思路
  • 灵活的输出方式

但是, 性能终究是忍痛舍弃 logrus 的“阿喀琉斯之踵” 目前 golang 日志库的大众选择主要集中在: logrus, zap, zerolog. zap 和 zerolog 的性能都是优秀的, 但是从用法习惯上我更倾向于 zap.

简单介绍 Zap 的使用

Zap 提供三种不同方式的输出(以 Info为 例)

log.Info("hello zap") // {"level":"info","ts":1576423173.016333,"caller":"test_zap/main.go:28","msg":"hello zap"}
log.Infof("hello %s", "zap") // {"level":"info","ts":1576423203.056074,"caller":"test_zap/main.go:29","msg":"hello zap"}
log.Infow("hello zap", "field1", "value1") //{"level":"info","ts":1576423203.0560799,"caller":"test_zap/main.go:30","msg":"hello zap","field1":"value1"}

如果我们对 logrus 的 key-value 理论比较在意的话, 使用 zap infow 可以完美解决

  • Zap 使用起来不便利的地方
  • Zap 使用上不能像 logrus 那样开箱即用
  • 使用者需要自己去组装相关函数
  • Zap 同样不提供日志切割的功能, 但是想添加上这个功能没有 logrus 那样便利

基于这些问题, 我封装了一套开箱即用的日志组件: https://github.com/yizhidaozuihou/log

打造 Zap 开箱即用日志组件 提供的功能:

  • 提供的功能: 像 logrus 一样, 全局的 Debug, Info ... 函数
  • 日志分割功能. 默认文件大小1024M,自动压缩, 最大有3个文件备份,备份保存时间7天, 不会打印日志被调用的文文件名和位置
  • 日志默认会被分成五类文件:debug、info、warn、error、panic 都会打印在xxx.log. xxx.log.Request输出 request log 的地方(如果有需要的话)

使用方法

 go get github.com/yizhidaozuihou/log

例子

package main

import "github.com/yizhidaozuihou/log"

func main() {
	// init log
	// set absolute path, and level
	// set output level
	// don't need request log
	// set log's caller using logOption
	log.Init("./test.log", log.DebugLevel, false, log.SetCaller(true))
	log.Info("hello george log")
	// flush
	log.Sync()
	//output: {"level":"info","ts":"2019-12-16T10:37:11.364+0800","caller":"example/example.go:12","msg":"hello george log"}
}

Documentation

Index

Constants

View Source
const (
	DebugLevel = "debug"
	InfoLevel  = "info"
	WarnLevel  = "warn"
	ErrorLevel = "error"
	PanicLevel = "panic"

	FileTypeLog     = "log"
	FileTypeRequest = "request_log"
)

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

Debug 使用方法:log.Debug("test")

func Debugf

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

Debugf 使用方法:log.Debugf("test:%s", err)

func Debugw

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

Debugw 使用方法:log.Debugw("test", "field1", "value1", "field2", "value2")

func Error

func Error(args ...interface{})

func Errorf

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

func Errorw

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

func Fatal

func Fatal(args ...interface{})

func Fatalf

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

func Fatalw

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

func Info

func Info(args ...interface{})

func Infof

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

func Infow

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

func Init

func Init(path, level string, needRequestLog bool, options ...LogOption)

Init init logger

func NewZapAdapter

func NewZapAdapter(path, level string) *zapAdapter

func Panic

func Panic(args ...interface{})

func Panicf

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

func Panicw

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

func RequestLogInfow

func RequestLogInfow(keysAndValues ...interface{})

func Sync

func Sync()

Sync flushes buffer, if any

func Warn

func Warn(args ...interface{})

func Warnf

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

func Warnw

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

Types

type Log

type Log struct {
	Path           string
	Level          string
	NeedRequestLog bool // 是否需要request.log
	// contains filtered or unexported fields
}

type LogOption

type LogOption interface {
	// contains filtered or unexported methods
}

func SetCaller

func SetCaller(caller bool) LogOption

func SetCompress

func SetCompress(compress bool) LogOption

func SetMaxAge

func SetMaxAge(age int) LogOption

func SetMaxBackups

func SetMaxBackups(n int) LogOption

func SetMaxFileSize

func SetMaxFileSize(size int) LogOption

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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