作者 陈志颖

feat:添加日志模块

2020/11/22 07:07:43.485 [I] [???:0] http server Running on http://:8082
2020/11/22 07:07:58.794 [D] [server.go:2843] ====>Recv User:<nil> RequestId:POST./cash-pool/activity/exchange-list/import.1606000078794641000
Auth=
BodyData:
2020/11/22 07:07:58.795 [C] [iface.go:261] the request url is /cash-pool/activity/exchange-list/import
2020/11/22 07:07:58.795 [C] [iface.go:261] Handler crashed with error interface conversion: interface {} is nil, not string
2020/11/22 07:07:58.796 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/runtime/panic.go:975
2020/11/22 07:07:58.796 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/runtime/iface.go:261
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
2020/11/22 07:07:58.796 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/reflect/value.go:475
2020/11/22 07:07:58.796 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/reflect/value.go:336
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
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
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
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
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)
2020/11/22 07:08:18.381 [D] [server.go:2843] ====>Recv User:<nil> RequestId:GET./cash-pool/cash-pool?companyId=311.1606000098381710000
Auth=
BodyData:{}
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"}
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
2020/11/22 07:08:50.492 [D] [server.go:2843] ====>Recv User:<nil> RequestId:POST./cash-pool/activity/exchange-list/import.1606000130492709000
Auth=
BodyData:
2020/11/22 07:08:50.493 [C] [iface.go:261] the request url is /cash-pool/activity/exchange-list/import
2020/11/22 07:08:50.493 [C] [iface.go:261] Handler crashed with error interface conversion: interface {} is nil, not string
2020/11/22 07:08:50.493 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/runtime/panic.go:975
2020/11/22 07:08:50.493 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/runtime/iface.go:261
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
2020/11/22 07:08:50.493 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/reflect/value.go:475
2020/11/22 07:08:50.493 [C] [iface.go:261] /usr/local/Cellar/go/1.15.2/libexec/src/reflect/value.go:336
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
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
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
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
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)
2020/11/22 07:09:10.243 [D] [server.go:2843] ====>Recv User:<nil> RequestId:POST./cash-pool/activity/exchange-list/import.1606000150243471000
Auth=
BodyData:{
"where": {
"activityId": "80",
"uid": 3242227175192320
}
}
2020/11/22 07:09:28.882 [I] [???:0] http server Running on http://:8082
... ...
... ... @@ -8,6 +8,9 @@ var LOG_LEVEL = "debug"
var ABILITY_SERVICE_HOST = "https://suplus-worth-app-gateway-dev.fjmaimaimai.com"
var MMM_OPEN_API_SERVICE_HOST = "http://mmm-open-api-dev.fjmaimaimai.com"
//var LOG_LEVEL = "debug"
var LOG_File = "app.log"
func init() {
if os.Getenv("LOG_LEVEL") != "" {
LOG_LEVEL = os.Getenv("LOG_LEVEL")
... ...
package constant
//package constant
//var Logger log.Logger
// TODO 日志输出
//func init() {
// Logger = logrus.NewLogrusLogger()
// Logger.SetServiceName(constant.SERVICE_NAME)
// Logger.SetLevel(constant.LOG_LEVEL)
//}
package log
import (
"github.com/linmadan/egglib-go/log"
"github.com/linmadan/egglib-go/log/logrus"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/constant"
"path/filepath"
"strconv"
)
var Logger log.Logger
var Logger *logs.BeeLogger
// TODO 日志输出
func init() {
Logger = logrus.NewLogrusLogger()
Logger.SetServiceName(constant.SERVICE_NAME)
Logger.SetLevel(constant.LOG_LEVEL)
Logger = NewBeegoLogger(LoggerConfig{
Filename: constant.LOG_File,
Level: constant.LOG_LEVEL,
})
}
type LoggerConfig struct {
Level string `json:"level,omitempty"`
Filename string `json:"filename,omitempty"`
MaxSize int `json:"max_size,omitempty"`
MaxBackups int `json:"max_backups,omitempty"`
MaxAge int `json:"max_age,omitempty"`
Compress bool `json:"compress,omitempty"`
}
func NewBeegoLogger(conf LoggerConfig) *logs.BeeLogger {
filename := `{"filename":"` + filepath.ToSlash(conf.Filename) + `"}`
log := logs.GetBeeLogger()
log.SetLogger(logs.AdapterFile, filename)
ilv, err := strconv.Atoi(conf.Level)
if err != nil {
ilv = logs.LevelDebug
}
log.SetLevel(ilv)
log.EnableFuncCallDepth(true)
log.SetLogFuncCallDepth(5)
return log
}
func Debug(args ...interface{}) {
beego.Debug(args...)
}
func Info(args ...interface{}) {
beego.Info(args...)
}
func Warn(args ...interface{}) {
beego.Warn(args...)
}
func Error(args ...interface{}) {
beego.Error(args...)
}
... ...
... ... @@ -4,14 +4,20 @@ import (
"github.com/astaxie/beego"
"github.com/linmadan/egglib-go/web/beego/filters"
. "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/log"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/port/beego/middleware"
_ "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/port/beego/routers"
"time"
)
func init() {
beego.InsertFilter("/*", beego.BeforeExec, filters.CreateRequestBodyFilter())
beego.InsertFilter("/*", beego.BeforeExec, filters.CreateRequstLogFilter(Logger))
beego.InsertFilter("/*", beego.AfterExec, filters.CreateResponseLogFilter(Logger), false)
//beego.InsertFilter("/*", beego.BeforeExec, filters.CreateRequstLogFilter(Logger))
//beego.InsertFilter("/*", beego.AfterExec, filters.CreateResponseLogFilter(Logger), false)
beego.InsertFilter("/*", beego.BeforeExec, middleware.CreateRequstLogFilter(Logger))
beego.InsertFilter("/*", beego.AfterExec, middleware.CreateResponseLogFilter(Logger), false)
timeLocal, _ := time.LoadLocation("Asia/Chongqing")
time.Local = timeLocal
}
... ...
... ... @@ -399,10 +399,14 @@ func (controller *SuMoneyController) ImportExchangeList () {
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), importExchangeCashListCommand)
//where := controller.GetString("where")
fmt.Printf("%+v\n", importExchangeCashListCommand)
activityId, _ := strconv.ParseInt(interface{}(importExchangeCashListCommand.Where).(map[string]interface{})["activityId"].(string), 10, 64)
fmt.Print(activityId, "\n")
fmt.Print(int64(interface{}(importExchangeCashListCommand.Where).(map[string]interface{})["uid"].(float64)), "\n")
file, h, _ := controller.GetFile("file")
jsonMap := make(map[string]interface{})
//jsonMap := make(map[string]interface{})
//if reflect.TypeOf(where).Kind() == reflect.TypeOf(jsonMap).Kind() { // 传入json
// jsonMap = map[string]interface{}{
... ... @@ -413,10 +417,10 @@ func (controller *SuMoneyController) ImportExchangeList () {
// _ := json.Unmarshal([]byte(where), &jsonMap)
//}
fmt.Printf("%+v\n", jsonMap)
//fmt.Printf("%+v\n", jsonMap)
tmp, _ := strconv.ParseInt(jsonMap["activityId"].(string), 10, 64 )
fmt.Print(tmp, "\n")
//tmp, _ := strconv.ParseInt(jsonMap["activityId"].(string), 10, 64 )
//fmt.Print(tmp, "\n")
// 错误信息返回
var response utils.JsonResponse
... ... @@ -492,14 +496,6 @@ func (controller *SuMoneyController) ImportExchangeList () {
//fmt.Print(len(rows), "\n")
//if personNameMatch, ok := where.(map[string]interface{})["personNameMatch"]; ok && (personNameMatch != "") {
// query = query.Where(`exchange_cash_person_list.employee_name LIKE ?`, fmt.Sprintf("%%%s%%", personNameMatch.(string)))
//}
//if activityId, ok := where.(map[string]interface{})["activityId"]; ok && activityId != "" {
// float, _ := strconv.ParseFloat(activityId.(string),64)
// query = query.Where("exchange_cash_person_list.activity_id = ?", float)
//}
for i, row := range rows {
//fmt.Printf("Row Number:%d, Row: %+v\n, Row Length: %d\n", i, row, len(row))
if i > 2 && len(row) > 1 {
... ...
package middleware
import (
"encoding/json"
"fmt"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/logs"
"io/ioutil"
"net/http"
"time"
)
func CreateRequstLogFilter(logger *logs.BeeLogger) func(ctx *context.Context) {
return func(ctx *context.Context) {
requestId := fmt.Sprintf("%v.%v.%v ", ctx.Input.Method(), ctx.Input.URI(), time.Now().UnixNano())
ctx.Request.Header.Add("requestId", requestId)
var body string = "{}"
if ctx.Input.GetData("requestBody") != nil {
body = string(ctx.Input.GetData("requestBody").([]byte))
}
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))
}
}
func CreateResponseLogFilter(logger *logs.BeeLogger) func(ctx *context.Context) {
return func(ctx *context.Context) {
requestId := ctx.Request.Header.Get("requestId")
body, _ := json.Marshal(ctx.Input.GetData("outputData"))
if len(body) > 1000 {
body = body[:1000]
}
logger.Debug(fmt.Sprintf("<====Send RequestId:%v BodyData:%s", requestId, body))
}
}
func CreateRequestBodyFilter() func(ctx *context.Context) {
return func(ctx *context.Context) {
if ctx.Request.Method == http.MethodPost || ctx.Request.Method == http.MethodPut {
body, _ := ioutil.ReadAll(ctx.Request.Body)
if len(body) == 0 {
body = []byte("{}")
}
ctx.Input.SetData("requestBody", body)
ctx.Request.Body.Close()
}
}
}
... ...