blob: 2e9d3c34152191abd6969465a146faaf74fd59e6 [file] [log] [blame]
Takahiro Suzukid7bf8202020-12-17 20:21:59 +09001// 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) {
65 _encoderMutex.RLock()
66 defer _encoderMutex.RUnlock()
67 if name == "" {
68 return nil, errNoEncoderNameSpecified
69 }
70 constructor, ok := _encoderNameToConstructor[name]
71 if !ok {
72 return nil, fmt.Errorf("no encoder registered for name %q", name)
73 }
74 return constructor(encoderConfig)
75}