123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- 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)
- }
|