|
| 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 | + |
| 21 | +package zapencoder |
| 22 | + |
| 23 | +import ( |
| 24 | + "errors" |
| 25 | + "sort" |
| 26 | + "time" |
| 27 | + |
| 28 | + "go.uber.org/zap" |
| 29 | + "go.uber.org/zap/zapcore" |
| 30 | +) |
| 31 | + |
| 32 | +type SamplingConfig = zap.SamplingConfig |
| 33 | + |
| 34 | +type Config zap.Config |
| 35 | + |
| 36 | +// Build constructs a logger from the Config and Options. |
| 37 | +func (cfg Config) Build(opts ...zap.Option) (*zap.Logger, error) { |
| 38 | + enc, err := cfg.buildEncoder() |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + |
| 43 | + sink, errSink, err := cfg.openSinks() |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + if cfg.Level == (zap.AtomicLevel{}) { |
| 49 | + return nil, errors.New("missing Level") |
| 50 | + } |
| 51 | + |
| 52 | + log := zap.New( |
| 53 | + NewCustomCore(enc, sink, cfg.Level), |
| 54 | + cfg.buildOptions(errSink)..., |
| 55 | + ) |
| 56 | + if len(opts) > 0 { |
| 57 | + log = log.WithOptions(opts...) |
| 58 | + } |
| 59 | + return log, nil |
| 60 | +} |
| 61 | + |
| 62 | +func (cfg Config) buildOptions(errSink zapcore.WriteSyncer) []zap.Option { |
| 63 | + opts := []zap.Option{zap.ErrorOutput(errSink)} |
| 64 | + |
| 65 | + if cfg.Development { |
| 66 | + opts = append(opts, zap.Development()) |
| 67 | + } |
| 68 | + |
| 69 | + if !cfg.DisableCaller { |
| 70 | + opts = append(opts, zap.AddCaller()) |
| 71 | + } |
| 72 | + |
| 73 | + stackLevel := zap.ErrorLevel |
| 74 | + if cfg.Development { |
| 75 | + stackLevel = zap.WarnLevel |
| 76 | + } |
| 77 | + if !cfg.DisableStacktrace { |
| 78 | + opts = append(opts, zap.AddStacktrace(stackLevel)) |
| 79 | + } |
| 80 | + |
| 81 | + if scfg := cfg.Sampling; scfg != nil { |
| 82 | + opts = append(opts, zap.WrapCore(func(core zapcore.Core) zapcore.Core { |
| 83 | + var samplerOpts []zapcore.SamplerOption |
| 84 | + if scfg.Hook != nil { |
| 85 | + samplerOpts = append(samplerOpts, zapcore.SamplerHook(scfg.Hook)) |
| 86 | + } |
| 87 | + return zapcore.NewSamplerWithOptions( |
| 88 | + core, |
| 89 | + time.Second, |
| 90 | + cfg.Sampling.Initial, |
| 91 | + cfg.Sampling.Thereafter, |
| 92 | + samplerOpts..., |
| 93 | + ) |
| 94 | + })) |
| 95 | + } |
| 96 | + |
| 97 | + if len(cfg.InitialFields) > 0 { |
| 98 | + fs := make([]zap.Field, 0, len(cfg.InitialFields)) |
| 99 | + keys := make([]string, 0, len(cfg.InitialFields)) |
| 100 | + for k := range cfg.InitialFields { |
| 101 | + keys = append(keys, k) |
| 102 | + } |
| 103 | + sort.Strings(keys) |
| 104 | + for _, k := range keys { |
| 105 | + fs = append(fs, zap.Any(k, cfg.InitialFields[k])) |
| 106 | + } |
| 107 | + opts = append(opts, zap.Fields(fs...)) |
| 108 | + } |
| 109 | + |
| 110 | + return opts |
| 111 | +} |
| 112 | + |
| 113 | +func (cfg Config) openSinks() (zapcore.WriteSyncer, zapcore.WriteSyncer, error) { |
| 114 | + sink, closeOut, err := zap.Open(cfg.OutputPaths...) |
| 115 | + if err != nil { |
| 116 | + return nil, nil, err |
| 117 | + } |
| 118 | + errSink, _, err := zap.Open(cfg.ErrorOutputPaths...) |
| 119 | + if err != nil { |
| 120 | + closeOut() |
| 121 | + return nil, nil, err |
| 122 | + } |
| 123 | + return sink, errSink, nil |
| 124 | +} |
| 125 | + |
| 126 | +func (cfg Config) buildEncoder() (zapcore.Encoder, error) { |
| 127 | + return New(cfg.EncoderConfig), nil |
| 128 | +} |
0 commit comments