蛋蛋星球-制度模式
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 月之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package zhios_order_relate_logx
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "go.uber.org/zap"
  7. )
  8. type LogX struct {
  9. logger *zap.Logger
  10. atomLevel *zap.AtomicLevel
  11. }
  12. type Field = zap.Field
  13. type FieldMap map[string]interface{}
  14. // 判断其他类型--start
  15. func getFields(msg string, format bool, args ...interface{}) (string, []Field) {
  16. var str []interface{}
  17. var fields []zap.Field
  18. if len(args) > 0 {
  19. for _, v := range args {
  20. if f, ok := v.(Field); ok {
  21. fields = append(fields, f)
  22. } else if f, ok := v.(FieldMap); ok {
  23. fields = append(fields, formatFieldMap(f)...)
  24. } else {
  25. str = append(str, AnyToString(v))
  26. }
  27. }
  28. if format {
  29. return fmt.Sprintf(msg, str...), fields
  30. }
  31. str = append([]interface{}{msg}, str...)
  32. return fmt.Sprintln(str...), fields
  33. }
  34. return msg, []Field{}
  35. }
  36. func (l *LogX) Debug(s interface{}, args ...interface{}) error {
  37. es, e := checkErr(s)
  38. if es != "" {
  39. msg, field := getFields(es, false, args...)
  40. l.logger.Debug(msg, field...)
  41. }
  42. return e
  43. }
  44. func (l *LogX) Info(s interface{}, args ...interface{}) error {
  45. es, e := checkErr(s)
  46. if es != "" {
  47. msg, field := getFields(es, false, args...)
  48. l.logger.Info(msg, field...)
  49. }
  50. return e
  51. }
  52. func (l *LogX) Warn(s interface{}, args ...interface{}) error {
  53. es, e := checkErr(s)
  54. if es != "" {
  55. msg, field := getFields(es, false, args...)
  56. l.logger.Warn(msg, field...)
  57. }
  58. return e
  59. }
  60. func (l *LogX) Error(s interface{}, args ...interface{}) error {
  61. es, e := checkErr(s)
  62. if es != "" {
  63. msg, field := getFields(es, false, args...)
  64. l.logger.Error(msg, field...)
  65. }
  66. return e
  67. }
  68. func (l *LogX) DPanic(s interface{}, args ...interface{}) error {
  69. es, e := checkErr(s)
  70. if es != "" {
  71. msg, field := getFields(es, false, args...)
  72. l.logger.DPanic(msg, field...)
  73. }
  74. return e
  75. }
  76. func (l *LogX) Panic(s interface{}, args ...interface{}) error {
  77. es, e := checkErr(s)
  78. if es != "" {
  79. msg, field := getFields(es, false, args...)
  80. l.logger.Panic(msg, field...)
  81. }
  82. return e
  83. }
  84. func (l *LogX) Fatal(s interface{}, args ...interface{}) error {
  85. es, e := checkErr(s)
  86. if es != "" {
  87. msg, field := getFields(es, false, args...)
  88. l.logger.Fatal(msg, field...)
  89. }
  90. return e
  91. }
  92. func checkErr(s interface{}) (string, error) {
  93. switch e := s.(type) {
  94. case error:
  95. return e.Error(), e
  96. case string:
  97. return e, errors.New(e)
  98. case []byte:
  99. return string(e), nil
  100. default:
  101. return "", nil
  102. }
  103. }
  104. func (l *LogX) LogError(err error) error {
  105. return l.Error(err.Error())
  106. }
  107. func (l *LogX) Debugf(msg string, args ...interface{}) error {
  108. s, f := getFields(msg, true, args...)
  109. l.logger.Debug(s, f...)
  110. return errors.New(s)
  111. }
  112. func (l *LogX) Infof(msg string, args ...interface{}) error {
  113. s, f := getFields(msg, true, args...)
  114. l.logger.Info(s, f...)
  115. return errors.New(s)
  116. }
  117. func (l *LogX) Warnf(msg string, args ...interface{}) error {
  118. s, f := getFields(msg, true, args...)
  119. l.logger.Warn(s, f...)
  120. return errors.New(s)
  121. }
  122. func (l *LogX) Errorf(msg string, args ...interface{}) error {
  123. s, f := getFields(msg, true, args...)
  124. l.logger.Error(s, f...)
  125. return errors.New(s)
  126. }
  127. func (l *LogX) DPanicf(msg string, args ...interface{}) error {
  128. s, f := getFields(msg, true, args...)
  129. l.logger.DPanic(s, f...)
  130. return errors.New(s)
  131. }
  132. func (l *LogX) Panicf(msg string, args ...interface{}) error {
  133. s, f := getFields(msg, true, args...)
  134. l.logger.Panic(s, f...)
  135. return errors.New(s)
  136. }
  137. func (l *LogX) Fatalf(msg string, args ...interface{}) error {
  138. s, f := getFields(msg, true, args...)
  139. l.logger.Fatal(s, f...)
  140. return errors.New(s)
  141. }
  142. func AnyToString(raw interface{}) string {
  143. switch i := raw.(type) {
  144. case []byte:
  145. return string(i)
  146. case int:
  147. return strconv.FormatInt(int64(i), 10)
  148. case int64:
  149. return strconv.FormatInt(i, 10)
  150. case float32:
  151. return strconv.FormatFloat(float64(i), 'f', 2, 64)
  152. case float64:
  153. return strconv.FormatFloat(i, 'f', 2, 64)
  154. case uint:
  155. return strconv.FormatInt(int64(i), 10)
  156. case uint8:
  157. return strconv.FormatInt(int64(i), 10)
  158. case uint16:
  159. return strconv.FormatInt(int64(i), 10)
  160. case uint32:
  161. return strconv.FormatInt(int64(i), 10)
  162. case uint64:
  163. return strconv.FormatInt(int64(i), 10)
  164. case int8:
  165. return strconv.FormatInt(int64(i), 10)
  166. case int16:
  167. return strconv.FormatInt(int64(i), 10)
  168. case int32:
  169. return strconv.FormatInt(int64(i), 10)
  170. case string:
  171. return i
  172. case error:
  173. return i.Error()
  174. }
  175. return fmt.Sprintf("%#v", raw)
  176. }