Skip to content

Commit

Permalink
调整log代码,兼容zap扩展支持fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mss-boot committed Mar 5, 2021
1 parent 39dd1ee commit 7571192
Show file tree
Hide file tree
Showing 47 changed files with 267 additions and 275 deletions.
5 changes: 2 additions & 3 deletions app/admin/apis/sys_job/sys_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"go-admin/app/admin/service"
"go-admin/common/apis"
"go-admin/common/dto"
"go-admin/common/global"
"go-admin/tools/app"
)

Expand All @@ -33,7 +32,7 @@ func (e *SysJob) RemoveJobForService(c *gin.Context) {
s := service.SysJob{}
s.Log = log
s.Orm = db
s.Cron = global.Runtime.GetCrontabKey(c.Request.Host)
s.Cron = app.Runtime.GetCrontabKey(c.Request.Host)
err = s.RemoveJob(&v)
if err != nil {
log.Errorf("RemoveJob error, %s", err.Error())
Expand Down Expand Up @@ -61,7 +60,7 @@ func (e *SysJob) StartJobForService(c *gin.Context) {
s := service.SysJob{}
s.Orm = db
s.Log = log
s.Cron = global.Runtime.GetCrontabKey(c.Request.Host)
s.Cron = app.Runtime.GetCrontabKey(c.Request.Host)
err = s.StartJob(&v)
if err != nil {
log.Errorf("GetCrontabKey error, %s", err.Error())
Expand Down
4 changes: 2 additions & 2 deletions app/admin/apis/tools/dbcolumns.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package tools

import (
"go-admin/common/apis"
"net/http"

"github.com/gin-gonic/gin"

"go-admin/app/admin/models/tools"
"go-admin/pkg/logger"
tools2 "go-admin/tools"
"go-admin/tools/app"
)
Expand All @@ -20,7 +20,7 @@ import (
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/db/columns/page [get]
func GetDBColumnList(c *gin.Context) {
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
var data tools.DBColumns
var err error
var pageSize = 10
Expand Down
4 changes: 2 additions & 2 deletions app/admin/apis/tools/dbtables.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package tools

import (
"go-admin/common/apis"
"net/http"

"github.com/gin-gonic/gin"

"go-admin/app/admin/models/tools"
"go-admin/pkg/logger"
tools2 "go-admin/tools"
"go-admin/tools/app"
"go-admin/tools/config"
Expand All @@ -26,7 +26,7 @@ func GetDBTableList(c *gin.Context) {
var err error
var pageSize = 10
var pageIndex = 1
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
if config.DatabaseConfig.Driver == "sqlite3" || config.DatabaseConfig.Driver == "postgres" {
res.Msg = "对不起,sqlite3 或 postgres 不支持代码生成!"
c.JSON(http.StatusOK, res.ReturnError(500))
Expand Down
10 changes: 5 additions & 5 deletions app/admin/apis/tools/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ package tools

import (
"bytes"
"go-admin/common/apis"
"net/http"
"text/template"

"github.com/gin-gonic/gin"

"go-admin/app/admin/models"
"go-admin/app/admin/models/tools"
"go-admin/pkg/logger"
tools2 "go-admin/tools"
"go-admin/tools/app"
"go-admin/tools/config"
)

func Preview(c *gin.Context) {
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
table := tools.SysTables{}
id, err := tools2.StringToInt(c.Param("tableId"))
tools2.HasError(err, "", -1)
Expand Down Expand Up @@ -69,7 +69,7 @@ func Preview(c *gin.Context) {
}

func GenCodeV3(c *gin.Context) {
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
table := tools.SysTables{}
id, err := tools2.StringToInt(c.Param("tableId"))
tools2.HasError(err, "", -1)
Expand Down Expand Up @@ -192,7 +192,7 @@ func ActionsGenV3(tab tools.SysTables) {
}

func GenMenuAndApi(c *gin.Context) {
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)

table := tools.SysTables{}
timeNow := tools2.GetCurrentTime()
Expand Down Expand Up @@ -433,4 +433,4 @@ func GenMenuAndApi(c *gin.Context) {
ADelete.MenuId, err = ADelete.Create(db)

app.OK(c, "", "数据生成成功!")
}
}
18 changes: 9 additions & 9 deletions app/admin/apis/tools/systables.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tools

import (
"go-admin/pkg/logger"
"go-admin/common/apis"
"gorm.io/gorm"
"net/http"
"strings"
Expand All @@ -22,7 +22,7 @@ import (
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/sys/tables/page [get]
func GetSysTableList(c *gin.Context) {
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
var data tools.SysTables
var err error
var pageSize = 10
Expand Down Expand Up @@ -68,7 +68,7 @@ func GetSysTableList(c *gin.Context) {
// @Router /api/v1/sys/tables/info/{tableId} [get]
// @Security Bearer
func GetSysTables(c *gin.Context) {
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
db, err := tools2.GetOrm(c)
if err != nil {
log.Errorf("get db connection error, %s", err.Error())
Expand All @@ -91,7 +91,7 @@ func GetSysTables(c *gin.Context) {
}

func GetSysTablesInfo(c *gin.Context) {
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
db, err := tools2.GetOrm(c)
if err != nil {
log.Errorf("get db connection error, %s", err.Error())
Expand All @@ -116,7 +116,7 @@ func GetSysTablesInfo(c *gin.Context) {
}

func GetSysTablesTree(c *gin.Context) {
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
db, err := tools2.GetOrm(c)
if err != nil {
log.Errorf("get db connection error, %s", err.Error())
Expand Down Expand Up @@ -144,7 +144,7 @@ func GetSysTablesTree(c *gin.Context) {
// @Router /api/v1/sys/tables/info [post]
// @Security Bearer
func InsertSysTable(c *gin.Context) {
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
db, err := tools2.GetOrm(c)
if err != nil {
log.Errorf("get db connection error, %s", err.Error())
Expand Down Expand Up @@ -246,7 +246,7 @@ func genTableInit(tx *gorm.DB, tablesList []string, i int, c *gin.Context) (tool
if strings.Contains(dbcolumn[i].ColumnType, "int") {
if strings.Contains(dbcolumn[i].ColumnKey, "PR") {
column.GoType = "int"
} else {
} else {
column.GoType = "string"
}
column.HtmlType = "input"
Expand Down Expand Up @@ -281,7 +281,7 @@ func UpdateSysTable(c *gin.Context) {
err := c.Bind(&data)
tools2.HasError(err, "数据解析失败", 500)

log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
db, err := tools2.GetOrm(c)
if err != nil {
log.Errorf("get db connection error, %s", err.Error())
Expand All @@ -307,7 +307,7 @@ func UpdateSysTable(c *gin.Context) {
// @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
// @Router /api/v1/sys/tables/info/{tableId} [delete]
func DeleteSysTables(c *gin.Context) {
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
db, err := tools2.GetOrm(c)
if err != nil {
log.Errorf("get db connection error, %s", err.Error())
Expand Down
8 changes: 4 additions & 4 deletions app/admin/middleware/handler/auth.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"go-admin/common/apis"
"net/http"

"github.com/gin-gonic/gin"
Expand All @@ -10,7 +11,6 @@ import (
"go-admin/app/admin/models/system"
"go-admin/app/admin/service"
jwt "go-admin/pkg/jwtauth"
"go-admin/pkg/logger"
"go-admin/tools"
"go-admin/tools/app"
"go-admin/tools/config"
Expand Down Expand Up @@ -59,7 +59,7 @@ func IdentityHandler(c *gin.Context) interface{} {
// @Success 200 {string} string "{"code": 200, "expire": "2019-08-07T12:45:48+08:00", "token": ".eyJleHAiOjE1NjUxNTMxNDgsImlkIjoiYWRtaW4iLCJvcmlnX2lhdCI6MTU2NTE0OTU0OH0.-zvzHvbg0A" }"
// @Router /login [post]
func Authenticator(c *gin.Context) (interface{}, error) {
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
db, err := tools.GetOrm(c)
if err != nil {
log.Errorf("get db error, %s", err.Error())
Expand Down Expand Up @@ -106,7 +106,7 @@ func Authenticator(c *gin.Context) (interface{}, error) {

// LoginLogToDB Write log to database
func LoginLogToDB(c *gin.Context, status string, msg string, username string) {
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
if config.LoggerConfig.EnabledDB {
var loginLog system.SysLoginLog
db, err := tools.GetOrm(c)
Expand Down Expand Up @@ -142,7 +142,7 @@ func LoginLogToDB(c *gin.Context, status string, msg string, username string) {
// @Router /logout [post]
// @Security Bearer
func LogOut(c *gin.Context) {
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
var loginLog system.SysLoginLog
ua := user_agent.New(c.Request.UserAgent())
loginLog.Ipaddr = c.ClientIP()
Expand Down
6 changes: 3 additions & 3 deletions app/admin/middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package middleware

import (
"fmt"
"go-admin/common/apis"
"net/http"
"strings"
"time"
Expand All @@ -11,7 +12,6 @@ import (
"go-admin/app/admin/models"
"go-admin/app/admin/models/system"
"go-admin/app/admin/service"
"go-admin/pkg/logger"
"go-admin/tools"
"go-admin/tools/app"
"go-admin/tools/config"
Expand All @@ -24,7 +24,7 @@ func LoggerToFile() gin.HandlerFunc {
if c.Request.Method == http.MethodOptions {
return
}
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
// 开始时间
startTime := time.Now()
// 处理请求
Expand Down Expand Up @@ -78,7 +78,7 @@ func LoggerToFile() gin.HandlerFunc {

// SetDBOperLog 写入操作日志表 fixme 该方法后续即将弃用
func SetDBOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string, reqMethod string, latencyTime time.Duration, body string, result string, status int) {
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
db, err := tools.GetOrm(c)
if err != nil {
log.Errorf("get db connection error, %s", err.Error())
Expand Down
7 changes: 3 additions & 4 deletions app/admin/middleware/permission.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package middleware

import (
"go-admin/pkg/logger"
"go-admin/common/apis"
"net/http"

"github.com/gin-gonic/gin"

"go-admin/common/global"
"go-admin/pkg/jwtauth"
"go-admin/tools/app"
)

//权限检查中间件
func AuthCheckRole() gin.HandlerFunc {
return func(c *gin.Context) {
log := logger.GetRequestLogger(c)
log := apis.GetRequestLogger(c)
data, _ := c.Get(jwtauth.JwtPayloadKey)
v := data.(jwtauth.MapClaims)
e := global.Runtime.GetCasbinKey(c.Request.Host)
e := app.Runtime.GetCasbinKey(c.Request.Host)
var res bool
var err error
//检查权限
Expand Down
5 changes: 2 additions & 3 deletions app/admin/models/initdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package models

import (
"fmt"
"go-admin/tools/app"
"gorm.io/gorm"
"io/ioutil"
"log"
"strings"

"go-admin/common/global"
)

func InitDb(db *gorm.DB) (err error) {
filePath := "config/db.sql"
err = ExecSql(db, filePath)
if global.Driver == "postgres" {
if app.Driver == "postgres" {
filePath = "config/pg.sql"
err = ExecSql(db, filePath)
}
Expand Down
8 changes: 4 additions & 4 deletions app/admin/router/init_router.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package router

import (
"go-admin/tools/app"
"os"

"github.com/gin-gonic/gin"
log "github.com/go-admin-team/go-admin-core/logger"

"go-admin/app/admin/middleware"
"go-admin/app/admin/middleware/handler"
"go-admin/common/global"
common "go-admin/common/middleware"
//_ "go-admin/pkg/jwtauth"
"go-admin/tools"
Expand All @@ -18,10 +18,10 @@ import (
// InitRouter 路由初始化,不要怀疑,这里用到了
func InitRouter() {
var r *gin.Engine
h := global.Runtime.GetEngine()
h := app.Runtime.GetEngine()
if h == nil {
h = gin.New()
global.Runtime.SetEngine(h)
app.Runtime.SetEngine(h)
}
switch h.(type) {
case *gin.Engine:
Expand All @@ -35,7 +35,7 @@ func InitRouter() {
}

r.Use(common.Sentinel()).
Use(common.RequestIdLogger(tools.TrafficKey, tools.LoggerKey))
Use(common.RequestId(tools.TrafficKey))
middleware.InitMiddleware(r)
// the jwt middleware
authMiddleware, err := middleware.AuthInit()
Expand Down
9 changes: 4 additions & 5 deletions app/admin/router/monitor.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package router

import (
"go-admin/tools/app"
"net/http"

log "github.com/go-admin-team/go-admin-core/logger"
"go-admin/common/global"

"github.com/gin-gonic/gin"
log "github.com/go-admin-team/go-admin-core/logger"
"github.com/go-admin-team/go-admin-core/tools/transfer"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func Monitor() {
var r *gin.Engine
h := global.Runtime.GetEngine()
h := app.Runtime.GetEngine()
if h == nil {
h = gin.New()
global.Runtime.SetEngine(h)
app.Runtime.SetEngine(h)
}
switch h.(type) {
case *gin.Engine:
Expand Down
Loading

0 comments on commit 7571192

Please sign in to comment.