log.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package log
  2. import (
  3. "designs/global"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. "go.uber.org/zap"
  7. )
  8. type causer interface {
  9. Format(s fmt.State, verb rune)
  10. }
  11. // 会打印error时的堆栈
  12. func ErrorWithStack(err error, msg ...string) error {
  13. titleMsg := ""
  14. if len(msg) > 0 {
  15. titleMsg = msg[0]
  16. }
  17. if _, ok := err.(causer); ok {
  18. global.App.Log.Errorf("%s%+v", titleMsg, errors.WithStack(err))
  19. } else {
  20. global.App.Log.Error(titleMsg, err.Error(), zap.StackSkip("", 1))
  21. }
  22. return err
  23. }
  24. func Debug(args ...interface{}) {
  25. global.App.Log.Debug(args...)
  26. }
  27. func Info(args ...interface{}) {
  28. global.App.Log.Info(args...)
  29. }
  30. func Warn(args ...interface{}) {
  31. global.App.Log.Warn(args...)
  32. }
  33. func Error(args ...interface{}) {
  34. global.App.Log.Error(args...)
  35. }
  36. func Debugf(format string, args ...interface{}) {
  37. global.App.Log.Debugf(format, args...)
  38. }
  39. func Infof(format string, args ...interface{}) {
  40. global.App.Log.Infof(format, args...)
  41. }
  42. func Warnf(format string, args ...interface{}) {
  43. global.App.Log.Warnf(format, args...)
  44. }
  45. func Errorf(format string, args ...interface{}) {
  46. global.App.Log.Errorf(format, args...)
  47. }