Skip to content

Commit

Permalink
Merge pull request #9 from OTOT-dev/log
Browse files Browse the repository at this point in the history
feat: 增加日志收集
  • Loading branch information
afzw authored Mar 24, 2024
2 parents a036851 + f79106a commit 8955704
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ docs

# other
**/*.DS_Store
log/
81 changes: 81 additions & 0 deletions common/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package common

import (
"bytes"
"fmt"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
"path"
)

var Log = logrus.New()

type LumberHook struct {
logger *lumberjack.Logger
}

func (hook LumberHook) Levels() []logrus.Level {
return logrus.AllLevels
}

func (hook LumberHook) Fire(entry *logrus.Entry) (err error) {

fields := entry.Data
var writeLine string
if len(fields) != 0 {
writeLine = fmt.Sprintf("[GIN] %s | %s | %d | %s | %s | %f \n",
fields["time"],
fields["method"],
fields["status"],
fields["path"],
fields["clientIp"],
fields["timeSub"],
)
} else {
writeLine = entry.Message + "\n"
}

_, err = hook.logger.Write([]byte(writeLine))
return
}

type MyFormatter struct{}

func (f MyFormatter) Format(entry *logrus.Entry) ([]byte, error) {

// 设置buffer 缓冲区
var b *bytes.Buffer
if entry.Buffer == nil {
b = &bytes.Buffer{}
} else {
b = entry.Buffer
}

// 设置格式
_, err := fmt.Fprintf(b, "%s\n", entry.Message)
if err != nil {
return nil, err
}

return b.Bytes(), nil
}

func InitLog(logPath, appName string) {

// 设置日志输出格式
Log.SetFormatter(&MyFormatter{})

logFileName := path.Join(logPath, appName)
// 使用滚动压缩方式记录日志
lumberLog := &lumberjack.Logger{
Filename: logFileName, //日志文件位置
MaxSize: 1, // 单文件最大容量,单位是MB
MaxBackups: 3, // 最大保留过期文件个数
MaxAge: 1, // 保留过期文件的最大时间间隔,单位是天
Compress: true, // 是否需要压缩滚动日志, 使用的 gzip 压缩
}

fileHook := LumberHook{lumberLog}

Log.AddHook(&fileHook)
}
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
DataBaseHost string
DataBasePort int
DataBaseName string
LogPath = "./log"
)

func initConfig() {
Expand Down Expand Up @@ -47,4 +48,7 @@ func initConfig() {
if sessionSecret := os.Getenv("SESSION_SECRET"); sessionSecret != "" {
SessionSecret = sessionSecret
}
if logPath := os.Getenv("LOG_PATH"); logPath != "" {
LogPath = logPath
}
}
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/spf13/viper v1.16.0
github.com/swaggo/files v1.0.1
github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.16.3
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gorm.io/driver/sqlite v1.5.5
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde
)
Expand All @@ -24,6 +24,7 @@ require (
github.com/bytedance/sonic v1.11.3 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
Expand Down Expand Up @@ -54,13 +55,18 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/swaggo/swag v1.16.3 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/urfave/cli/v2 v2.27.1 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.22.0 // indirect
Expand All @@ -69,5 +75,7 @@ require (
golang.org/x/tools v0.19.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -230,6 +232,10 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
Expand Down Expand Up @@ -268,6 +274,10 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -595,7 +605,11 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand All @@ -615,3 +629,5 @@ rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package main

import (
"auth-server/common"
"auth-server/config"
"auth-server/router"
)

func main() {
//初始化日志
common.InitLog(config.LogPath, config.ServerName)
router.InitRouter()
}
109 changes: 109 additions & 0 deletions middleware/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package middleware

import (
"auth-server/common"
"auth-server/config"
"bytes"
"fmt"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"io"
"net/http"
"time"
)

var log = common.Log

const (
statusColor200 = 42
statusColor404 = 43
statusColor500 = 41
statusColor400 = 40

methodColorGET = 44
methodColorPOST = 45
methodColorPATCH = 46
methodColorDELTE = 47
)

func LogMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery
params := ""
if config.DebugMode && c.ContentType() == "application/json" {
ByteBody, _ := io.ReadAll(c.Request.Body)
c.Request.Body = io.NopCloser(bytes.NewBuffer(ByteBody))
params = string(ByteBody)
}

// Process request
c.Next()

// Stop timer
end := time.Now()
timeSub := end.Sub(start).Seconds()
clientIP := c.ClientIP()
method := c.Request.Method
statusCode := c.Writer.Status()

if raw != "" {
path = path + "?" + raw
}

var statusColor string
switch statusCode {
case http.StatusOK:
statusColor = fmt.Sprintf("\033[%dm %d \033[0m", statusColor200, statusCode)
case http.StatusNotFound:
statusColor = fmt.Sprintf("\033[%dm %d \033[0m", statusColor404, statusCode)
case http.StatusBadRequest:
statusColor = fmt.Sprintf("\033[%dm %d \033[0m", statusColor400, statusCode)
case http.StatusInternalServerError:
statusColor = fmt.Sprintf("\033[%dm %d \033[0m", statusColor500, statusCode)
}

var methodColor string
switch method {
case http.MethodGet:
methodColor = fmt.Sprintf("\033[%dm %s \033[0m", methodColorGET, method)
case http.MethodDelete:
methodColor = fmt.Sprintf("\033[%dm %s \033[0m", methodColorDELTE, method)
case http.MethodPost:
methodColor = fmt.Sprintf("\033[%dm %s \033[0m", methodColorPOST, method)
case http.MethodPatch:
methodColor = fmt.Sprintf("\033[%dm %s \033[0m", methodColorPATCH, method)
}
// 写入到文件中不包含相关到颜色编码,所以需要额外到字段来存储原始信息
fields := logrus.Fields{
"time": start.Format("2006-01-02 15:04:06"),
"method": method,
"status": statusCode,
"path": path,
"clientIp": clientIP,
"timeSub": timeSub,
}
if config.DebugMode {
log.WithFields(fields).Infof("[GIN] %s |%s| %s| %s | %s | %fs \n %s",
start.Format("2006-01-02 15:04:06"),
statusColor,
clientIP,
methodColor,
path,
timeSub,
params,
)
} else {
log.WithFields(fields).Infof("[GIN] %s |%s| %s| %s | %s | %fs",
start.Format("2006-01-02 15:04:06"),
statusColor,
clientIP,
methodColor,
path,
timeSub,
)
}

}
}
9 changes: 6 additions & 3 deletions middleware/response.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package middleware

import (
"auth-server/common"
"auth-server/model"
"github.com/gin-gonic/gin"
"net/http"
Expand All @@ -22,14 +23,14 @@ func Success(c *gin.Context, data interface{}) {

// Auto 根据传入的内容自动推断是属于正确还是错误
func Auto(c *gin.Context, err model.ErrorCode, data interface{}) {
// todo 将具体的日志错误输出到文件中
var resp response
if err.Code != 0 {
resp.Code = err.Code
resp.Msg = err.Msg
if err.Err != nil {
resp.Data = err.Err.Error()
}
common.Log.Error(resp)
} else {
resp.Data = data
}
Expand All @@ -42,9 +43,11 @@ func Fail(c *gin.Context, error model.ErrorCode) {
if error.Err != nil {
errorData = error.Err.Error()
}
c.AbortWithStatusJSON(http.StatusOK, response{
resp := response{
Code: error.Code,
Msg: error.Msg,
Data: errorData,
})
}
common.Log.Error(resp)
c.AbortWithStatusJSON(http.StatusOK, resp)
}
Loading

0 comments on commit 8955704

Please sign in to comment.