Browse Source

开启日志功能

main
pixel 5 years ago
parent
commit
6612e792bb
  1. 18
      QMPlusServer/controller/servers/reportformat.go
  2. 2
      QMPlusServer/init/initRouter/initRouter.go
  3. 10
      QMPlusServer/main.go

18
QMPlusServer/controller/servers/reportformat.go

@ -2,10 +2,28 @@ package servers
import (
"github.com/gin-gonic/gin"
"main/init/qmlog"
"net/http"
"time"
)
func ReportFormat(c *gin.Context, success bool, msg string, json gin.H) {
// 开始时间
start := time.Now()
path := c.Request.URL.Path
clientIP := c.ClientIP()
method := c.Request.Method
statusCode := c.Writer.Status()
qmlog.QMLog.Infof("| %3d | %13v | %15s | %s %s |%s|",
statusCode,
start,
clientIP,
method, path, gin.H{
"success": success,
"msg": msg,
"data": json,
},
)
c.JSON(http.StatusOK, gin.H{
"success": success,
"msg": msg,

2
QMPlusServer/init/initRouter/initRouter.go

@ -5,12 +5,14 @@ import (
"github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
_ "main/docs"
"main/middleware"
"main/router"
)
//初始化总路由
func InitRouter() *gin.Engine {
var Router = gin.Default()
Router.Use(middleware.Logger()) // 如果不需要日志 请关闭这里
Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
//Router.Use(middleware.Logger())
router.InitUserRouter(Router) // 注册用户路由

10
QMPlusServer/main.go

@ -23,7 +23,7 @@ func main() {
registTable.RegistTable(qmsql.InitMysql(config.Dbconfig.Admin))
defer qmsql.DEFAULTDB.Close()
Router := initRouter.InitRouter()
//qmlog.QMLog.Info("服务器开启") // 日志测试代码
qmlog.QMLog.Info("服务器开启") // 日志测试代码
s := &http.Server{
Addr: ":8888",
@ -32,16 +32,16 @@ func main() {
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
time.Sleep(10*time.Microsecond)
time.Sleep(10 * time.Microsecond)
fmt.Printf(`欢迎使用 Gin-Vue-Admin
作者奇淼 And Spike666
微信shouzi_1994
默认自动化文档地址:http://127.0.0.1%s/swagger/index.html
默认前端文件运行地址:http://127.0.0.1:8080
`,s.Addr)
`, s.Addr)
_ = s.ListenAndServe()
}
func run(server *http.Server){
func run(server *http.Server) {
}
}
Loading…
Cancel
Save