logger.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package base
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "github.com/sirupsen/logrus"
  7. "os"
  8. "strings"
  9. "time"
  10. )
  11. // const values
  12. const (
  13. NOLOGFlAG string = "NOLOGFlAG"
  14. TimestampFormat string = "2003-01-02 15:04:05.000"
  15. )
  16. // getLogLevel for API
  17. func getLogLevel(loglevel string) (logrus.Level, bool) {
  18. switch loglevel {
  19. case "debug":
  20. return logrus.DebugLevel, true
  21. case "info":
  22. return logrus.InfoLevel, true
  23. case "warn":
  24. return logrus.WarnLevel, true
  25. case "error":
  26. return logrus.ErrorLevel, true
  27. case "fatal":
  28. return logrus.FatalLevel, true
  29. case "panic":
  30. return logrus.PanicLevel, true
  31. default:
  32. return 0, false
  33. }
  34. }
  35. // InitLog for API
  36. func InitLog(loglevel string) {
  37. logrus.SetFormatter(&logrus.JSONFormatter{
  38. TimestampFormat: TimestampFormat,
  39. })
  40. logrus.SetOutput(os.Stdout)
  41. if level, exist := getLogLevel(loglevel); exist {
  42. logrus.SetLevel(level)
  43. } else {
  44. if GetJHYEnv() == ENV_DEV {
  45. logrus.SetLevel(logrus.DebugLevel)
  46. } else if GetJHYEnv() == ENV_TEST {
  47. logrus.SetLevel(logrus.DebugLevel)
  48. } else if GetJHYEnv() == ENV_PRODUCT {
  49. logrus.SetLevel(logrus.InfoLevel)
  50. }
  51. }
  52. }
  53. // Logger for API
  54. func Logger(ctx *gin.Context) *logrus.Entry {
  55. entry := logrus.NewEntry(logrus.StandardLogger()).WithFields(logrus.Fields{
  56. "requestId": GetRequestId(ctx),
  57. })
  58. return entry
  59. }
  60. // DebugLogger for API
  61. func DebugLogger(ctx *gin.Context, value ...interface{}) {
  62. if GetNoLogFlag(ctx) == true {
  63. return
  64. }
  65. if ctx != nil {
  66. Logger(ctx).Debug(value...)
  67. } else {
  68. logrus.Debug(value...)
  69. }
  70. }
  71. // DebugfLogger for API
  72. func DebugfLogger(ctx *gin.Context, format string, value ...interface{}) {
  73. if GetNoLogFlag(ctx) == true {
  74. return
  75. }
  76. if ctx != nil {
  77. Logger(ctx).Debugf(format, value...)
  78. } else {
  79. logrus.Debugf(format, value...)
  80. }
  81. }
  82. // InfoLogger for API
  83. func InfoLogger(ctx *gin.Context, value ...interface{}) {
  84. if GetNoLogFlag(ctx) == true {
  85. return
  86. }
  87. if ctx != nil {
  88. Logger(ctx).Info(value...)
  89. } else {
  90. logrus.Info(value...)
  91. }
  92. }
  93. // InfofLogger for API
  94. func InfofLogger(ctx *gin.Context, format string, value ...interface{}) {
  95. if GetNoLogFlag(ctx) == true {
  96. return
  97. }
  98. if ctx != nil {
  99. Logger(ctx).Infof(format, value...)
  100. } else {
  101. logrus.Infof(format, value...)
  102. }
  103. }
  104. // WarnLogger for API
  105. func WarnLogger(ctx *gin.Context, value ...interface{}) {
  106. if GetNoLogFlag(ctx) == true {
  107. return
  108. }
  109. if ctx != nil {
  110. Logger(ctx).Warn(value...)
  111. } else {
  112. logrus.Warn(value...)
  113. }
  114. }
  115. // WarnfLogger for API
  116. func WarnfLogger(ctx *gin.Context, format string, value ...interface{}) {
  117. if GetNoLogFlag(ctx) == true {
  118. return
  119. }
  120. if ctx != nil {
  121. Logger(ctx).Warnf(format, value...)
  122. } else {
  123. logrus.Warnf(format, value...)
  124. }
  125. }
  126. // ErrorLogger for API
  127. func ErrorLogger(ctx *gin.Context, value ...interface{}) {
  128. if ctx != nil {
  129. Logger(ctx).Error(value...)
  130. } else {
  131. logrus.Error(value...)
  132. }
  133. }
  134. // ErrorfLogger for API
  135. func ErrorfLogger(ctx *gin.Context, format string, value ...interface{}) {
  136. if ctx != nil {
  137. Logger(ctx).Errorf(format, value...)
  138. } else {
  139. logrus.Errorf(format, value...)
  140. }
  141. }
  142. // PanicLogger for API
  143. func PanicLogger(ctx *gin.Context, value ...interface{}) {
  144. if ctx != nil {
  145. Logger(ctx).Panic(value...)
  146. } else {
  147. logrus.Panic(value...)
  148. }
  149. }
  150. // PanicfLogger for API
  151. func PanicfLogger(ctx *gin.Context, format string, value ...interface{}) {
  152. if ctx != nil {
  153. Logger(ctx).Panicf(format, value...)
  154. } else {
  155. logrus.Panicf(format, value...)
  156. }
  157. }
  158. // SetNoLogFlag for API
  159. func SetNoLogFlag(ctx *gin.Context) {
  160. ctx.Set(NOLOGFlAG, true)
  161. }
  162. // GetNoLogFlag for API
  163. func GetNoLogFlag(ctx *gin.Context) bool {
  164. if ctx == nil {
  165. return false
  166. }
  167. flag, ok := ctx.Get(NOLOGFlAG)
  168. return ok == true && flag == true
  169. }
  170. // StackLogger for API
  171. func StackLogger(ctx *gin.Context, err error) {
  172. if !strings.Contains(fmt.Sprintf("%+v", err), "\n") {
  173. return
  174. }
  175. var info []byte
  176. if ctx != nil {
  177. info, _ = json.Marshal(map[string]interface{}{"time": time.Now().Format("2006-01-02 15:04:05"), "level": "error", "module": "errorstack", "requestId": GetRequestId(ctx)})
  178. } else {
  179. info, _ = json.Marshal(map[string]interface{}{"time": time.Now().Format("2006-01-02 15:04:05"), "level": "error", "module": "errorstack"})
  180. }
  181. fmt.Printf("%s\n-------------------stack-start-------------------\n%+v\n-------------------stack-end-------------------\n", string(info), err)
  182. }