elk.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package app
  2. import (
  3. "duoduo/conf"
  4. "duoduo/tools"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "github.com/olivere/elastic/v7"
  8. "github.com/sirupsen/logrus"
  9. "gopkg.in/sohlich/elogrus.v7"
  10. "log"
  11. )
  12. var logs *logrus.Logger
  13. func InitLogElk() {
  14. fmt.Println("InitLogElk")
  15. //加载配置
  16. confIni, errConf := conf.ConnIni()
  17. if errConf != nil {
  18. fmt.Println(errConf)
  19. }
  20. url := confIni.MustValue("elk", "url")
  21. host := confIni.MustValue("elk", "host")
  22. index := confIni.MustValue("elk", "index")
  23. logs = logrus.New()
  24. fmt.Println("url = ", url)
  25. client, err := elastic.NewSimpleClient(elastic.SetURL(url))
  26. if err != nil {
  27. fmt.Println("err = ", err.Error())
  28. log.Panic(err)
  29. }
  30. hook, err := elogrus.NewAsyncElasticHook(client, host, logrus.InfoLevel, index)
  31. if err != nil {
  32. fmt.Println("err = ", err.Error())
  33. log.Panic(err)
  34. }
  35. logs.Hooks.Add(hook)
  36. logs.WithFields(logrus.Fields{
  37. "name": "joe",
  38. "age": 42,
  39. }).Error("elk start!")
  40. }
  41. func GetLog() *logrus.Logger {
  42. if logs == nil {
  43. return logrus.New()
  44. }
  45. return logs
  46. }
  47. func ElkError(msg interface{}) {
  48. //log.Error(msg)
  49. }
  50. func ELKLogs(c *gin.Context, json string) {
  51. logs.WithFields(logrus.Fields{
  52. "Url": c.Request.RequestURI,
  53. "Status": c.Writer.Status(),
  54. "Method": c.Request.Method,
  55. }).Infof("请求参数:%s", json)
  56. }
  57. func OrderELKLogs(url string, method string, orderId string, json interface{}) {
  58. jsonStr, err := tools.JsonMarshal(json)
  59. if err != nil {
  60. logs.WithFields(logrus.Fields{
  61. "Url": url,
  62. "Method": method,
  63. "OrderId": orderId,
  64. }).Error("错误说明:结构体转json失败 %s", err.Error())
  65. return
  66. }
  67. logs.WithFields(logrus.Fields{
  68. "Url": url,
  69. "Method": method,
  70. "OrderId": orderId,
  71. }).Infof("请求参数:%s", jsonStr)
  72. }
  73. func FromDataELKLogs(url string, method string, orderId string, data string) {
  74. logs.WithFields(logrus.Fields{
  75. "Url": url,
  76. "Method": method,
  77. "OrderId": orderId,
  78. }).Infof("请求参数:%s", data)
  79. }
  80. func OrderOutDataELKLogs(url string, method string, orderId string, json interface{}) {
  81. jsonStr, err := tools.JsonMarshal(json)
  82. if err != nil {
  83. logs.WithFields(logrus.Fields{
  84. "Url": url,
  85. "Method": method,
  86. "OrderId": orderId,
  87. }).Error("返回错误说明:结构体转json失败 %s", err.Error())
  88. return
  89. }
  90. logs.WithFields(logrus.Fields{
  91. "Url": url,
  92. "Method": method,
  93. "OrderId": orderId,
  94. }).Infof("返回参数:%s", jsonStr)
  95. }
  96. func TispELKLogs(url string, method string, json string) {
  97. logs.WithFields(logrus.Fields{
  98. "Url": url,
  99. "Method": method,
  100. }).Infof("请求参数:%s", json)
  101. }
  102. func TispELKOutDataLogs(url string, method string, json string) {
  103. logs.WithFields(logrus.Fields{
  104. "Url": url,
  105. "Method": method,
  106. }).Infof("返回参数:%s", json)
  107. }
  108. func TispELKOutDataErrorLogs(url string, method string, json string) {
  109. logs.WithFields(logrus.Fields{
  110. "Url": url,
  111. "Method": method,
  112. }).Error("错误说明:%s", json)
  113. }