ginLogrus

package module
v0.0.0-...-6b8863d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

ginLogrus

不在维护

为了在gorm实现同样的功能,该日志插件全部重建,下面是新库的地址 https://github.com/justin-ren/xlogrushttps://gitee.com/JurenGo/xlogrus

介绍
  • golang的gin框架下基于logrus的插件。

  • 支持gin的middleware的access.log,请求访问日志自动记录。

  • 另外还有业务代码手动记录的trace.log。

  • 有颜色的日志信息打印到stdout,并用hook记录相同的无颜色日志到log文件。

  • 支持循环日志,并可设置保留日志的数量。

  • 另外,warn,error级别以上的日志会单独记录到error.log中。

  • 并为有日期后缀的活动日志生成相应的access.log,trace.log,error.log名字的链接,方便trail命令直接访问。

  • 输出如下: 输入图片说明

  • 日志格式如下:

$ ls -lrt
total 12
lrwxrwxrwx 1 renxiong renxiong  18 Jan  5 23:43 trace.log -> trace.log.20230105
lrwxrwxrwx 1 renxiong renxiong  16 Jan  5 23:44 error.log -> error.log.202301
-rw-r--r-- 1 renxiong renxiong 124 Jan  5 23:44 access.log.20230105
lrwxrwxrwx 1 renxiong renxiong  19 Jan  5 23:44 access.log -> access.log.20230105
-rw-r--r-- 1 renxiong renxiong 235 Jan  5 23:44 trace.log.20230105
-rw-r--r-- 1 renxiong renxiong  95 Jan  5 23:44 error.log.202301
$ cat access.log
[2023-01-05 23:44:16.265705]  INFO  clientIP=::1 dataLength=17 latency=789.116µs method=GET path=/log/index statusCode=200
$ cat trace.log
[2023-01-05 23:43:51.664605]  INFO enter main
[2023-01-05 23:44:16.264921] DEBUG enter index
[2023-01-05 23:44:16.265422]  INFO enter index
[2023-01-05 23:44:16.265465]  WARN enter index
[2023-01-05 23:44:20.024242] ERROR enter ignore
$ cat error.log
[2023-01-05 23:44:16.265465]  WARN enter index
[2023-01-05 23:44:20.024242] ERROR enter ignore
$ 


软件架构

依赖下面第三方插件

	"github.com/gin-gonic/gin"
	//日志切换插件,可以根据日志创建时间切换日志,还可以设置日志文件保存的数量,和为活动日志创建没有时间搓的file link
	rotateLogs "github.com/lestrrat-go/file-rotatelogs"
	//出错时记录call stack 输出格式 %+v
	"github.com/pkg/errors"
	//logrus的钩子插件,可以根据loglevel记录到不同的文件
	fileLogHook "github.com/rifflock/lfshook"
	//logrus日志插件
	"github.com/sirupsen/logrus"
	//设置日志文件头格式:时间格式,颜色格式,可以实现stdout有颜色,log file无颜色
	prefixFormatter "github.com/x-cray/logrus-prefixed-formatter"
安装教程
go get -u -t gitee.com/JurenGo/ginLogrus
使用说明
/**
 * @project testGinLogrus
 * @author JurenGo
 * @desc ginLogrus usage
 * @date 10:07 AM 1/1/23
 **/

package main

import (
	"gitee.com/JurenGo/ginLogrus"
	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
	"net/http"
)

var (
	//gin的middleware, 写入access.log, gin自动调用
	aLog gin.HandlerFunc
	//业务代码的trace.log,手工调用
	tLog *logrus.Logger
)

func init() {
	accessConfig := &ginLogrus.LogAccessConfig{
		SkipRoute: map[string]struct{}{
			"/log/ignore": struct{}{},
		},
		StdTimeFormat:  "06/01/02 15:04:05",
		LogPath:        "./logs/",
		FilePrefix:     "access.log",
		FileSuffix:     "%Y%m%d",
		IsErrFileHook:  true,
		KeepCount:      7,
		FileTimeFormat: "2006-01-02 15:04:05.000000",
	}
	traceConfig := &ginLogrus.LogTraceConfig{
		StdTimeFormat:  "06/01/02 15:04:05",
		LogPath:        "./logs/",
		FilePrefix:     "trace.log",
		FileSuffix:     "%Y%m%d",
		IsErrFileHook:  true,
		KeepCount:      7,
		FileTimeFormat: "2006-01-02 15:04:05.000000",
	}
	tLog = ginLogrus.NewTraceLog(traceConfig)
	aLog = ginLogrus.NewAccessLog(accessConfig)
}
func main() {

	tLog.Info("enter main")
	r := gin.New()
	r.Use(aLog, gin.Recovery())
	rLog := r.Group("log")
	rLog.GET("/index", indexAction)
	rLog.GET("/ignore", ignoreAction)
	err := r.Run(":8080")
	if err != nil {
		panic(errors.Cause(err))
		return
	}
}

func indexAction(ctx *gin.Context) {
	tLog.Debugln("enter index")
	tLog.Infoln("enter index")
	tLog.Warnln("enter index")
	ctx.JSON(http.StatusOK, gin.H{"msg": "success"})
}

func ignoreAction(ctx *gin.Context) {
	tLog.Errorln("enter ignore")
	ctx.JSON(http.StatusOK, gin.H{"msg": "success"})
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAccessLog

func NewAccessLog(logConfig *LogAccessConfig) gin.HandlerFunc

NewAccessLog

  • @msg gin的日志middleware,gin系统自动调用
  • @param logConfig *LogAccessConfig
  • @return: gin.HandlerFunc //标准gin middleware返回结构体

func NewTraceLog

func NewTraceLog(logConfig *LogTraceConfig) *logrus.Logger

Types

type LogAccessConfig

type LogAccessConfig struct {
	StdTimeFormat  string
	FileTimeFormat string
	LogPath        string
	FilePrefix     string
	FileSuffix     string
	SkipRoute      map[string]struct{}
	IsErrFileHook  bool
	KeepCount      int
}

LogAccessConfig

  • @msg access.log config info

type LogTraceConfig

type LogTraceConfig struct {
	StdTimeFormat  string
	FileTimeFormat string
	LogPath        string
	FilePrefix     string
	FileSuffix     string
	IsErrFileHook  bool
	KeepCount      int
}

LogTraceConfig

  • @msg trace log config info

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL