From 562df437c1a559aa02cad4dd97210b26bc317622 Mon Sep 17 00:00:00 2001 From: taoshihan1991 <630892807@qq.com> Date: Tue, 20 Oct 2020 14:04:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=97=A5=E5=BF=97=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E5=99=A8=E8=AE=B0=E5=BD=95sql=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/server.go | 3 ++ go.mod | 1 + models/models.go | 2 ++ tmpl/common.go | 4 +++ tools/logger.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 tools/logger.go diff --git a/cmd/server.go b/cmd/server.go index 784340b..583f08c 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -8,6 +8,7 @@ import ( "github.com/taoshihan1991/imaptool/config" "github.com/taoshihan1991/imaptool/docs" "github.com/taoshihan1991/imaptool/router" + "github.com/taoshihan1991/imaptool/tools" "log" "os" "os/exec" @@ -56,6 +57,8 @@ func run() { engine.LoadHTMLGlob("static/html/*") engine.Static("/static", "./static") + //记录日志 + engine.Use(tools.LoggerToFile()) router.InitViewRouter(engine) router.InitApiRouter(engine) diff --git a/go.mod b/go.mod index 3824f9f..4b350ce 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/ipipdotnet/ipdb-go v1.3.0 github.com/jinzhu/gorm v1.9.14 github.com/satori/go.uuid v1.2.0 + github.com/sirupsen/logrus v1.4.2 github.com/spf13/cobra v0.0.5 github.com/swaggo/gin-swagger v1.2.0 github.com/swaggo/swag v1.5.1 diff --git a/models/models.go b/models/models.go index ba87fbf..4c9972e 100644 --- a/models/models.go +++ b/models/models.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/jinzhu/gorm" "github.com/taoshihan1991/imaptool/config" + "github.com/taoshihan1991/imaptool/tools" "time" ) @@ -26,6 +27,7 @@ func init() { } DB.SingularTable(true) DB.LogMode(true) + DB.SetLogger(tools.Logger()) DB.DB().SetMaxIdleConns(10) DB.DB().SetMaxOpenConns(100) diff --git a/tmpl/common.go b/tmpl/common.go index 7b80b5d..11503c4 100644 --- a/tmpl/common.go +++ b/tmpl/common.go @@ -46,6 +46,10 @@ func (obj *CommonHtml) Display(file string, data interface{}) { //首页 func PageIndex(c *gin.Context) { + if c.Request.RequestURI == "/favicon.ico" { + return + } + lang, _ := c.Get("lang") language := config.CreateLanguage(lang.(string)) about := models.FindAboutByPageLanguage("index", lang.(string)) diff --git a/tools/logger.go b/tools/logger.go new file mode 100644 index 0000000..cf7d7df --- /dev/null +++ b/tools/logger.go @@ -0,0 +1,87 @@ +package tools + +import ( + "fmt" + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" + "os" + "path" + "time" +) + +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.Out = src + + //设置日志级别 + logger.SetLevel(logrus.DebugLevel) + + //设置日志格式 + logger.SetFormatter(&logrus.TextFormatter{ + TimestampFormat: "2006-01-02 15:04:05", + }) + return logger +} + +func LoggerToFile() gin.HandlerFunc { + logger := Logger() + return func(c *gin.Context) { + // 开始时间 + startTime := time.Now() + + // 处理请求 + c.Next() + + // 结束时间 + endTime := time.Now() + + // 执行时间 + latencyTime := endTime.Sub(startTime) + + // 请求方式 + reqMethod := c.Request.Method + + // 请求路由 + reqUri := c.Request.RequestURI + + // 状态码 + statusCode := c.Writer.Status() + + // 请求IP + clientIP := c.ClientIP() + + //日志格式 + logger.Infof("| %3d | %13v | %15s | %s | %s |", + statusCode, + latencyTime, + clientIP, + reqMethod, + reqUri, + ) + } +}