Feat:add logger
This commit is contained in:
parent
6d14ddee7f
commit
2d5ebb64d3
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
.idea
|
||||
build/
|
||||
logs/
|
||||
|
||||
.env
|
||||
1
go.mod
1
go.mod
@ -6,6 +6,7 @@ require (
|
||||
github.com/gin-gonic/gin v1.5.0
|
||||
github.com/gomodule/redigo v2.0.0+incompatible
|
||||
github.com/kr/pretty v0.1.0 // indirect
|
||||
github.com/sirupsen/logrus v1.6.0
|
||||
golang.org/x/sys v0.0.0-20191010194322-b09406accb47 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
|
||||
gopkg.in/yaml.v2 v2.2.4 // indirect
|
||||
|
||||
6
go.sum
6
go.sum
@ -16,6 +16,8 @@ github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo=
|
||||
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
@ -31,7 +33,10 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLD
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
|
||||
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
@ -39,6 +44,7 @@ github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
|
||||
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
|
||||
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
|
||||
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
|
||||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY=
|
||||
|
||||
99
main.go
99
main.go
@ -6,9 +6,12 @@ import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gomodule/redigo/redis"
|
||||
"github.com/sirupsen/logrus"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -47,6 +50,10 @@ var redisClient redis.Conn
|
||||
func main() {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
router := gin.Default()
|
||||
|
||||
// Log 收集中间件
|
||||
router.Use(LoggerToFile())
|
||||
|
||||
router.LoadHTMLGlob("public/*.html")
|
||||
|
||||
port := flag.Int("port", defaultPort, "服务端口")
|
||||
@ -144,6 +151,98 @@ func main() {
|
||||
router.Run(fmt.Sprintf(":%d", *port))
|
||||
}
|
||||
|
||||
func Logger() *logrus.Logger {
|
||||
now := time.Now()
|
||||
logFilePath := ""
|
||||
if dir, err := os.Getwd(); err == nil {
|
||||
logFilePath = dir + "/logs/"
|
||||
}
|
||||
if err := os.MkdirAll(logFilePath, 0777); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
logFileName := now.Format("2006-01-02") + ".log"
|
||||
|
||||
//日志文件
|
||||
fileName := path.Join(logFilePath, logFileName)
|
||||
if _, err := os.Stat(fileName); err != nil {
|
||||
if _, err := os.Create(fileName); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
//写入文件
|
||||
src, err := os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
|
||||
if err != nil {
|
||||
fmt.Println("err", err)
|
||||
}
|
||||
|
||||
//实例化
|
||||
logger := logrus.New()
|
||||
|
||||
//设置输出
|
||||
logger.SetOutput(src)
|
||||
// logger.Out = src
|
||||
|
||||
//设置日志级别
|
||||
logger.SetLevel(logrus.DebugLevel)
|
||||
|
||||
//设置日志格式
|
||||
logger.Formatter = &logrus.JSONFormatter{}
|
||||
|
||||
return logger
|
||||
}
|
||||
|
||||
func LoggerToFile() gin.HandlerFunc {
|
||||
logger := Logger()
|
||||
return func(c *gin.Context) {
|
||||
logMap := make(map[string]interface{})
|
||||
|
||||
// 开始时间
|
||||
startTime := time.Now()
|
||||
logMap["startTime"] = startTime.Format("2006-01-02 15:04:05")
|
||||
|
||||
// 处理请求
|
||||
c.Next()
|
||||
|
||||
// 结束时间
|
||||
endTime := time.Now()
|
||||
logMap["endTime"] = endTime.Format("2006-01-02 15:04:05")
|
||||
|
||||
// 执行时间
|
||||
logMap["latencyTime"] = endTime.Sub(startTime).Microseconds()
|
||||
|
||||
// 请求方式
|
||||
logMap["reqMethod"] = c.Request.Method
|
||||
|
||||
// 请求路由
|
||||
logMap["reqUri"] = c.Request.RequestURI
|
||||
|
||||
// 状态码
|
||||
logMap["statusCode"] = c.Writer.Status()
|
||||
|
||||
// 请求IP
|
||||
logMap["clientIP"] = c.ClientIP()
|
||||
|
||||
// 请求 UA
|
||||
logMap["clientUA"] = c.Request.UserAgent()
|
||||
|
||||
//日志格式
|
||||
// logJson, _ := json.Marshal(logMap)
|
||||
// logger.Info(string(logJson))
|
||||
|
||||
logger.WithFields(logrus.Fields{
|
||||
"startTime": logMap["startTime"],
|
||||
"endTime": logMap["endTime"],
|
||||
"latencyTime": logMap["latencyTime"],
|
||||
"reqMethod": logMap["reqMethod"],
|
||||
"reqUri": logMap["reqUri"],
|
||||
"statusCode": logMap["statusCode"],
|
||||
"clientIP": logMap["clientIP"],
|
||||
"clientUA": logMap["clientUA"],
|
||||
}).Info()
|
||||
}
|
||||
}
|
||||
|
||||
// 短链接转长链接
|
||||
func shortToLong(shortKey string) string {
|
||||
redisClient = redisPool.Get()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user