blob: 08ed83354360124387f449d36d3d03dcc9a5280a [file] [log] [blame]
Don Newton7577f072020-01-06 12:41:11 -05001// 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
21package zap
22
23import (
24 "errors"
25 "fmt"
26 "sync"
27
28 "go.uber.org/zap/zapcore"
29)
30
31var (
32 errNoEncoderNameSpecified = errors.New("no encoder name specified")
33
34 _encoderNameToConstructor = map[string]func(zapcore.EncoderConfig) (zapcore.Encoder, error){
35 "console": func(encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) {
36 return zapcore.NewConsoleEncoder(encoderConfig), nil
37 },
38 "json": func(encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) {
39 return zapcore.NewJSONEncoder(encoderConfig), nil
40 },
41 }
42 _encoderMutex sync.RWMutex
43)
44
45// RegisterEncoder registers an encoder constructor, which the Config struct
46// can then reference. By default, the "json" and "console" encoders are
47// registered.
48//
49// Attempting to register an encoder whose name is already taken returns an
50// error.
51func RegisterEncoder(name string, constructor func(zapcore.EncoderConfig) (zapcore.Encoder, error)) error {
52 _encoderMutex.Lock()
53 defer _encoderMutex.Unlock()
54 if name == "" {
55 return errNoEncoderNameSpecified
56 }
57 if _, ok := _encoderNameToConstructor[name]; ok {
58 return fmt.Errorf("encoder already registered for name %q", name)
59 }
60 _encoderNameToConstructor[name] = constructor
61 return nil
62}
63
64func newEncoder(name string, encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) {
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +000065 if encoderConfig.TimeKey != "" && encoderConfig.EncodeTime == nil {
66 return nil, fmt.Errorf("missing EncodeTime in EncoderConfig")
67 }
68
Don Newton7577f072020-01-06 12:41:11 -050069 _encoderMutex.RLock()
70 defer _encoderMutex.RUnlock()
71 if name == "" {
72 return nil, errNoEncoderNameSpecified
73 }
74 constructor, ok := _encoderNameToConstructor[name]
75 if !ok {
76 return nil, fmt.Errorf("no encoder registered for name %q", name)
77 }
78 return constructor(encoderConfig)
79}