本文介绍了 logrus 日志库和与 iris web 框架的结合。

logrus特性

JSONFormatterTextFormatterFormatter

logrus使用

简单示例

package main

import (
  log "github.com/sirupsen/logrus"
)

func main() {
  log.WithFields(log.Fields{
    "animal": "walrus",
  }).Info("A walrus appears")
}

输出结果:

time="2018-08-11T15:42:22+08:00" level=info msg="A walrus appears" animal=walrus

简单配置

package main

import (
    "os"
    log "github.com/sirupsen/logrus"
)

func init() {
    // 设置日志格式为json格式
    log.SetFormatter(&log.JSONFormatter{})

    // 设置将日志输出到标准输出(默认的输出为stderr,标准错误)
    // 日志消息输出可以是任意的io.writer类型
    log.SetOutput(os.Stdout)

    // 设置日志级别为warn以上
    log.SetLevel(log.WarnLevel)
}

func main() {
    log.WithFields(log.Fields{
        "animal": "walrus",
        "size":   10,
    }).Info("A group of walrus emerges from the ocean")

    log.WithFields(log.Fields{
        "omg":    true,
        "number": 122,
    }).Warn("The group's number increased tremendously!")

    log.WithFields(log.Fields{
        "omg":    true,
        "number": 100,
    }).Fatal("The ice breaks!")
}

Logger

logger
package main

import (
    "github.com/sirupsen/logrus"
    "os"
)

// logrus提供了New()函数来创建一个logrus的实例。
// 项目中,可以创建任意数量的logrus实例。
var log = logrus.New()

func main() {
    // 为当前logrus实例设置消息的输出,同样地,
    // 可以设置logrus实例的输出到任意io.writer
    log.Out = os.Stdout

    // 为当前logrus实例设置消息输出格式为json格式。
    // 同样地,也可以单独为某个logrus实例设置日志级别和hook,这里不详细叙述。
    log.Formatter = &logrus.JSONFormatter{}

    log.WithFields(logrus.Fields{
        "animal": "walrus",
        "size":   10,
    }).Info("A group of walrus emerges from the ocean")
}

Fields

Fields
log.WithFields(log.Fields{
  "event": event,
  "topic": topic,
  "key": key,
}).Fatal("Failed to send event")
WithFieldsWithFields
Fieldrequest_iduser_iplog.WithFields(log.Fields{"request_id": request_id, "user_ip": user_ip})logrus.EntryFieldslogrus.Entry
requestLogger := log.WithFields(log.Fields{"request_id": request_id, "user_ip": user_ip})
requestLogger.Info("something happened on that request") # will log request_id and user_ip
requestLogger.Warn("something not great happened")

日志本地文件分割

file-rotatelogsfile-rotatelogs
import (
    "github.com/lestrrat-go/file-rotatelogs"
    "github.com/rifflock/lfshook"
    log "github.com/sirupsen/logrus"
    "time"
)

func newLfsHook(logLevel *string, maxRemainCnt uint) log.Hook {
    writer, err := rotatelogs.New(
        logName+".%Y%m%d%H",
        // WithLinkName为最新的日志建立软连接,以方便随着找到当前日志文件
        rotatelogs.WithLinkName(logName),

        // WithRotationTime设置日志分割的时间,这里设置为一小时分割一次
        rotatelogs.WithRotationTime(time.Hour),

        // WithMaxAge和WithRotationCount二者只能设置一个,
        // WithMaxAge设置文件清理前的最长保存时间,
        // WithRotationCount设置文件清理前最多保存的个数。
        //rotatelogs.WithMaxAge(time.Hour*24),
        rotatelogs.WithRotationCount(maxRemainCnt),
    )

    if err != nil {
        log.Errorf("config local file system for logger error: %v", err)
    }

    level, ok := logLevels[*logLevel]

    if ok {
        log.SetLevel(level)
    } else {
        log.SetLevel(log.WarnLevel)
    }

    lfsHook := lfshook.NewHook(lfshook.WriterMap{
        log.DebugLevel: writer,
        log.InfoLevel:  writer,
        log.WarnLevel:  writer,
        log.ErrorLevel: writer,
        log.FatalLevel: writer,
        log.PanicLevel: writer,
    }, &log.TextFormatter{DisableColors: true})

    return lfsHook
}

结合IRIS使用

先初始化一个Logger出来

config/config.go
type LogConfig struct {
 Level string `yaml:"level"`
 Path  string `yaml:"path"`
 Save  uint   `yaml:"save"`
}

config/logger.go` 根据配置的日志级别,日志路径,日志保留天数初始化一个Logger

package config

import (
 "github.com/lestrrat-go/file-rotatelogs"
 "github.com/rifflock/lfshook"
 "github.com/sirupsen/logrus"
 "os"
 "path"
 "time"
)

var (
 Log = logrus.New()
)

func initLog(logConfig LogConfig) {
 Log.Out = os.Stdout
 var loglevel logrus.Level
 err := loglevel.UnmarshalText([]byte(logConfig.Level))
 if err != nil {
  Log.Panicf("设置log级别失败:%v", err)
 }
 Log.SetLevel(loglevel)
 Log.Formatter = &logrus.TextFormatter{}
 LocalFilesystemLogger(Log, logConfig.Path, logConfig.Save)
 //Log.ReportCaller = true
}

func logWriter(logPath string, level string, save uint) *rotatelogs.RotateLogs {
 logFullPath := path.Join(logPath, level)
 logwriter, err := rotatelogs.New(
  logFullPath+".%Y%m%d",
  rotatelogs.WithLinkName(logFullPath),      // 生成软链,指向最新日志文件
  rotatelogs.WithRotationCount(save),        // 文件最大保存份数
  rotatelogs.WithRotationTime(24*time.Hour), // 日志切割时间间隔
 )
 if err != nil {
  panic(err)
 }
 return logwriter
}

func LocalFilesystemLogger(log *logrus.Logger, logPath string, save uint) {
 lfHook := lfshook.NewHook(lfshook.WriterMap{
  logrus.DebugLevel: logWriter(logPath, "debug", save), // 为不同级别设置不同的输出目的
  logrus.InfoLevel:  logWriter(logPath, "info", save),
  logrus.WarnLevel:  logWriter(logPath, "warn", save),
  logrus.ErrorLevel: logWriter(logPath, "error", save),
  logrus.FatalLevel: logWriter(logPath, "fatal", save),
  logrus.PanicLevel: logWriter(logPath, "panic", save),
 }, &logrus.JSONFormatter{})
 log.AddHook(lfHook)
}
middleware/logger_middleware.go
package middleware

import (
 "bytes"
 "github.com/dgrijalva/jwt-go"
 "github.com/kataras/iris/v12"
 "goms/config"
 "io/ioutil"
 "net/http"
 "path"
 "time"
)

func LoggerHandler(ctx iris.Context) {
 p := ctx.Request().URL.Path
 method := ctx.Request().Method
 start := time.Now()
 fields := make(map[string]interface{})
 fields["title"] = "访问日志"
 fields["fun_name"] = path.Join(method, p)
 fields["ip"] = ctx.Request().RemoteAddr
 fields["method"] = method
 fields["url"] = ctx.Request().URL.String()
 fields["proto"] = ctx.Request().Proto
 //fields["header"] = ctx.Request().Header
 fields["user_agent"] = ctx.Request().UserAgent()
 fields["x_request_id"] = ctx.GetHeader("X-Request-Id")

 // 如果是POST/PUT请求,并且内容类型为JSON,则读取内容体
 if method == http.MethodPost || method == http.MethodPut || method == http.MethodPatch {
  body, err := ioutil.ReadAll(ctx.Request().Body)
  if err == nil {
   defer ctx.Request().Body.Close()
   buf := bytes.NewBuffer(body)
   ctx.Request().Body = ioutil.NopCloser(buf)
   fields["content_length"] = ctx.GetContentLength()
   fields["body"] = string(body)
  }
 }
 ctx.Next()

 //下面是返回日志
 fields["res_status"] = ctx.ResponseWriter().StatusCode()
 if ctx.Values().GetString("out_err") != "" {
  fields["out_err"] = ctx.Values().GetString("out_err")
 }
 fields["res_length"] = ctx.ResponseWriter().Header().Get("size")
 if v := ctx.Values().Get("res_body"); v != nil {
  if b, ok := v.([]byte); ok {
   fields["res_body"] = string(b)
  }
 }
 token := ctx.Values().Get("jwt")
 if token != nil {
  fields["uid"] = token.(*jwt.Token).Claims
 }
 timeConsuming := time.Since(start).Nanoseconds() / 1e6
 config.Log.WithFields(fields).Infof("[http] %s-%s-%s-%d(%dms)",
  p, ctx.Request().Method, ctx.Request().RemoteAddr, ctx.ResponseWriter().StatusCode(), timeConsuming)
}
route/route.go
package route

import (
 "github.com/kataras/iris/v12"
 "github.com/kataras/iris/v12/mvc"
 "goms/controllers"
 "goms/middleware"
)

func InitRoute(app *iris.Application) {
 app.Use(middleware.LoggerHandler)

 mvc.Configure(app.Party("/account"), func(m *mvc.Application) {
  m.Handle(controllers.NewLoginController())
 })
    
    ......
}

最后感觉还是不咋好用,用Json格式不太好看,用Text格式又没有时间,再寻找一下别的Log库看看!