一,安装用到的库:
1,安装zap日志库:
liuhongdi@ku:/data/liuhongdi/zaplog$ go get -u go.uber.org/zap
2,安装go-file-rotatelogs库
liuhongdi@ku:/data/liuhongdi/zaplog2$ go get -u github.com/lestrrat/go-file-rotatelogs
说明:刘宏缔的go森林是一个专注golang的博客, 地址:https://blog.csdn.net/weixin_43881017
说明:作者:刘宏缔 邮箱: [email protected]
二,演示项目的相关信息:
1,项目地址:
https://github.com/liuhongdi/digv06
2,功能说明:记录http服务的访问日志access log
3,项目结构:如图:
三,go代码说明
1,config/config.yaml
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
LogFilePath: /data/gologs/logs
-
-
-
-
-
LogFilePath: /data/gologs/logs
-
-
2,global/setting.go
-
-
-
-
-
"github.com/liuhongdi/digv06/pkg/setting"
-
-
-
-
type ServerSettingS struct {
-
-
-
ReadTimeout time.Duration
-
WriteTimeout time.Duration
-
-
-
type DatabaseSettingS struct {
-
-
-
-
-
-
-
-
-
-
-
-
type LogSettingS struct {
-
-
-
-
-
-
-
-
type AccessLogSettingS struct {
-
-
-
-
-
-
-
ServerSetting *ServerSettingS
-
DatabaseSetting *DatabaseSettingS
-
-
AccessLogSetting *AccessLogSettingS
-
-
-
-
func SetupSetting() error {
-
s, err := setting.NewSetting()
-
-
-
-
err = s.ReadSection("Database", &DatabaseSetting)
-
-
-
-
-
err = s.ReadSection("Server", &ServerSetting)
-
-
-
-
-
err = s.ReadSection("Log", &LogSetting)
-
-
-
-
-
err = s.ReadSection("AccessLog", &AccessLogSetting)
-
-
-
-
-
-
fmt.Println(ServerSetting)
-
fmt.Println(DatabaseSetting)
-
-
fmt.Println(AccessLogSetting)
-
-
3,global/accessLog.go
-
-
-
-
-
"github.com/liuhongdi/digv06/pkg/setting"
-
-
-
-
type ServerSettingS struct {
-
-
-
ReadTimeout time.Duration
-
WriteTimeout time.Duration
-
-
-
type DatabaseSettingS struct {
-
-
-
-
-
-
-
-
-
-
-
-
type LogSettingS struct {
-
-
-
-
-
-
-
-
type AccessLogSettingS struct {
-
-
-
-
-
-
-
ServerSetting *ServerSettingS
-
DatabaseSetting *DatabaseSettingS
-
-
AccessLogSetting *AccessLogSettingS
-
-
-
-
func SetupSetting() error {
-
s, err := setting.NewSetting()
-
-
-
-
err = s.ReadSection("Database", &DatabaseSetting)
-
-
-
-
-
err = s.ReadSection("Server", &ServerSetting)
-
-
-
-
-
err = s.ReadSection("Log", &LogSetting)
-
-
-
-
-
err = s.ReadSection("AccessLog", &AccessLogSetting)
-
-
-
-
-
-
fmt.Println(ServerSetting)
-
fmt.Println(DatabaseSetting)
-
-
fmt.Println(AccessLogSetting)
-
-
4,middelware/accesslog.go
-
-
-
-
-
"github.com/liuhongdi/digv06/global"
-
"github.com/liuhongdi/digv06/pkg/util"
-
-
"github.com/gin-gonic/gin"
-
-
-
type AccessLogWriter struct {
-
-
-
-
-
func (w AccessLogWriter) Write(p []byte) (int, error) {
-
if n, err := w.body.Write(p); err != nil {
-
-
-
return w.ResponseWriter.Write(p)
-
-
-
func AccessLog() gin.HandlerFunc {
-
return func(c *gin.Context) {
-
bodyWriter := &AccessLogWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer}
-
-
-
beginTime := time.Now().UnixNano()
-
-
endTime := time.Now().UnixNano()
-
duration:=endTime-beginTime
-
-
-
-
-
-
layout := "2006-01-02 15:04:05"
-
timeNow := time.Now().Format(layout)
-
-
global.AccessLogger.Infof(s,
-
-
-
-
-
-
-
-
-
c.Request.Header.Get("User-Agent"),
-
-
-
-
5,pkg/zaplog/zaplog.go
-
-
-
-
rotatelogs "github.com/lestrrat/go-file-rotatelogs"
-
-
"go.uber.org/zap/zapcore"
-
-
-
-
-
-
func GetInitLogger(filepath,infofilename,warnfilename,fileext string) (*zap.SugaredLogger,error) {
-
-
-
-
infoLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
-
return lvl < zapcore.WarnLevel
-
-
-
warnLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
-
return lvl >= zapcore.WarnLevel
-
-
-
infoWriter,err := getLogWriter(filepath+"/"+infofilename,fileext)
-
-
-
-
warnWriter,err2 := getLogWriter(filepath+"/"+warnfilename,fileext)
-
-
-
-
-
-
-
zapcore.NewCore(encoder, infoWriter, infoLevel),
-
zapcore.NewCore(encoder, warnWriter, warnLevel),
-
-
loggerres := zap.New(core, zap.AddCaller())
-
-
return loggerres.Sugar(),nil
-
-
-
-
func GetInitAccessLogger(filepath,filename,fileext string) (*zap.SugaredLogger,error) {
-
-
warnWriter,err2 := getLogWriter(filepath+"/"+filename,fileext)
-
-
-
-
-
-
-
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
-
-
-
EncoderConfig: zapcore.EncoderConfig{
-
-
-
OutputPaths: []string{"stdout", "./log.txt"},
-
ErrorOutputPaths: []string{"stderr"},
-
-
-
l, err := cfg.Build(SetOutput(warnWriter, cfg))
-
-
-
-
-
-
-
-
-
func SetOutput(ws zapcore.WriteSyncer, conf zap.Config) zap.Option {
-
-
-
-
enc = zapcore.NewJSONEncoder(conf.EncoderConfig)
-
-
enc = zapcore.NewConsoleEncoder(conf.EncoderConfig)
-
-
panic("unknown encoding")
-
-
return zap.WrapCore(func(core zapcore.Core) zapcore.Core {
-
return zapcore.NewCore(enc, ws, conf.Level)
-
-
-
-
-
-
func getEncoder() zapcore.Encoder {
-
encoderConfig := zap.NewProductionEncoderConfig()
-
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
-
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
-
return zapcore.NewConsoleEncoder(encoderConfig)
-
-
-
-
func getLogWriter(filePath,fileext string) (zapcore.WriteSyncer,error) {
-
warnIoWriter,err := getWriter(filePath,fileext)
-
-
-
-
return zapcore.AddSync(warnIoWriter),nil
-
-
-
-
func getWriter(filename,fileext string) (io.Writer,error) {
-
-
hook, err := rotatelogs.New(
-
filename+"_%Y%m%d."+fileext,
-
rotatelogs.WithLinkName(filename),
-
rotatelogs.WithMaxAge(time.Hour*24*30),
-
rotatelogs.WithRotationTime(time.Hour*24),
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
6,router/router.go
-
-
-
-
"github.com/gin-gonic/gin"
-
"github.com/liuhongdi/digv06/controller"
-
"github.com/liuhongdi/digv06/global"
-
"github.com/liuhongdi/digv06/middleware"
-
"github.com/liuhongdi/digv06/pkg/result"
-
-
-
-
func Router() *gin.Engine {
-
-
-
router.NoRoute(HandleNotFound)
|
请发表评论