golang 日志模块

package main

import (
	"net/http"
	"github.com/natefinch/lumberjack"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

var sugarLogger *zap.SugaredLogger

func main() {
	InitLogger()
	defer sugarLogger.Sync()
	for i := 0;i<100000;i++{
		sugarLogger.Infof("Success! statusCode = 1000 for URL %s","https://www.baidu.com/s?wd=2020%E5%B9%B4%E5%A4%8D%E6%97%A6%E5%A4%A7%E5%AD%A6%E5%8C%BB%E5%AD%A6%E9%99%A2%20%E5%8D%8E%E5%B1%B1%E7%8F%AD&rsv_spt=1&rsv_iqid=0x97cb0fdf00018530&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=0&rsv_dl=tb&oq=2020%25E5%25B9%25B4%25E5%25A4%258D%25E6%2597%25A6%25E5%25A4%25A7%25E5%25AD%25A6%25E5%258C%25BB%25E5%25AD%25A6%25E9%2599%25A2%2520%25E6%259D%259C%25E5%2587%258C%25E5%25BF%2597&rsv_t=ef5de3jYLKy4aE9tpAWoRFLdeDgHxVhW4ItBARNBBmV5mcIZQhFXKc6rug%2BKatCtZ3Xj&rsv_btype=t&inputT=6564&rsv_pq=fc54ad7c000cc678&sug=2020%25E5%25B9%25B4%25E5%25A4%258D%25E6%2597%25A6%25E5%25A4%25A7%25E5%25AD%25A6&rsv_sug3=261&rsv_sug1=146&rsv_sug7=100&rsv_sug2=0&rsv_sug4=7270")
		sugarLogger.Infof("Success! statusCode = 1000 for URL %s","https://www.baidu.com/s?wd=2020%E5%B9%B4%E5%A4%8D%E6%97%A6%E5%A4%A7%E5%AD%A6%E5%8C%BB%E5%AD%A6%E9%99%A2%20%E5%8D%8E%E5%B1%B1%E7%8F%AD&rsv_spt=1&rsv_iqid=0x97cb0fdf00018530&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=0&rsv_dl=tb&oq=2020%25E5%25B9%25B4%25E5%25A4%258D%25E6%2597%25A6%25E5%25A4%25A7%25E5%25AD%25A6%25E5%258C%25BB%25E5%25AD%25A6%25E9%2599%25A2%2520%25E6%259D%259C%25E5%2587%258C%25E5%25BF%2597&rsv_t=ef5de3jYLKy4aE9tpAWoRFLdeDgHxVhW4ItBARNBBmV5mcIZQhFXKc6rug%2BKatCtZ3Xj&rsv_btype=t&inputT=6564&rsv_pq=fc54ad7c000cc678&sug=2020%25E5%25B9%25B4%25E5%25A4%258D%25E6%2597%25A6%25E5%25A4%25A7%25E5%25AD%25A6&rsv_sug3=261&rsv_sug1=146&rsv_sug7=100&rsv_sug2=0&rsv_sug4=7270")
		}
}

func InitLogger() {
	writeSyncer := getLogWriter()
	encoder := getEncoder()
	core := zapcore.NewCore(encoder, writeSyncer, zapcore.DebugLevel)

	logger := zap.New(core, zap.AddCaller())
	sugarLogger = logger.Sugar()
}

func getEncoder() zapcore.Encoder {
	encoderConfig := zap.NewProductionEncoderConfig()
	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
	encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
	return zapcore.NewConsoleEncoder(encoderConfig)
}

func getLogWriter() zapcore.WriteSyncer {
	lumberJackLogger := &lumberjack.Logger{
		Filename:   "./test.log",
		MaxSize:    10,
		MaxBackups: 50,
		MaxAge:     1,
		Compress:   true,
	}
	return zapcore.AddSync(lumberJackLogger)
}

Lumberjack Logger采用以下属性作为输入:

  • Filename: 日志文件的位置
  • MaxSize:在进行切割之前,日志文件的最大大小(以MB为单位)
  • MaxBackups:保留旧文件的最大个数
  • MaxAges:保留旧文件的最大天数
  • Compress:是否压缩/归档旧文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值