encoder.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. _encoderMutex.RLock()
  59. defer _encoderMutex.RUnlock()
  60. if name == "" {
  61. return nil, errNoEncoderNameSpecified
  62. }
  63. constructor, ok := _encoderNameToConstructor[name]
  64. if !ok {
  65. return nil, fmt.Errorf("no encoder registered for name %q", name)
  66. }
  67. return constructor(encoderConfig)
  68. }