feat:update log

This commit is contained in:
wenjianzhang committed 2020-07-24 18:10:25 +08:00
1 parent 0768efbcb3
commit f3adadd6ef
20 files changed
+194 -146

No files matched your search

+1 -2
View File
@@ -4,7 +4,6 @@ import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"go-admin/models"
"go-admin/tools"
"go-admin/tools/app"
@@ -210,7 +209,7 @@ func InsetSysUserAvatar(c *gin.Context) {
guid := uuid.New().String()
filPath := "static/uploadfile/" + guid + ".jpg"
for _, file := range files {
log.Println(file.Filename)
tools.Logger.Println(file.Filename)
// 上传文件至指定目录
_ = c.SaveUploadedFile(file, filPath)
}
+32 -18
View File
@@ -8,7 +8,6 @@ import (
tools2 "go-admin/tools"
"go-admin/tools/app"
"go-admin/tools/config"
"log"
"net/http"
"text/template"
)
@@ -57,6 +56,7 @@ func GenCode(c *gin.Context) {
id, err := tools2.StringToInt(c.Param("tableId"))
tools2.HasError(err, "", -1)
table.TableId = id
tab, _ := table.Get()
ischeckrole := true
rouyerfile := "template/routercheckrole.go.template"
@@ -68,8 +68,6 @@ func GenCode(c *gin.Context) {
}
}
tab, _ := table.Get()
oldTest := "// {{认证路由自动补充在此处请勿删除}}"
newText := "// {{认证路由自动补充在此处请勿删除}} \r\n register" + tab.ClassName + "Router(v1,authMiddleware)"
@@ -79,9 +77,6 @@ func GenCode(c *gin.Context) {
rouyerfile = "template/routernocheckrole.go.template"
}
t1, err := template.ParseFiles("template/model.go.template")
tools2.HasError(err, "", -1)
t2, err := template.ParseFiles("template/api.go.template")
@@ -121,14 +116,14 @@ func GenCode(c *gin.Context) {
NewText: newText,
}
if helper.OldText == helper.NewText {
log.Println("error !! the NewText isEqual the OldText")
tools2.Logger.Println("error !! the NewText isEqual the OldText")
return
}
if err := helper.DoWrok(); err != nil {
log.Print("error:", err.Error())
tools2.Logger.Print("error:", err.Error())
} else {
log.Print("done!")
tools2.Logger.Print("done!")
}
app.OK(c, "", "代码生成成功!")
@@ -146,13 +141,12 @@ func GenMenuAndApi(c *gin.Context) {
Mmenu.MenuName = tab.TBName + "管理"
Mmenu.Title = tab.TableComment
Mmenu.Icon = "pass"
Mmenu.Path = tab.TBName
Mmenu.MenuType = "C"
Mmenu.Path = "/" + tab.TBName
Mmenu.MenuType = "M"
Mmenu.Action = "无"
Mmenu.Permission = tab.PackageName + ":" + tab.ModuleName + ":list"
Mmenu.ParentId = 4
Mmenu.ParentId = 0
Mmenu.NoCache = false
Mmenu.Component = "/" + tab.ModuleName + "/index"
Mmenu.Component = "Layout"
Mmenu.Sort = 0
Mmenu.Visible = "0"
Mmenu.IsFrame = "0"
@@ -162,6 +156,26 @@ func GenMenuAndApi(c *gin.Context) {
Mmenu.UpdatedAt = timeNow
Mmenu.MenuId, err = Mmenu.Create()
Cmenu := models.Menu{}
Cmenu.MenuName = tab.TBName + "管理"
Cmenu.Title = tab.TableComment
Cmenu.Icon = "pass"
Cmenu.Path = tab.TBName
Cmenu.MenuType = "C"
Cmenu.Action = "无"
Cmenu.Permission = tab.PackageName + ":" + tab.ModuleName + ":list"
Cmenu.ParentId = Mmenu.MenuId
Cmenu.NoCache = false
Cmenu.Component = "/" + tab.ModuleName + "/index"
Cmenu.Sort = 0
Cmenu.Visible = "0"
Cmenu.IsFrame = "0"
Cmenu.CreateBy = "1"
Cmenu.UpdateBy = "1"
Cmenu.CreatedAt = timeNow
Cmenu.UpdatedAt = timeNow
Cmenu.MenuId, err = Cmenu.Create()
MList := models.Menu{}
MList.MenuName = tab.TBName
MList.Title = "分页获取" + tab.TableComment
@@ -170,7 +184,7 @@ func GenMenuAndApi(c *gin.Context) {
MList.MenuType = "F"
MList.Action = "无"
MList.Permission = tab.PackageName + ":" + tab.ModuleName + ":query"
MList.ParentId = Mmenu.MenuId
MList.ParentId = Cmenu.MenuId
MList.NoCache = false
MList.Sort = 0
MList.Visible = "0"
@@ -189,7 +203,7 @@ func GenMenuAndApi(c *gin.Context) {
MCreate.MenuType = "F"
MCreate.Action = "无"
MCreate.Permission = tab.PackageName + ":" + tab.ModuleName + ":add"
MCreate.ParentId = Mmenu.MenuId
MCreate.ParentId = Cmenu.MenuId
MCreate.NoCache = false
MCreate.Sort = 0
MCreate.Visible = "0"
@@ -208,7 +222,7 @@ func GenMenuAndApi(c *gin.Context) {
MUpdate.MenuType = "F"
MUpdate.Action = "无"
MUpdate.Permission = tab.PackageName + ":" + tab.ModuleName + ":edit"
MUpdate.ParentId = Mmenu.MenuId
MUpdate.ParentId = Cmenu.MenuId
MUpdate.NoCache = false
MUpdate.Sort = 0
MUpdate.Visible = "0"
@@ -227,7 +241,7 @@ func GenMenuAndApi(c *gin.Context) {
MDelete.MenuType = "F"
MDelete.Action = "无"
MDelete.Permission = tab.PackageName + ":" + tab.ModuleName + ":remove"
MDelete.ParentId = Mmenu.MenuId
MDelete.ParentId = Cmenu.MenuId
MDelete.NoCache = false
MDelete.Sort = 0
MDelete.Visible = "0"
+6 -11
View File
@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go-admin/database"
@@ -29,7 +28,6 @@ var (
Short: "Start API server",
Example: "go-admin server config/settings.yml",
PreRun: func(cmd *cobra.Command, args []string) {
usage()
setup()
},
RunE: func(cmd *cobra.Command, args []string) error {
@@ -44,11 +42,6 @@ func init() {
StartCmd.PersistentFlags().StringVarP(&mode, "mode", "m", "dev", "server mode ; eg:dev,test,prod")
}
func usage() {
usageStr := `starting api server`
log.Printf("%s\n", usageStr)
}
func setup() {
//1. 读取配置
config.ConfigSetup(configYml)
@@ -59,6 +52,8 @@ func setup() {
mycasbin.Setup()
usageStr := `starting api server`
tools.Logger.Printf("%s\n", usageStr)
}
func run() error {
@@ -82,11 +77,11 @@ func run() error {
// 服务连接
if config.ApplicationConfig.IsHttps {
if err := srv.ListenAndServeTLS(config.SslConfig.Pem, config.SslConfig.KeyStr); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s \r\n", err)
tools.Logger.Fatalf("listen: %s \r\n", err)
}
} else {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s \r\n", err)
tools.Logger.Fatalf("listen: %s \r\n", err)
}
}
}()
@@ -104,8 +99,8 @@ func run() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
tools.Logger.Fatal("Server Shutdown:", err)
}
log.Println("Server exiting")
tools.Logger.Println("Server exiting")
return nil
}
+20 -3
View File
@@ -8,12 +8,19 @@ settings:
writertimeout: 2
domain: localhost:8000
ishttps: false
logger: #請求日志
path: temp/logs/access
stdout: true
level: all
enabled: true
ssl:
key: keystring
pem: temp/pem.pem
log:
operdb: false
dir: temp/logs
logger: # 业务日志
path: temp/logs/bus
stdout: true
level: all
enabled: true
jwt:
secret: go-admin
timeout: 3600
@@ -23,6 +30,16 @@ settings:
source: user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8&parseTime=True&loc=Local
# source: sqlite3.db
# source: host=myhost port=myport user=gorm dbname=gorm password=mypassword
logger: #数据库日志
path: temp/logs/db
stdout: true
level: all
enabled: true
gen:
dbname: dbname
frontpath: /go-admin-ui/src # 代码生成是使用前端代码存放位置,需要指定到src文件夹,相对路径
logger: #数据库日志
path: temp/logs/gen
stdout: true
level: all
enabled: true
+2 -1
View File
@@ -29,7 +29,8 @@ func (e *Mysql) Setup() {
log.Fatalf("database error %v", global.Eloquent.Error)
}
global.Eloquent.LogMode(true)
global.Eloquent.LogMode(config.DatabaseConfig.Logger.Enabled)
global.Eloquent.SetLogger(global.DBLogger)
}
// 打开数据库连接
+7 -4
View File
@@ -3,22 +3,25 @@ package global
import (
"github.com/casbin/casbin/v2"
"github.com/gin-gonic/gin"
"github.com/gogf/gf/os/glog"
"github.com/jinzhu/gorm"
)
var GinEngine *gin.Engine
var CasbinEnforcer *casbin.Enforcer
var Eloquent *gorm.DB
var Source string
var Driver string
var DBName string
var CasbinEnforcer *casbin.Enforcer
// go-admin Version Info
var Version string
func init() {
Version = "1.0.10"
}
var Logger *glog.Logger
var DBLogger *glog.Logger
var AccessLogger *glog.Logger
-11
View File
@@ -1,12 +1 @@
package global
import (
"github.com/sirupsen/logrus"
)
var RequestLogger = &logrus.Entry{}
func init() {
// TODO: requestLogger log format
// RequestLogger = logrus.WithFields(logrus.Fields{"request_id": request_id, "user_ip": user_ip})
}
+2 -1
View File
@@ -12,7 +12,8 @@ require (
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/go-openapi/spec v0.19.7 // indirect
github.com/go-openapi/swag v0.19.8 // indirect
github.com/go-sql-driver/mysql v1.4.1
github.com/go-sql-driver/mysql v1.5.0
github.com/gogf/gf v1.13.2
github.com/google/uuid v1.1.1
github.com/jinzhu/gorm v1.9.10
github.com/mailru/easyjson v0.7.1 // indirect
+26
View File
@@ -36,6 +36,8 @@ github.com/casbin/casbin/v2 v2.2.1/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n
github.com/casbin/gorm-adapter/v2 v2.0.3 h1:m8o/APMGkm5Gb8RLHU51F/+9ODUyGgPmC+EEzRJsPr0=
github.com/casbin/gorm-adapter/v2 v2.0.3/go.mod h1:pnulk7RNHbFsldTBruui8jqUhq91sLHtub8LJCJSZg8=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/clbanning/mxj v1.8.4 h1:HuhwZtbyvyOw+3Z1AowPkU87JkJUSv751ELWaiTpj8I=
github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
@@ -116,7 +118,11 @@ github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gogf/gf v1.13.2 h1:R6iFOl+JdBE0J9eqFyZxtKwfJpP1QBHJ9ym0lWXGWHs=
github.com/gogf/gf v1.13.2/go.mod h1:iDDJJJeh5KTUbow6IIYgQpRLjyIfhM/IfWkLR3vb/Ac=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
@@ -133,6 +139,8 @@ github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
@@ -148,6 +156,12 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORR
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gqcn/structs v1.1.1 h1:cyzGRwfmn3d1d54fwW3KUNyG9QxR0ldIeqwFGeBt638=
github.com/gqcn/structs v1.1.1/go.mod h1:/aBhTBSsKQ2Ec9pbnYdGphtdWXHFn4KrCL0fXM/Adok=
github.com/grokify/html-strip-tags-go v0.0.0-20190921062105-daaa06bf1aaf h1:wIOAyJMMen0ELGiFzlmqxdcV1yGbkyHBAB6PolcNbLA=
github.com/grokify/html-strip-tags-go v0.0.0-20190921062105-daaa06bf1aaf/go.mod h1:2Su6romC5/1VXOQMaWL2yb618ARB8iVo6/DR99A6d78=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
@@ -171,6 +185,8 @@ github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwK
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
@@ -207,6 +223,8 @@ github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA=
@@ -228,6 +246,8 @@ github.com/mssola/user_agent v0.5.1 h1:sJUCUozh+j7c0dR2zMIUX5aJjoY/TNo/gXiNujoH5
github.com/mssola/user_agent v0.5.1/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88=
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
@@ -371,6 +391,8 @@ golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 h1:pNX+40auqi2JqRfOP1akLGtYcn15TUbkhwuCO3foqqM=
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -398,6 +420,8 @@ golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2 h1:z99zHgr7hKfrUcX/KsoJk5FJfjTceCKIp96+biqP4To=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -458,6 +482,8 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
+3 -3
View File
@@ -4,7 +4,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/mojocn/base64Captcha"
"github.com/mssola/user_agent"
log "github.com/sirupsen/logrus"
"go-admin/global"
"go-admin/models"
jwt "go-admin/pkg/jwtauth"
"go-admin/tools"
@@ -88,14 +88,14 @@ func Authenticator(c *gin.Context) (interface{}, error) {
msg = "登录失败"
status = "1"
LoginLogToDB(c, status, msg, username)
log.Println(e.Error())
global.AccessLogger.Println(e.Error())
}
return nil, jwt.ErrFailedAuthentication
}
// Write log to database
func LoginLogToDB(c *gin.Context, status string, msg string, username string) {
if config.LogConfig.Operdb {
if config.LoggerConfig.Enabled {
var loginlog models.LoginLog
ua := user_agent.New(c.Request.UserAgent())
loginlog.Ipaddr = c.ClientIP()
+12 -10
View File
@@ -2,7 +2,7 @@ package middleware
import (
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"go-admin/global"
"go-admin/models"
"go-admin/tools"
config2 "go-admin/tools/config"
@@ -39,16 +39,18 @@ func LoggerToFile() gin.HandlerFunc {
clientIP := c.ClientIP()
// 日志格式
log.Infof(" %s %3d %13v %15s %s %s \r\n",
startTime.Format("2006-01-02 15:04:05.9999"),
statusCode,
latencyTime,
clientIP,
reqMethod,
reqUri,
)
//global.AccessLogger.Printf(" %s %3d %13v %15s %s %s \r\n",
// startTime.Format("2006-01-02 15:04:05.9999"),
// statusCode,
// latencyTime,
// clientIP,
// reqMethod,
// reqUri,
//)
if c.Request.Method != "GET" && c.Request.Method != "OPTIONS" && config2.LogConfig.Operdb {
global.AccessLogger.Info(statusCode, latencyTime, clientIP, reqMethod, reqUri)
if c.Request.Method != "GET" && c.Request.Method != "OPTIONS" && config2.LoggerConfig.Enabled {
SetDBOperLog(c, clientIP, statusCode, reqUri, reqMethod, latencyTime)
}
}
+3 -2
View File
@@ -3,15 +3,16 @@ package cronjob
import (
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
tools2 "go-admin/tools"
)
func TestJob(c *cron.Cron) {
id, err := c.AddFunc("1 * * * *", func() {
log.Println("Every hour on the one hour")
tools2.Logger.Println("Every hour on the one hour")
})
if err != nil {
log.Println(err)
tools2.Logger.Println(err)
log.Println("start error")
} else {
log.Printf("Start Success; ID: %v \r\n", id)
+10 -6
View File
@@ -1,11 +1,15 @@
package logger
import (
log "github.com/sirupsen/logrus"
"github.com/gogf/gf/os/glog"
"go-admin/tools/config"
)
var (
Info *log.Logger
Warning *log.Logger
Error *log.Logger
)
var Logger *glog.Logger
func InitLog() {
Logger = glog.New()
Logger.Path(config.LoggerConfig.Path)
_ = Logger.SetLevelStr(config.LoggerConfig.Level)
Logger.Info("Logger init Success!")
}
+7
View File
@@ -13,6 +13,7 @@ type Application struct {
DemoMsg string
Domain string
IsHttps bool
Logger *Logger
}
func InitApplication(cfg *viper.Viper) *Application {
@@ -27,6 +28,12 @@ func InitApplication(cfg *viper.Viper) *Application {
DemoMsg: cfg.GetString("demoMsg"),
Domain: cfg.GetString("domain"),
IsHttps: cfg.GetBool("ishttps"),
Logger: &Logger{
Path: cfg.GetString("logger.path"),
Level: cfg.GetString("logger.level"),
Stdout: cfg.GetBool("logger.stdout"),
Enabled: cfg.GetBool("logger.enabled"),
},
}
}
+5 -5
View File
@@ -12,7 +12,7 @@ import (
var cfgDatabase *viper.Viper
var cfgApplication *viper.Viper
var cfgJwt *viper.Viper
var cfgLog *viper.Viper
var cfgLogger *viper.Viper
var cfgSsl *viper.Viper
var cfgGen *viper.Viper
@@ -48,11 +48,11 @@ func ConfigSetup(path string) {
}
JwtConfig = InitJwt(cfgJwt)
cfgLog = viper.Sub("settings.log")
if cfgLog == nil {
panic("config not found settings.log")
cfgLogger = viper.Sub("settings.logger")
if cfgLogger == nil {
panic("config not found settings.logger")
}
LogConfig = InitLog(cfgLog)
LoggerConfig = InitLog(cfgLogger)
cfgSsl = viper.Sub("settings.ssl")
if cfgSsl == nil {
+7
View File
@@ -6,6 +6,7 @@ type Database struct {
Driver string
Source string
DBName string
Logger *Logger
}
func InitDatabase(cfg *viper.Viper) *Database {
@@ -14,6 +15,12 @@ func InitDatabase(cfg *viper.Viper) *Database {
Driver: cfg.GetString("driver"),
Source: cfg.GetString("source"),
DBName: cfg.GetString("dbname"),
Logger: &Logger{
Path: cfg.GetString("logger.path"),
Level: cfg.GetString("logger.level"),
Stdout: cfg.GetBool("logger.stdout"),
Enabled: cfg.GetBool("logger.enabled"),
},
}
return db
}
-17
View File
@@ -1,17 +0,0 @@
package config
import "github.com/spf13/viper"
type Log struct {
Dir string
Operdb bool
}
func InitLog(cfg *viper.Viper) *Log {
return &Log{
Dir: cfg.GetString("dir"),
Operdb: cfg.GetBool("operdb"),
}
}
var LogConfig = new(Log)
+21
View File
@@ -0,0 +1,21 @@
package config
import "github.com/spf13/viper"
type Logger struct {
Path string
Level string
Stdout bool
Enabled bool
}
func InitLog(cfg *viper.Viper) *Logger {
return &Logger{
Path: cfg.GetString("path"),
Level: cfg.GetString("level"),
Enabled: cfg.GetBool("enabled"),
Stdout: cfg.GetBool("stdout"),
}
}
var LoggerConfig = new(Logger)
+28 -49
View File
@@ -1,61 +1,40 @@
package tools
import (
"errors"
log "github.com/sirupsen/logrus"
config2 "go-admin/tools/config"
"os"
"time"
"github.com/gogf/gf/os/glog"
"go-admin/global"
"go-admin/tools/config"
)
var Logger *glog.Logger
var DBLogger *glog.Logger
var AccessLogger *glog.Logger
func InitLogger() {
log.SetFormatter(&log.TextFormatter{FieldMap: log.FieldMap{
log.FieldKeyTime: "@timestamp",
log.FieldKeyLevel: "@level",
log.FieldKeyMsg: "@message"}})
Logger = glog.New()
Logger.SetPath(config.LoggerConfig.Path)
Logger.Stdout(config.LoggerConfig.Stdout)
Logger.SetFile("logger-{Ymd}.log")
_ = Logger.SetLevelStr(config.LoggerConfig.Level)
switch Mode(config2.ApplicationConfig.Mode) {
case ModeDev, ModeTest:
log.SetOutput(os.Stdout)
log.SetLevel(log.TraceLevel)
case ModeProd:
file, err := os.OpenFile(config2.LogConfig.Dir+"/api-"+time.Now().Format("2006-01-02")+".log", os.O_WRONLY|os.O_APPEND|os.O_CREATE|os.O_SYNC, 0600)
if err != nil {
log.Fatalln("log init failed")
}
Logger.Info("Logger init Success!")
var info os.FileInfo
info, err = file.Stat()
if err != nil {
log.Fatal(err)
}
fileWriter := logFileWriter{file, info.Size()}
log.SetOutput(&fileWriter)
log.SetLevel(log.WarnLevel)
}
DBLogger = glog.New()
_ = DBLogger.SetPath(config.DatabaseConfig.Logger.Path)
DBLogger.Stdout(config.DatabaseConfig.Logger.Stdout)
DBLogger.SetFile("db-{Ymd}.log")
_ = DBLogger.SetLevelStr(config.DatabaseConfig.Logger.Level)
DBLogger.Info("DBLogger init Success!")
log.SetReportCaller(true)
}
AccessLogger = glog.New()
_ = AccessLogger.SetPath(config.ApplicationConfig.Logger.Path)
AccessLogger.Stdout(config.ApplicationConfig.Logger.Stdout)
AccessLogger.SetFile("access-{Ymd}.log")
_ = AccessLogger.SetLevelStr(config.ApplicationConfig.Logger.Level)
AccessLogger.Info("AccessLogger init Success!")
type logFileWriter struct {
file *os.File
size int64
}
global.Logger = Logger.Line()
global.DBLogger = DBLogger.Line()
global.AccessLogger = AccessLogger.Line()
func (p *logFileWriter) Write(data []byte) (n int, err error) {
if p == nil {
return 0, errors.New("logFileWriter is nil")
}
if p.file == nil {
return 0, errors.New("file not opened")
}
n, e := p.file.Write(data)
p.size += int64(n)
//每天一个文件
if p.file.Name() != config2.LogConfig.Dir+"/api-"+time.Now().Format("2006-01-02")+".log" {
p.file.Close()
p.file, _ = os.OpenFile(config2.LogConfig.Dir+"/api-"+time.Now().Format("2006-01-02")+".log", os.O_WRONLY|os.O_APPEND|os.O_CREATE|os.O_SYNC, 0600)
p.size = 0
}
return n, e
}
+2 -3
View File
@@ -1,7 +1,6 @@
package tools
import (
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/bcrypt"
"strconv"
)
@@ -19,7 +18,7 @@ func StrToInt(err error, index string) int {
func CompareHashAndPassword(e string, p string) (bool, error) {
err := bcrypt.CompareHashAndPassword([]byte(e), []byte(p))
if err != nil {
log.Print(err.Error())
Logger.Print(err.Error())
return false, err
}
return true, nil
@@ -51,7 +50,7 @@ func HasError(err error, msg string, code ...int) {
if msg == "" {
msg = err.Error()
}
log.Println(err)
Logger.Println(err)
panic("CustomError#" + strconv.Itoa(statusCode) + "#" + msg)
}
}