logs.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package logc
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/wuntsong-org/go-zero-plus/core/logx"
  6. )
  7. type (
  8. LogConf = logx.LogConf
  9. LogField = logx.LogField
  10. )
  11. // AddGlobalFields adds global fields.
  12. func AddGlobalFields(fields ...LogField) {
  13. logx.AddGlobalFields(fields...)
  14. }
  15. // Alert alerts v in alert level, and the message is written to error log.
  16. func Alert(_ context.Context, v string) {
  17. logx.Alert(v)
  18. }
  19. // Close closes the logging.
  20. func Close() error {
  21. return logx.Close()
  22. }
  23. // Debug writes v into access log.
  24. func Debug(ctx context.Context, v ...interface{}) {
  25. getLogger(ctx).Debug(v...)
  26. }
  27. // Debugf writes v with format into access log.
  28. func Debugf(ctx context.Context, format string, v ...interface{}) {
  29. getLogger(ctx).Debugf(format, v...)
  30. }
  31. // Debugv writes v into access log with json content.
  32. func Debugv(ctx context.Context, v interface{}) {
  33. getLogger(ctx).Debugv(v)
  34. }
  35. // Debugw writes msg along with fields into access log.
  36. func Debugw(ctx context.Context, msg string, fields ...LogField) {
  37. getLogger(ctx).Debugw(msg, fields...)
  38. }
  39. // Error writes v into error log.
  40. func Error(ctx context.Context, v ...any) {
  41. getLogger(ctx).Error(v...)
  42. }
  43. // Errorf writes v with format into error log.
  44. func Errorf(ctx context.Context, format string, v ...any) {
  45. getLogger(ctx).Errorf(fmt.Errorf(format, v...).Error())
  46. }
  47. // Errorv writes v into error log with json content.
  48. // No call stack attached, because not elegant to pack the messages.
  49. func Errorv(ctx context.Context, v any) {
  50. getLogger(ctx).Errorv(v)
  51. }
  52. // Errorw writes msg along with fields into error log.
  53. func Errorw(ctx context.Context, msg string, fields ...LogField) {
  54. getLogger(ctx).Errorw(msg, fields...)
  55. }
  56. // Field returns a LogField for the given key and value.
  57. func Field(key string, value any) LogField {
  58. return logx.Field(key, value)
  59. }
  60. // Info writes v into access log.
  61. func Info(ctx context.Context, v ...any) {
  62. getLogger(ctx).Info(v...)
  63. }
  64. // Infof writes v with format into access log.
  65. func Infof(ctx context.Context, format string, v ...any) {
  66. getLogger(ctx).Infof(format, v...)
  67. }
  68. // Infov writes v into access log with json content.
  69. func Infov(ctx context.Context, v any) {
  70. getLogger(ctx).Infov(v)
  71. }
  72. // Infow writes msg along with fields into access log.
  73. func Infow(ctx context.Context, msg string, fields ...LogField) {
  74. getLogger(ctx).Infow(msg, fields...)
  75. }
  76. // Must checks if err is nil, otherwise logs the error and exits.
  77. func Must(err error) {
  78. logx.Must(err)
  79. }
  80. // MustSetup sets up logging with given config c. It exits on error.
  81. func MustSetup(c logx.LogConf) {
  82. logx.MustSetup(c)
  83. }
  84. // SetLevel sets the logging level. It can be used to suppress some logs.
  85. func SetLevel(level uint32) {
  86. logx.SetLevel(level)
  87. }
  88. // SetUp sets up the logx. If already set up, just return nil.
  89. // we allow SetUp to be called multiple times, because for example
  90. // we need to allow different service frameworks to initialize logx respectively.
  91. // the same logic for SetUp
  92. func SetUp(c LogConf) error {
  93. return logx.SetUp(c)
  94. }
  95. // Slow writes v into slow log.
  96. func Slow(ctx context.Context, v ...any) {
  97. getLogger(ctx).Slow(v...)
  98. }
  99. // Slowf writes v with format into slow log.
  100. func Slowf(ctx context.Context, format string, v ...any) {
  101. getLogger(ctx).Slowf(format, v...)
  102. }
  103. // Slowv writes v into slow log with json content.
  104. func Slowv(ctx context.Context, v any) {
  105. getLogger(ctx).Slowv(v)
  106. }
  107. // Sloww writes msg along with fields into slow log.
  108. func Sloww(ctx context.Context, msg string, fields ...LogField) {
  109. getLogger(ctx).Sloww(msg, fields...)
  110. }
  111. // getLogger returns the logx.Logger with the given ctx and correct caller.
  112. func getLogger(ctx context.Context) logx.Logger {
  113. return logx.WithContext(ctx).WithCallerSkip(1)
  114. }