|
|
package Middleware
|
|
|
|
|
|
import (
|
|
|
"dsDataex/Utils/CommonUtil"
|
|
|
"dsDataex/Utils/ConfigUtil"
|
|
|
"dsDataex/Utils/KafkaUtil"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/sirupsen/logrus"
|
|
|
"github.com/tracer0tong/kafkalogrus"
|
|
|
"os"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
//转为东八区时间
|
|
|
type CSTFormatter struct {
|
|
|
logrus.Formatter
|
|
|
}
|
|
|
|
|
|
func (u CSTFormatter) Format(e *logrus.Entry) ([]byte, error) {
|
|
|
e.Time = e.Time.Local()
|
|
|
return u.Formatter.Format(e)
|
|
|
}
|
|
|
|
|
|
//add by zhangjun 2020-08-03
|
|
|
func stringEndWith(input string) bool {
|
|
|
|
|
|
if strings.Contains(input,".png"){ return true }
|
|
|
if strings.Contains(input,".jpg"){ return true }
|
|
|
if strings.Contains(input,".js"){ return true }
|
|
|
if strings.Contains(input,".css"){ return true }
|
|
|
if strings.Contains(input,".ico"){ return true }
|
|
|
if strings.Contains(input,".svg"){ return true}
|
|
|
if strings.Contains(input,".gif"){ return true }
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
func init() {
|
|
|
var err error
|
|
|
var hook *kafkalogrus.KafkaLogrusHook
|
|
|
|
|
|
//add by zhangjun 2020-08-03
|
|
|
//初始化 topic,多个 partition,提高数据批处理性能。
|
|
|
KafkaUtil.InitTopic(ConfigUtil.KafkaAccessLogTopic)
|
|
|
|
|
|
if hook, err = kafkalogrus.NewKafkaLogrusHook(
|
|
|
"kh",
|
|
|
logrus.AllLevels,
|
|
|
&logrus.JSONFormatter{},
|
|
|
//change by zhangjun 2020-08-03
|
|
|
//[]string{ConfigUtil.KafkaAddress},
|
|
|
ConfigUtil.KafkaBrokers,
|
|
|
ConfigUtil.KafkaAccessLogTopic,
|
|
|
true,
|
|
|
nil); err != nil {
|
|
|
panic(err)
|
|
|
}
|
|
|
|
|
|
//设置日志格式
|
|
|
logrus.SetFormatter(CSTFormatter{&logrus.JSONFormatter{}})
|
|
|
|
|
|
//add by zhangjun 2020-08-03
|
|
|
src, _:= os.OpenFile("./gin_server.log", os.O_APPEND|os.O_WRONLY, os.ModeAppend)
|
|
|
logrus.SetOutput(src)
|
|
|
|
|
|
logrus.AddHook(hook)
|
|
|
}
|
|
|
|
|
|
// 日志记录到 Kafka
|
|
|
func LoggerToKafka() gin.HandlerFunc {
|
|
|
return func(c *gin.Context) {
|
|
|
// 开始时间
|
|
|
startTime := time.Now().Local()
|
|
|
// 处理请求
|
|
|
c.Next()
|
|
|
// 结束时间
|
|
|
endTime := time.Now().Local()
|
|
|
// 执行时间
|
|
|
latencyTime := endTime.Sub(startTime).Seconds()
|
|
|
// 请求方式
|
|
|
reqMethod := c.Request.Method
|
|
|
// 请求路由
|
|
|
reqUri := c.Request.RequestURI
|
|
|
// 状态码
|
|
|
statusCode := c.Writer.Status()
|
|
|
// 请求IP
|
|
|
clientIP := c.ClientIP()
|
|
|
|
|
|
//统一认证的会话id
|
|
|
//var identityId = -1
|
|
|
//var personId = -1
|
|
|
var deviceId = -1
|
|
|
/*
|
|
|
cookie, e := c.Request.Cookie(ConfigUtil.SessionId)
|
|
|
if e == nil {
|
|
|
deviceId, identityId, personId = SsoUtil.AnalyzeSessionId(cookie.Value)
|
|
|
}
|
|
|
*/
|
|
|
|
|
|
personId, err := c.Cookie("person_id")
|
|
|
if err != nil {
|
|
|
personId = "-1"
|
|
|
}
|
|
|
identityId, err := c.Cookie("identity_id")
|
|
|
if err != nil {
|
|
|
identityId = "-1"
|
|
|
}
|
|
|
|
|
|
//add by zhangjun 2020-08-03
|
|
|
broswerId, err := c.Cookie("browser_id")
|
|
|
if err != nil {
|
|
|
broswerId = "-1"
|
|
|
}
|
|
|
var properties=make(map[string]interface{})
|
|
|
|
|
|
properties["req_method"]=reqMethod
|
|
|
properties["latency_time"]=latencyTime
|
|
|
properties["status_code"]=statusCode
|
|
|
|
|
|
strs:=strings.Split(reqUri,"/")
|
|
|
properties["req_action"]=strs[len(strs)-1]
|
|
|
|
|
|
//一般静态文件
|
|
|
if stringEndWith( strs[len(strs)-1]){
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//change by zhangjun 2020-08-03
|
|
|
// 日志格式
|
|
|
//logrus.WithFields(logrus.Fields{
|
|
|
// "status_code": statusCode,
|
|
|
// "access_time": startTime,
|
|
|
// "latency_time": latencyTime,
|
|
|
// "client_ip": clientIP,
|
|
|
// "req_method": reqMethod,
|
|
|
// "req_uri": reqUri,
|
|
|
// "device_id": deviceId,
|
|
|
// "identity_id": identityId,
|
|
|
// "person_id": personId,
|
|
|
//}).Info()
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
"system_id":ConfigUtil.ProjectName,
|
|
|
"datasource_id":ConfigUtil.KafkaAccessLogTopic,
|
|
|
"data_id":CommonUtil.GetUUID(),
|
|
|
|
|
|
"user_id":personId,
|
|
|
"identity":identityId,
|
|
|
"access_id":broswerId,
|
|
|
"access_ip":clientIP,
|
|
|
"access_way":deviceId,
|
|
|
|
|
|
"event_type":"101",
|
|
|
"event_name":"gin_webrequest",
|
|
|
"event_time":startTime.Format("2006/01/02 15:04:05"),
|
|
|
"event_uri":reqUri,
|
|
|
"event_seqno":0,
|
|
|
"collect_time":endTime.Format("2006/01/02 15:04:05"),
|
|
|
"event_property":properties,
|
|
|
}).Info()
|
|
|
}
|
|
|
}
|