做好每一件事,读好每一本书,天道酬勤
使用zap日志
2022-02-12 / 1 min read

使用zap日志

将日志打印到文件里面

package main

import (
	"go.uber.org/zap"
	"time"
)

func NewLogger() (*zap.Logger, error) {
	ctg := zap.NewProductionConfig()
	ctg.OutputPaths = []string{
		"./myproject.log",
	}
	return ctg.Build()
}
func main() {
	looger, err := NewLogger()
	if err != nil {
		panic(err)
	}
	sugar := looger.Sugar()
	defer sugar.Sync()
	url := "https://imooc.com"
	sugar.Info("failed to fetch URL",
		// Structured context as loosely typed key-value pairs.
		"url", url,
		"attempt", 3,
		"backoff", time.Second,
	)
}