zapify.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package zap
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/ydb-platform/ydb/library/go/core/log"
  6. "go.uber.org/zap"
  7. "go.uber.org/zap/zapcore"
  8. )
  9. // ZapifyLevel turns interface log level to zap log level
  10. func ZapifyLevel(level log.Level) zapcore.Level {
  11. switch level {
  12. case log.TraceLevel:
  13. return zapcore.DebugLevel
  14. case log.DebugLevel:
  15. return zapcore.DebugLevel
  16. case log.InfoLevel:
  17. return zapcore.InfoLevel
  18. case log.WarnLevel:
  19. return zapcore.WarnLevel
  20. case log.ErrorLevel:
  21. return zapcore.ErrorLevel
  22. case log.FatalLevel:
  23. return zapcore.FatalLevel
  24. default:
  25. // For when new log level is not added to this func (most likely never).
  26. panic(fmt.Sprintf("unknown log level: %d", level))
  27. }
  28. }
  29. // UnzapifyLevel turns zap log level to interface log level.
  30. func UnzapifyLevel(level zapcore.Level) log.Level {
  31. switch level {
  32. case zapcore.DebugLevel:
  33. return log.DebugLevel
  34. case zapcore.InfoLevel:
  35. return log.InfoLevel
  36. case zapcore.WarnLevel:
  37. return log.WarnLevel
  38. case zapcore.ErrorLevel:
  39. return log.ErrorLevel
  40. case zapcore.FatalLevel, zapcore.DPanicLevel, zapcore.PanicLevel:
  41. return log.FatalLevel
  42. default:
  43. // For when new log level is not added to this func (most likely never).
  44. panic(fmt.Sprintf("unknown log level: %d", level))
  45. }
  46. }
  47. // nolint: gocyclo
  48. func zapifyField(field log.Field) zap.Field {
  49. switch field.Type() {
  50. case log.FieldTypeNil:
  51. return zap.Reflect(field.Key(), nil)
  52. case log.FieldTypeString:
  53. return zap.String(field.Key(), field.String())
  54. case log.FieldTypeBinary:
  55. return zap.Binary(field.Key(), field.Binary())
  56. case log.FieldTypeBoolean:
  57. return zap.Bool(field.Key(), field.Bool())
  58. case log.FieldTypeSigned:
  59. return zap.Int64(field.Key(), field.Signed())
  60. case log.FieldTypeUnsigned:
  61. return zap.Uint64(field.Key(), field.Unsigned())
  62. case log.FieldTypeFloat:
  63. return zap.Float64(field.Key(), field.Float())
  64. case log.FieldTypeTime:
  65. return zap.Time(field.Key(), field.Time())
  66. case log.FieldTypeDuration:
  67. return zap.Duration(field.Key(), field.Duration())
  68. case log.FieldTypeError:
  69. return zap.NamedError(field.Key(), field.Error())
  70. case log.FieldTypeArray:
  71. return zap.Any(field.Key(), field.Interface())
  72. case log.FieldTypeAny:
  73. return zap.Any(field.Key(), field.Interface())
  74. case log.FieldTypeReflect:
  75. return zap.Reflect(field.Key(), field.Interface())
  76. case log.FieldTypeByteString:
  77. return zap.ByteString(field.Key(), field.Binary())
  78. case log.FieldTypeContext:
  79. return Context(field.Interface().(context.Context))
  80. default:
  81. // For when new field type is not added to this func
  82. panic(fmt.Sprintf("unknown field type: %d", field.Type()))
  83. }
  84. }
  85. func zapifyFields(fields ...log.Field) []zapcore.Field {
  86. zapFields := make([]zapcore.Field, 0, len(fields))
  87. for _, field := range fields {
  88. zapFields = append(zapFields, zapifyField(field))
  89. }
  90. return zapFields
  91. }