logs.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package logc
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/zeromicro/go-zero/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. // Error writes v into error log.
  24. func Error(ctx context.Context, v ...any) {
  25. getLogger(ctx).Error(v...)
  26. }
  27. // Errorf writes v with format into error log.
  28. func Errorf(ctx context.Context, format string, v ...any) {
  29. getLogger(ctx).Errorf(fmt.Errorf(format, v...).Error())
  30. }
  31. // Errorv writes v into error log with json content.
  32. // No call stack attached, because not elegant to pack the messages.
  33. func Errorv(ctx context.Context, v any) {
  34. getLogger(ctx).Errorv(v)
  35. }
  36. // Errorw writes msg along with fields into error log.
  37. func Errorw(ctx context.Context, msg string, fields ...LogField) {
  38. getLogger(ctx).Errorw(msg, fields...)
  39. }
  40. // Field returns a LogField for the given key and value.
  41. func Field(key string, value any) LogField {
  42. return logx.Field(key, value)
  43. }
  44. // Info writes v into access log.
  45. func Info(ctx context.Context, v ...any) {
  46. getLogger(ctx).Info(v...)
  47. }
  48. // Infof writes v with format into access log.
  49. func Infof(ctx context.Context, format string, v ...any) {
  50. getLogger(ctx).Infof(format, v...)
  51. }
  52. // Infov writes v into access log with json content.
  53. func Infov(ctx context.Context, v any) {
  54. getLogger(ctx).Infov(v)
  55. }
  56. // Infow writes msg along with fields into access log.
  57. func Infow(ctx context.Context, msg string, fields ...LogField) {
  58. getLogger(ctx).Infow(msg, fields...)
  59. }
  60. // Must checks if err is nil, otherwise logs the error and exits.
  61. func Must(err error) {
  62. logx.Must(err)
  63. }
  64. // MustSetup sets up logging with given config c. It exits on error.
  65. func MustSetup(c logx.LogConf) {
  66. logx.MustSetup(c)
  67. }
  68. // SetLevel sets the logging level. It can be used to suppress some logs.
  69. func SetLevel(level uint32) {
  70. logx.SetLevel(level)
  71. }
  72. // SetUp sets up the logx. If already set up, just return nil.
  73. // we allow SetUp to be called multiple times, because for example
  74. // we need to allow different service frameworks to initialize logx respectively.
  75. // the same logic for SetUp
  76. func SetUp(c LogConf) error {
  77. return logx.SetUp(c)
  78. }
  79. // Slow writes v into slow log.
  80. func Slow(ctx context.Context, v ...any) {
  81. getLogger(ctx).Slow(v...)
  82. }
  83. // Slowf writes v with format into slow log.
  84. func Slowf(ctx context.Context, format string, v ...any) {
  85. getLogger(ctx).Slowf(format, v...)
  86. }
  87. // Slowv writes v into slow log with json content.
  88. func Slowv(ctx context.Context, v any) {
  89. getLogger(ctx).Slowv(v)
  90. }
  91. // Sloww writes msg along with fields into slow log.
  92. func Sloww(ctx context.Context, msg string, fields ...LogField) {
  93. getLogger(ctx).Sloww(msg, fields...)
  94. }
  95. // getLogger returns the logx.Logger with the given ctx and correct caller.
  96. func getLogger(ctx context.Context) logx.Logger {
  97. return logx.WithContext(ctx).WithCallerSkip(1)
  98. }