log.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Package log provides the colored console logger setup and the business
  2. // Record* helpers, ported from sdf-serve pkg/common/log.
  3. package log
  4. import (
  5. "context"
  6. stdErrors "errors"
  7. "fmt"
  8. "udeServer/internal/apierr"
  9. "udeServer/internal/response"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. "go.uber.org/zap"
  12. "go.uber.org/zap/zapcore"
  13. )
  14. // SetupLoggerWithColor replaces the global zap logger with a colored
  15. // development configuration. It only takes effect when Log.Mode is "console";
  16. // otherwise go-zero keeps its default logging behaviour.
  17. func SetupLoggerWithColor(conf logx.LogConf) {
  18. if conf.Mode != "console" {
  19. return
  20. }
  21. cfg := zap.NewDevelopmentConfig()
  22. cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
  23. cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
  24. cfg.Level = zap.NewAtomicLevelAt(getZapLevel(conf.Level))
  25. logger, err := cfg.Build()
  26. if err != nil {
  27. panic(err)
  28. }
  29. zap.ReplaceGlobals(logger)
  30. }
  31. func getZapLevel(level string) zapcore.Level {
  32. switch level {
  33. case "debug":
  34. return zapcore.DebugLevel
  35. case "info":
  36. return zapcore.InfoLevel
  37. case "warn":
  38. return zapcore.WarnLevel
  39. case "error":
  40. return zapcore.ErrorLevel
  41. default:
  42. return zapcore.InfoLevel
  43. }
  44. }
  45. // RecordError logs an error. For HTTP-aware errors (apierr.Error) the mapped
  46. // business code is reported as well; anything else is logged as a system
  47. // error with its full cause.
  48. func RecordError(ctx context.Context, err error) {
  49. var apiErr *apierr.Error
  50. if stdErrors.As(err, &apiErr) {
  51. logx.WithContext(ctx).Errorf("【BUSINESS_ERROR】Status:%d, Code:%d, Msg:%s",
  52. apiErr.Status, response.Code(err), apiErr.Msg)
  53. } else {
  54. logx.WithContext(ctx).Errorf("【SYSTEM_ERROR】Cause: %+v", err)
  55. }
  56. }
  57. // RecordInfo logs ordinary business information.
  58. func RecordInfo(ctx context.Context, format string, args ...interface{}) {
  59. logx.WithContext(ctx).Infof("【BUSINESS_INFO】"+format, args...)
  60. }
  61. // RecordWarn logs issues worth attention that do not necessarily alert.
  62. // go-zero logx has no Warn level, so warnings go through the global zap
  63. // logger (same as sdf-serve); the ctx parameter stays for call-site
  64. // compatibility.
  65. func RecordWarn(ctx context.Context, format string, args ...interface{}) {
  66. zap.L().Warn(fmt.Sprintf("【BUSINESS_WARN】"+format, args...))
  67. }
  68. // RecordDebug logs development-time debug information.
  69. func RecordDebug(ctx context.Context, format string, args ...interface{}) {
  70. logx.WithContext(ctx).Debugf("【BUSINESS_DEBUG】"+format, args...)
  71. }