encoder.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package zap
  21. import (
  22. "errors"
  23. "fmt"
  24. "sync"
  25. "go.uber.org/zap/zapcore"
  26. )
  27. var (
  28. errNoEncoderNameSpecified = errors.New("no encoder name specified")
  29. _encoderNameToConstructor = map[string]func(zapcore.EncoderConfig) (zapcore.Encoder, error){
  30. "console": func(encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) {
  31. return zapcore.NewConsoleEncoder(encoderConfig), nil
  32. },
  33. "json": func(encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) {
  34. return zapcore.NewJSONEncoder(encoderConfig), nil
  35. },
  36. }
  37. _encoderMutex sync.RWMutex
  38. )
  39. // RegisterEncoder registers an encoder constructor, which the Config struct
  40. // can then reference. By default, the "json" and "console" encoders are
  41. // registered.
  42. //
  43. // Attempting to register an encoder whose name is already taken returns an
  44. // error.
  45. func RegisterEncoder(name string, constructor func(zapcore.EncoderConfig) (zapcore.Encoder, error)) error {
  46. _encoderMutex.Lock()
  47. defer _encoderMutex.Unlock()
  48. if name == "" {
  49. return errNoEncoderNameSpecified
  50. }
  51. if _, ok := _encoderNameToConstructor[name]; ok {
  52. return fmt.Errorf("encoder already registered for name %q", name)
  53. }
  54. _encoderNameToConstructor[name] = constructor
  55. return nil
  56. }
  57. func newEncoder(name string, encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) {
  58. if encoderConfig.TimeKey != "" && encoderConfig.EncodeTime == nil {
  59. return nil, fmt.Errorf("missing EncodeTime in EncoderConfig")
  60. }
  61. _encoderMutex.RLock()
  62. defer _encoderMutex.RUnlock()
  63. if name == "" {
  64. return nil, errNoEncoderNameSpecified
  65. }
  66. constructor, ok := _encoderNameToConstructor[name]
  67. if !ok {
  68. return nil, fmt.Errorf("no encoder registered for name %q", name)
  69. }
  70. return constructor(encoderConfig)
  71. }