Golang/Gin 学习

Golang-07 为gin添加logrus日志功能

2019-09-18  本文已影响0人  国服最坑开发
自定义输出格式
package cclog

import (
    "bytes"
    "fmt"
    "strings"

    "github.com/sirupsen/logrus"
)

type Formatter struct{}

func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
    var out string
    if entry.HasCaller() {
        var pkg bytes.Buffer
        for _, t := range strings.Split(entry.Caller.File, "/") {
                        if len(t) == 0 {
                           continue;
                         }
            pkg.WriteString(t[:1])
            pkg.WriteString(".")
        }
        pkg.WriteString(entry.Caller.Function)

        out = fmt.Sprintf("[%s][%s] %s(%d) %s\n", entry.Level.String()[:4], entry.Time.Format("2006-01-02 15:03:04.000"), pkg.String(), entry.Caller.Line, entry.Message)
    } else {
        out = fmt.Sprintf("[%s][%s] %s\n", entry.Level.String()[:4], entry.Time.Format("2006-01-02 15:03:04.000"), entry.Message)
    }
    return []byte(out), nil
}
配置类

import (
    "time"
    log "github.com/sirupsen/logrus"
    "gopkg.in/natefinch/lumberjack.v2"
    "hello_gin/cclog"
)

func init() {
    log.SetFormatter(new(cclog.Formatter))
    log.SetReportCaller(true)
    log.SetOutput(&lumberjack.Logger{
        Filename:   "./logs/api.log",
        MaxSize:    100, // megabytes
        MaxBackups: 3,
        MaxAge:     28,   // days
        Compress:   true, // disabled by default
    })
    // log.SetOutput(os.Stdout)
    log.SetLevel(log.DebugLevel)
}

func main() {
    r := gin.Default()
    r.GET("/app", handleApp)
    r.Run(":80")
}
func handleApp(c *gin.Context) {
    log.Info("good luck")
    c.JSON(200, gin.H{"code": 300, "msg": "OK"})
}

打印输出:

[info][2019-09-10 15:03:50.330] D.g.s.h.m.main.handleApp(67) good luck

上一篇下一篇

猜你喜欢

热点阅读