kraken
1// Copyright (c) 2016-2019 Uber Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package log15
16import (17"go.uber.org/zap"18"go.uber.org/zap/zapcore"19)
20
21// Config defines Logger configuration.
22type Config struct {23Level zapcore.Level `yaml:"level"`24Disable bool `yaml:"disable"`25ServiceName string `yaml:"service_name"`26Path string `yaml:"path"`27Encoding string `yaml:"encoding"`28EncodeTime zapcore.TimeEncoder `yaml:"timeEncoder" json:"-"`29}
30
31func (c Config) applyDefaults() Config {32if c.Path == "" {33c.Path = "stderr"34}35if c.Encoding == "" {36c.Encoding = "console"37}38return c39}
40
41// New creates a logger that is not default.
42func New(c Config, fields map[string]interface{}) (*zap.Logger, error) {43c = c.applyDefaults()44if c.Disable {45return zap.NewNop(), nil46}47if fields == nil {48fields = map[string]interface{}{}49}50if c.ServiceName != "" {51fields["service_name"] = c.ServiceName52}53
54return zap.Config{55Level: zap.NewAtomicLevelAt(c.Level),56Sampling: &zap.SamplingConfig{57Initial: 100,58Thereafter: 100,59},60Encoding: c.Encoding,61EncoderConfig: zapcore.EncoderConfig{62MessageKey: "message",63NameKey: "logger_name",64LevelKey: "level",65TimeKey: "ts",66CallerKey: "caller",67EncodeLevel: zapcore.CapitalLevelEncoder,68EncodeTime: c.EncodeTime,69EncodeDuration: zapcore.SecondsDurationEncoder,70EncodeCaller: zapcore.ShortCallerEncoder,71},72DisableStacktrace: true,73OutputPaths: []string{c.Path},74InitialFields: fields,75}.Build()76}
77