Commit ad1d9bf1 by mushishixian

完善日志

parent e60af8cc
package controller
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/ichunt2019/logger"
"search_server/pkg/common"
"search_server/service"
)
......@@ -28,7 +28,7 @@ func Recommend(c *gin.Context) {
req := &common.RecommendRequest{}
err := c.Bind(&req) //使用的是post参数
if err != nil {
fmt.Println(err)
logger.Error("%s", err.Error())
}
if req.Flag == 0 {
common.Output(101, "参数不全", nil)
......
package controller
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/gomodule/redigo/redis"
"github.com/ichunt2019/logger"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"search_server/pkg/common"
......@@ -41,14 +41,13 @@ func GetDataIndex(c *gin.Context) {
defer redisCon.Close()
//获取stockFlag
res, err := redis.Bool(redisCon.Do("SADD", "search_keyword_member", r.Keyword+"_"+r.SupplierName))
fmt.Println(r.Keyword + "_" + r.SupplierName)
if err != nil {
fmt.Println(err)
logger.Error("%s", err.Error())
}
if res {
_, err := redis.Bool(redisCon.Do("INCR", config.Get("redis_all.SEARCH_API_TOTAL_PRE").String()+r.SupplierName))
if err != nil {
fmt.Println(err)
logger.Error("%s", err.Error())
}
}
r.FirstSearchPage = 2
......@@ -71,7 +70,8 @@ func GetDataIndex(c *gin.Context) {
result := service.GetReturnSupplierData(dullData)
result["total"] = gjson.Get(esResult, "hits.total").Int()
result["took"] = gjson.Get(esResult, "took").Int()
c.JSON(200, common.SuccessResponse(0, "", result))
common.Output(0, "", result)
return
}
//获取查询es的条件语句
......
......@@ -129,6 +129,7 @@ func NumberToHtml(number int) (html string) {
}
return
}
/**
* 生成纯小写字母的字符串
* 返回形式如下 : yuiopkdsi rnvewjeil xmiqplmza
......@@ -266,9 +267,6 @@ func auth() bool {
return false
}
// interface{}转为 []interface{}
//將任意類型的切片轉為 []interface{}
func CreateAnyTypeSlice(slice interface{}) ([]interface{}, bool) {
......@@ -300,3 +298,17 @@ func isSlice(arg interface{}) (val reflect.Value, ok bool) {
return
}
//向左补充字符串
func StrPadLeft(input string, padLength int, padString string) string {
output := padString
for padLength > len(output) {
output += output
}
if len(input) >= padLength {
return input
}
return output[:padLength-len(input)] + input
}
package common
import (
"encoding/json"
"github.com/gomodule/redigo/redis"
"github.com/ichunt2019/logger"
log "github.com/sirupsen/logrus"
"os"
"search_server/middleware"
"search_server/pkg/config"
"search_server/pkg/gredis"
"search_server/pkg/message"
"strconv"
"strings"
"time"
)
type SearchApiLog struct {
Msg string `json:"msg"`
MsgCode string `json:"msgCode"`
Ts int64 `json:"ts"`
DateStr string `json:"dateStr"`
App string `json:"app"`
ServerIp string `json:"serverIp"`
FileName string `json:"fileName"`
LineNo string `json:"lineNo"`
Method string `json:"method"`
}
func AnalyzeSearchError(code int, ip, errMsg, file, line, method string) (log SearchApiLog) {
//获取所有参数
request := make(map[string]string)
ctx := middleware.Context
ctx.MultipartForm()
for name, value := range ctx.Request.Form {
if value[0] != "" {
request[name] = strings.TrimSpace(value[0])
}
}
requestByte, _ := json.Marshal(request)
log.Msg = errMsg + string(requestByte)
codeStr := strconv.Itoa(code)
log.MsgCode = StrPadLeft(codeStr, 6, "0")
log.Ts = time.Now().Unix()
log.DateStr = time.Now().Format("2006-01-02 15:04:05")
log.App = "search_api"
log.ServerIp = ip
log.FileName = file
log.LineNo = line
log.Method = method
//还要判断是否要推送消息
unitSupplierLogCode := config.Cfg.Section("UNIT_SUPPLIER_LOG_CODE").KeysHash()
if unitSupplierLogCode[codeStr] == "" {
if !strings.Contains(strings.ToUpper(errMsg), "SOAP-ERROR") {
//推送到钉钉
result, err := message.DingDingPush(errMsg)
if err != nil || result.Errcode != 0 {
logger.Error("%s", err, result)
}
}
} else {
redisCon := gredis.Conn("search_w")
defer redisCon.Close()
key := config.Get("SEARCH_API_LOG.SEARCH_API_ERROR_PRE").String() + unitSupplierLogCode[codeStr]
_, err := redis.Bool(redisCon.Do("INCR", key))
if err != nil {
logger.Error("%s", err)
}
}
return
}
type LogFormatter struct{}
//格式详情
func (s *LogFormatter) Format(entry *log.Entry) ([]byte, error) {
msg := entry.Message + "\n"
return []byte(msg), nil
}
//写入到特定的文件夹给ELK使用的日志
func WriteSearchErrorLog(searchLog string) {
// 设置日志格式为json格式
log.SetFormatter(new(LogFormatter))
path := "../logs/search/"
//设置output,默认为stderr,可以为任何io.Writer,比如文件*os.File
//生成当天的日志
filePath := path + time.Now().Format("20060102") + ".log"
file, _ := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
log.SetOutput(file)
log.Error(searchLog)
}
package common
import (
"encoding/json"
"runtime"
"search_server/middleware"
"search_server/pkg/log_report"
"strconv"
"strings"
)
......@@ -48,12 +48,15 @@ func Output(errCode int, errMsg string, data interface{}) {
ErrMsg: errMsg,
Data: data,
}
if errCode >= 100 {
SearchApiLogger(errCode, errMsg)
}
ctx := middleware.Context
if ctx.DefaultQuery("callback", "") != "" {
ctx.JSONP(200, response)
} else {
referer := ctx.Request.Referer()
referer = strings.TrimRight(referer,"/")
referer = strings.TrimRight(referer, "/")
ctx.Header("Access-Control-Allow-Origin", referer)
ctx.Header("Access-Control-Allow-Credentials", "true")
//允许跨站访问的站点域名
......@@ -63,12 +66,13 @@ func Output(errCode int, errMsg string, data interface{}) {
}
//错误的搜索日志记录
func SearchApiLog(code int, msg string) {
pc, file, line, _ := runtime.Caller(0)
func SearchApiLogger(code int, msg string) {
pc, file, line, _ := runtime.Caller(2)
f := runtime.FuncForPC(pc)
ctx := middleware.Context
errMsg := "提示信息:" + msg + ",请求url:" + ctx.Request.URL.String()
lineNo := strconv.Itoa(line)
log_report.AnalyzeSearchError(code, ctx.ClientIP(), errMsg, file, lineNo, f.Name())
searchLog := AnalyzeSearchError(code, ctx.ClientIP(), errMsg, file, lineNo, f.Name())
searchLogByte, _ := json.Marshal(searchLog)
WriteSearchErrorLog(string(searchLogByte))
}
package log_report
import (
"encoding/json"
log "github.com/sirupsen/logrus"
"os"
)
type SearchApiLog struct {
Msg string
MsgCode int
Ts int64
DateStr string
App string
ServerIp string
FileName string
LineNo string
Method string
}
func AnalyzeSearchError(code int, ip, errMsg, file, line, method string) (log SearchApiLog) {
//log.Msg = "" + errMsg
//log.MsgCode = 1
//log.Ts = time.Now().Unix()
//log.DateStr = time.Now().Format("2006-01-02 15:04:05")
//log.App = "search_api"
//log.ServerIp = ip
//log.FileName = file
//log.LineNo = line
//log.Method = method
//
////还要判断是否要推送消息
//unitSupplierLogCode := config.Cfg.Section("UNIT_SUPPLIER_LOG_CODE").KeysHash()
//codeStr := strconv.Itoa(code)
//if unitSupplierLogCode[codeStr] == "" {
// if !strings.Contains(strings.ToUpper(errMsg), "SOAP-ERROR") {
// //推送到钉钉
// result, err := message.DingDingPush(errMsg)
// if err != nil || result.Errcode != 0 {
// //logger.Errorf("%s", err, result)
// }
// }
//} else {
// redisCon := gredis.Conn("search_w")
// defer redisCon.Close()
// key := config.Get("SEARCH_API_LOG.SEARCH_API_ERROR_PRE").String() + unitSupplierLogCode[codeStr]
// redisCon.Do("INCR", key)
//}
return
}
type LogFormatter struct{}
//格式详情
func (s *LogFormatter) Format(entry *log.Entry) ([]byte, error) {
msg := entry.Message
return []byte(msg), nil
}
//写入到特定的文件夹给ELK使用的日志
func WriteSearchErrorLog(searchLog SearchApiLog) {
logJson, _ := json.Marshal(searchLog)
// 设置日志格式为json格式
log.SetFormatter(new(LogFormatter))
//设置output,默认为stderr,可以为任何io.Writer,比如文件*os.File
file, _ := os.OpenFile("1.log", os.O_CREATE|os.O_WRONLY, 0666)
log.SetOutput(file)
log.Error(logJson)
}
func WriteSearchErrorLogTest(searchLog string) {
// 设置日志格式为json格式
log.SetFormatter(new(LogFormatter))
//设置output,默认为stderr,可以为任何io.Writer,比如文件*os.File
file, _ := os.OpenFile("1.log", os.O_CREATE|os.O_WRONLY, 0666)
log.SetOutput(file)
log.Error(searchLog)
}
......@@ -7,7 +7,7 @@ import (
func SetUp() (err error) {
logConfig := make(map[string]string)
logConfig["log_path"] = "logs"
logConfig["log_chan_size"] = "1000"
logConfig["log_chan_size"] = "5"
err = logger.InitLogger("file", logConfig)
if err != nil {
return err
......
......@@ -3,9 +3,9 @@ package message
import (
"encoding/json"
"fmt"
"github.com/syyongx/php2go"
"net/http"
"net/url"
"search_server/pkg/common"
"search_server/pkg/config"
"strconv"
"time"
......@@ -25,7 +25,7 @@ func SendMessage(mobile int64, keyWord string, content map[string]interface{}) {
"touser": {string(requestTel)},
"keyword": {keyWord},
"k1": {strconv.FormatInt(timeNow, 10)},
"k2": {common.Md5(common.Md5(strconv.FormatInt(timeNow, 10)) + apiMd5Str)},
"k2": {php2go.Md5(php2go.Md5(strconv.FormatInt(timeNow, 10)) + apiMd5Str)},
"is_ignore": {},
})
......
package routes
import (
"fmt"
"github.com/gin-gonic/gin"
"search_server/controller"
"search_server/middleware"
"search_server/pkg/common"
)
//初始化路由
......@@ -33,7 +33,8 @@ func InitRouter() *gin.Engine {
r.POST("/search/get_data", controller.GetDataIndex)
r.GET("/test", func(c *gin.Context) {
fmt.Println(c.Query("test"))
common.Output(1000, "testest", nil)
})
return r
}
......@@ -7,7 +7,7 @@ import (
"github.com/syyongx/php2go"
"reflect"
"search_server/boot"
"search_server/controller"
"search_server/pkg/common"
)
func StrRandom(lenNum int) string {
......@@ -47,8 +47,10 @@ func main() {
//fmt.Println(str)
//log.Error("sadsadsadsa")
//log.Error("sadsadsadsa")
//log_report.WriteSearchErrorLog("sdasdasdas")
common.Output(1000, "ceshiceshi", nil)
//log.Error("sadsadsadsa")
controller.GetData("LM358","arrow")
//controller.GetData("LM358","arrow")
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment