package app
import (
"duoduo/conf"
"duoduo/tools"
"fmt"
"github.com/gin-gonic/gin"
"github.com/olivere/elastic/v7"
"github.com/sirupsen/logrus"
"gopkg.in/sohlich/elogrus.v7"
"log"
)
var logs *logrus.Logger
func InitLogElk() {
fmt.Println("InitLogElk")
//加载配置
confIni, errConf := conf.ConnIni()
if errConf != nil {
fmt.Println(errConf)
}
url := confIni.MustValue("elk", "url")
host := confIni.MustValue("elk", "host")
index := confIni.MustValue("elk", "index")
logs = logrus.New()
fmt.Println("url = ", url)
client, err := elastic.NewSimpleClient(elastic.SetURL(url))
if err != nil {
fmt.Println("err = ", err.Error())
log.Panic(err)
}
hook, err := elogrus.NewAsyncElasticHook(client, host, logrus.InfoLevel, index)
if err != nil {
fmt.Println("err = ", err.Error())
log.Panic(err)
}
logs.Hooks.Add(hook)
logs.WithFields(logrus.Fields{
"name": "joe",
"age": 42,
}).Error("elk start!")
}
func GetLog() *logrus.Logger {
if logs == nil {
return logrus.New()
}
return logs
}
func ElkError(msg interface{}) {
//log.Error(msg)
}
func ELKLogs(c *gin.Context, json string) {
logs.WithFields(logrus.Fields{
"Url": c.Request.RequestURI,
"Status": c.Writer.Status(),
"Method": c.Request.Method,
}).Infof("请求参数:%s", json)
}
func OrderELKLogs(url string, method string, orderId string, json interface{}) {
jsonStr, err := tools.JsonMarshal(json)
if err != nil {
logs.WithFields(logrus.Fields{
"Url": url,
"Method": method,
"OrderId": orderId,
}).Error("错误说明:结构体转json失败 %s", err.Error())
return
}
logs.WithFields(logrus.Fields{
"Url": url,
"Method": method,
"OrderId": orderId,
}).Infof("请求参数:%s", jsonStr)
}
func FromDataELKLogs(url string, method string, orderId string, data string) {
logs.WithFields(logrus.Fields{
"Url": url,
"Method": method,
"OrderId": orderId,
}).Infof("请求参数:%s", data)
}
func OrderOutDataELKLogs(url string, method string, orderId string, json interface{}) {
jsonStr, err := tools.JsonMarshal(json)
if err != nil {
logs.WithFields(logrus.Fields{
"Url": url,
"Method": method,
"OrderId": orderId,
}).Error("返回错误说明:结构体转json失败 %s", err.Error())
return
}
logs.WithFields(logrus.Fields{
"Url": url,
"Method": method,
"OrderId": orderId,
}).Infof("返回参数:%s", jsonStr)
}
func TispELKLogs(url string, method string, json string) {
logs.WithFields(logrus.Fields{
"Url": url,
"Method": method,
}).Infof("请求参数:%s", json)
}
func TispELKOutDataLogs(url string, method string, json string) {
logs.WithFields(logrus.Fields{
"Url": url,
"Method": method,
}).Infof("返回参数:%s", json)
}
func TispELKOutDataErrorLogs(url string, method string, json string) {
logs.WithFields(logrus.Fields{
"Url": url,
"Method": method,
}).Error("错误说明:%s", json)
}