作者 陈志颖

feat:添加日志模块

  1 +2020/11/22 07:07:43.485 [I] [???:0] http server Running on http://:8082
  2 +2020/11/22 07:07:58.794 [D] [server.go:2843] ====>Recv User:<nil> RequestId:POST./cash-pool/activity/exchange-list/import.1606000078794641000
  3 + Auth=
  4 + BodyData:
  5 +2020/11/22 07:07:58.795 [C] [iface.go:261] the request url is /cash-pool/activity/exchange-list/import
  6 +2020/11/22 07:07:58.795 [C] [iface.go:261] Handler crashed with error interface conversion: interface {} is nil, not string
  7 +2020/11/22 07:07:58.796 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/runtime/panic.go:975
  8 +2020/11/22 07:07:58.796 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/runtime/iface.go:261
  9 +2020/11/22 07:07:58.796 [C] [iface.go:261] /Users/stevechan/GolandProjects/mmm-worth/pkg/port/beego/controllers/su_money_controller.go:403
  10 +2020/11/22 07:07:58.796 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/reflect/value.go:475
  11 +2020/11/22 07:07:58.796 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/reflect/value.go:336
  12 +2020/11/22 07:07:58.796 [C] [iface.go:261] /Users/stevechan/Golang/pkg/mod/github.com/astaxie/beego@v1.12.1/router.go:853
  13 +2020/11/22 07:07:58.796 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/net/http/server.go:2843
  14 +2020/11/22 07:07:58.796 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/net/http/server.go:1925
  15 +2020/11/22 07:07:58.796 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/runtime/asm_amd64.s:1374
  16 +2020/11/22 07:07:58.797 [server.go:1141] [HTTP] http: superfluous response.WriteHeader call from github.com/astaxie/beego/context.(*Response).WriteHeader (context.go:230)
  17 +2020/11/22 07:08:18.381 [D] [server.go:2843] ====>Recv User:<nil> RequestId:GET./cash-pool/cash-pool?companyId=311.1606000098381710000
  18 + Auth=
  19 + BodyData:{}
  20 +2020/11/22 07:08:18.844 [D] [server.go:2843] <====Send RequestId:GET./cash-pool/cash-pool?companyId=311.1606000098381710000 BodyData:{"code":0,"data":{"cashPoolId":14,"cash":2000,"companyId":311,"exchangedCash":1007,"unExchangeCash":993,"exchangedSuMoney":196,"unExchangeSuMoney":1079,"rate":5.14,"lastRate":5,"createTime":"2020-11-18T01:46:49.196257+08:00"},"msg":"ok"}
  21 +2020/11/22 07:08:18.844 [D] [server.go:1925] | 127.0.0.1| 200 | 462.469407ms| match| GET  /cash-pool/cash-pool r:/cash-pool/cash-pool
  22 +2020/11/22 07:08:50.492 [D] [server.go:2843] ====>Recv User:<nil> RequestId:POST./cash-pool/activity/exchange-list/import.1606000130492709000
  23 + Auth=
  24 + BodyData:
  25 +2020/11/22 07:08:50.493 [C] [iface.go:261] the request url is /cash-pool/activity/exchange-list/import
  26 +2020/11/22 07:08:50.493 [C] [iface.go:261] Handler crashed with error interface conversion: interface {} is nil, not string
  27 +2020/11/22 07:08:50.493 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/runtime/panic.go:975
  28 +2020/11/22 07:08:50.493 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/runtime/iface.go:261
  29 +2020/11/22 07:08:50.493 [C] [iface.go:261] /Users/stevechan/GolandProjects/mmm-worth/pkg/port/beego/controllers/su_money_controller.go:403
  30 +2020/11/22 07:08:50.493 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/reflect/value.go:475
  31 +2020/11/22 07:08:50.493 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/reflect/value.go:336
  32 +2020/11/22 07:08:50.493 [C] [iface.go:261] /Users/stevechan/Golang/pkg/mod/github.com/astaxie/beego@v1.12.1/router.go:853
  33 +2020/11/22 07:08:50.493 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/net/http/server.go:2843
  34 +2020/11/22 07:08:50.493 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/net/http/server.go:1925
  35 +2020/11/22 07:08:50.493 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/runtime/asm_amd64.s:1374
  36 +2020/11/22 07:08:50.494 [server.go:1141] [HTTP] http: superfluous response.WriteHeader call from github.com/astaxie/beego/context.(*Response).WriteHeader (context.go:230)
  37 +2020/11/22 07:09:10.243 [D] [server.go:2843] ====>Recv User:<nil> RequestId:POST./cash-pool/activity/exchange-list/import.1606000150243471000
  38 + Auth=
  39 + BodyData:{
  40 + "where": {
  41 + "activityId": "80",
  42 + "uid": 3242227175192320
  43 + }
  44 +}
  45 +2020/11/22 07:09:28.882 [I] [???:0] http server Running on http://:8082
@@ -8,6 +8,9 @@ var LOG_LEVEL = "debug" @@ -8,6 +8,9 @@ var LOG_LEVEL = "debug"
8 var ABILITY_SERVICE_HOST = "https://suplus-worth-app-gateway-dev.fjmaimaimai.com" 8 var ABILITY_SERVICE_HOST = "https://suplus-worth-app-gateway-dev.fjmaimaimai.com"
9 var MMM_OPEN_API_SERVICE_HOST = "http://mmm-open-api-dev.fjmaimaimai.com" 9 var MMM_OPEN_API_SERVICE_HOST = "http://mmm-open-api-dev.fjmaimaimai.com"
10 10
  11 +//var LOG_LEVEL = "debug"
  12 +var LOG_File = "app.log"
  13 +
11 func init() { 14 func init() {
12 if os.Getenv("LOG_LEVEL") != "" { 15 if os.Getenv("LOG_LEVEL") != "" {
13 LOG_LEVEL = os.Getenv("LOG_LEVEL") 16 LOG_LEVEL = os.Getenv("LOG_LEVEL")
1 -package constant 1 +//package constant
  2 +
  3 +//var Logger log.Logger
  4 +
  5 +// TODO 日志输出
  6 +//func init() {
  7 +// Logger = logrus.NewLogrusLogger()
  8 +// Logger.SetServiceName(constant.SERVICE_NAME)
  9 +// Logger.SetLevel(constant.LOG_LEVEL)
  10 +//}
  11 +
  12 +
  13 +package log
2 14
3 import ( 15 import (
4 - "github.com/linmadan/egglib-go/log"  
5 - "github.com/linmadan/egglib-go/log/logrus" 16 + "github.com/astaxie/beego"
  17 + "github.com/astaxie/beego/logs"
6 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/constant" 18 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/constant"
  19 + "path/filepath"
  20 + "strconv"
7 ) 21 )
8 22
9 -var Logger log.Logger 23 +var Logger *logs.BeeLogger
10 24
11 -// TODO 日志输出  
12 func init() { 25 func init() {
13 - Logger = logrus.NewLogrusLogger()  
14 - Logger.SetServiceName(constant.SERVICE_NAME)  
15 - Logger.SetLevel(constant.LOG_LEVEL) 26 + Logger = NewBeegoLogger(LoggerConfig{
  27 + Filename: constant.LOG_File,
  28 + Level: constant.LOG_LEVEL,
  29 + })
  30 +}
  31 +
  32 +type LoggerConfig struct {
  33 + Level string `json:"level,omitempty"`
  34 + Filename string `json:"filename,omitempty"`
  35 + MaxSize int `json:"max_size,omitempty"`
  36 + MaxBackups int `json:"max_backups,omitempty"`
  37 + MaxAge int `json:"max_age,omitempty"`
  38 + Compress bool `json:"compress,omitempty"`
16 } 39 }
17 40
  41 +func NewBeegoLogger(conf LoggerConfig) *logs.BeeLogger {
  42 + filename := `{"filename":"` + filepath.ToSlash(conf.Filename) + `"}`
  43 +
  44 + log := logs.GetBeeLogger()
  45 +
  46 + log.SetLogger(logs.AdapterFile, filename)
  47 + ilv, err := strconv.Atoi(conf.Level)
  48 + if err != nil {
  49 + ilv = logs.LevelDebug
  50 + }
  51 + log.SetLevel(ilv)
  52 + log.EnableFuncCallDepth(true)
  53 + log.SetLogFuncCallDepth(5)
  54 + return log
  55 +}
  56 +
  57 +func Debug(args ...interface{}) {
  58 + beego.Debug(args...)
  59 +}
  60 +func Info(args ...interface{}) {
  61 + beego.Info(args...)
  62 +}
  63 +func Warn(args ...interface{}) {
  64 + beego.Warn(args...)
  65 +}
  66 +func Error(args ...interface{}) {
  67 + beego.Error(args...)
  68 +}
18 69
@@ -4,14 +4,20 @@ import ( @@ -4,14 +4,20 @@ import (
4 "github.com/astaxie/beego" 4 "github.com/astaxie/beego"
5 "github.com/linmadan/egglib-go/web/beego/filters" 5 "github.com/linmadan/egglib-go/web/beego/filters"
6 . "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/log" 6 . "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/log"
  7 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/port/beego/middleware"
7 _ "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/port/beego/routers" 8 _ "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/port/beego/routers"
8 "time" 9 "time"
9 ) 10 )
10 11
11 func init() { 12 func init() {
12 beego.InsertFilter("/*", beego.BeforeExec, filters.CreateRequestBodyFilter()) 13 beego.InsertFilter("/*", beego.BeforeExec, filters.CreateRequestBodyFilter())
13 - beego.InsertFilter("/*", beego.BeforeExec, filters.CreateRequstLogFilter(Logger))  
14 - beego.InsertFilter("/*", beego.AfterExec, filters.CreateResponseLogFilter(Logger), false) 14 +
  15 + //beego.InsertFilter("/*", beego.BeforeExec, filters.CreateRequstLogFilter(Logger))
  16 + //beego.InsertFilter("/*", beego.AfterExec, filters.CreateResponseLogFilter(Logger), false)
  17 +
  18 + beego.InsertFilter("/*", beego.BeforeExec, middleware.CreateRequstLogFilter(Logger))
  19 + beego.InsertFilter("/*", beego.AfterExec, middleware.CreateResponseLogFilter(Logger), false)
  20 +
15 timeLocal, _ := time.LoadLocation("Asia/Chongqing") 21 timeLocal, _ := time.LoadLocation("Asia/Chongqing")
16 time.Local = timeLocal 22 time.Local = timeLocal
17 } 23 }
@@ -399,10 +399,14 @@ func (controller *SuMoneyController) ImportExchangeList () { @@ -399,10 +399,14 @@ func (controller *SuMoneyController) ImportExchangeList () {
399 json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), importExchangeCashListCommand) 399 json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), importExchangeCashListCommand)
400 400
401 //where := controller.GetString("where") 401 //where := controller.GetString("where")
  402 + fmt.Printf("%+v\n", importExchangeCashListCommand)
  403 + activityId, _ := strconv.ParseInt(interface{}(importExchangeCashListCommand.Where).(map[string]interface{})["activityId"].(string), 10, 64)
  404 + fmt.Print(activityId, "\n")
  405 + fmt.Print(int64(interface{}(importExchangeCashListCommand.Where).(map[string]interface{})["uid"].(float64)), "\n")
402 406
403 file, h, _ := controller.GetFile("file") 407 file, h, _ := controller.GetFile("file")
404 408
405 - jsonMap := make(map[string]interface{}) 409 + //jsonMap := make(map[string]interface{})
406 410
407 //if reflect.TypeOf(where).Kind() == reflect.TypeOf(jsonMap).Kind() { // 传入json 411 //if reflect.TypeOf(where).Kind() == reflect.TypeOf(jsonMap).Kind() { // 传入json
408 // jsonMap = map[string]interface{}{ 412 // jsonMap = map[string]interface{}{
@@ -413,10 +417,10 @@ func (controller *SuMoneyController) ImportExchangeList () { @@ -413,10 +417,10 @@ func (controller *SuMoneyController) ImportExchangeList () {
413 // _ := json.Unmarshal([]byte(where), &jsonMap) 417 // _ := json.Unmarshal([]byte(where), &jsonMap)
414 //} 418 //}
415 419
416 - fmt.Printf("%+v\n", jsonMap) 420 + //fmt.Printf("%+v\n", jsonMap)
417 421
418 - tmp, _ := strconv.ParseInt(jsonMap["activityId"].(string), 10, 64 )  
419 - fmt.Print(tmp, "\n") 422 + //tmp, _ := strconv.ParseInt(jsonMap["activityId"].(string), 10, 64 )
  423 + //fmt.Print(tmp, "\n")
420 424
421 // 错误信息返回 425 // 错误信息返回
422 var response utils.JsonResponse 426 var response utils.JsonResponse
@@ -492,14 +496,6 @@ func (controller *SuMoneyController) ImportExchangeList () { @@ -492,14 +496,6 @@ func (controller *SuMoneyController) ImportExchangeList () {
492 496
493 //fmt.Print(len(rows), "\n") 497 //fmt.Print(len(rows), "\n")
494 498
495 - //if personNameMatch, ok := where.(map[string]interface{})["personNameMatch"]; ok && (personNameMatch != "") {  
496 - // query = query.Where(`exchange_cash_person_list.employee_name LIKE ?`, fmt.Sprintf("%%%s%%", personNameMatch.(string)))  
497 - //}  
498 - //if activityId, ok := where.(map[string]interface{})["activityId"]; ok && activityId != "" {  
499 - // float, _ := strconv.ParseFloat(activityId.(string),64)  
500 - // query = query.Where("exchange_cash_person_list.activity_id = ?", float)  
501 - //}  
502 -  
503 for i, row := range rows { 499 for i, row := range rows {
504 //fmt.Printf("Row Number:%d, Row: %+v\n, Row Length: %d\n", i, row, len(row)) 500 //fmt.Printf("Row Number:%d, Row: %+v\n, Row Length: %d\n", i, row, len(row))
505 if i > 2 && len(row) > 1 { 501 if i > 2 && len(row) > 1 {
  1 +package middleware
  2 +
  3 +import (
  4 + "encoding/json"
  5 + "fmt"
  6 + "github.com/astaxie/beego/context"
  7 + "github.com/astaxie/beego/logs"
  8 + "io/ioutil"
  9 + "net/http"
  10 + "time"
  11 +)
  12 +
  13 +func CreateRequstLogFilter(logger *logs.BeeLogger) func(ctx *context.Context) {
  14 + return func(ctx *context.Context) {
  15 + requestId := fmt.Sprintf("%v.%v.%v ", ctx.Input.Method(), ctx.Input.URI(), time.Now().UnixNano())
  16 + ctx.Request.Header.Add("requestId", requestId)
  17 + var body string = "{}"
  18 + if ctx.Input.GetData("requestBody") != nil {
  19 + body = string(ctx.Input.GetData("requestBody").([]byte))
  20 + }
  21 + logger.Debug(fmt.Sprintf("====>Recv User:%v RequestId:%s \n Auth=%v \n BodyData:%s", ctx.Input.GetData("UserId"), requestId, ctx.Input.Header("x-mmm-accesstoken"), body))
  22 + }
  23 +}
  24 +
  25 +func CreateResponseLogFilter(logger *logs.BeeLogger) func(ctx *context.Context) {
  26 + return func(ctx *context.Context) {
  27 + requestId := ctx.Request.Header.Get("requestId")
  28 + body, _ := json.Marshal(ctx.Input.GetData("outputData"))
  29 + if len(body) > 1000 {
  30 + body = body[:1000]
  31 + }
  32 + logger.Debug(fmt.Sprintf("<====Send RequestId:%v BodyData:%s", requestId, body))
  33 + }
  34 +}
  35 +
  36 +func CreateRequestBodyFilter() func(ctx *context.Context) {
  37 + return func(ctx *context.Context) {
  38 + if ctx.Request.Method == http.MethodPost || ctx.Request.Method == http.MethodPut {
  39 + body, _ := ioutil.ReadAll(ctx.Request.Body)
  40 + if len(body) == 0 {
  41 + body = []byte("{}")
  42 + }
  43 + ctx.Input.SetData("requestBody", body)
  44 + ctx.Request.Body.Close()
  45 + }
  46 + }
  47 +}