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
16// This package wraps logger functionality that is being used
17// in kraken providing seamless migration tooling if needed
18// and hides out some initialization details
19
20import (21"go.uber.org/zap"22"go.uber.org/zap/zapcore"23)
24
25var (26_default *zap.SugaredLogger27)
28
29// configure a default logger
30func init() {31zapConfig := zap.NewProductionConfig()32zapConfig.Encoding = "console"33zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder34zapConfig.DisableStacktrace = true35
36ConfigureLogger(zapConfig)37}
38
39// ConfigureLogger configures a global zap logger instance.
40func ConfigureLogger(zapConfig zap.Config) *zap.SugaredLogger {41logger, err := zapConfig.Build()42if err != nil {43panic(err)44}45
46// Skip this wrapper in a call stack.47logger = logger.WithOptions(zap.AddCallerSkip(1))48
49_default = logger.Sugar()50return _default51}
52
53// SetGlobalLogger sets the global logger.
54func SetGlobalLogger(l *zap.SugaredLogger) {55_default = l56}
57
58// Default returns the default global logger.
59func Default() *zap.SugaredLogger {60return _default61}
62
63// Debug uses fmt.Sprint to construct and log a message.
64func Debug(args ...interface{}) {65Default().Debug(args...)66}
67
68// Info uses fmt.Sprint to construct and log a message.
69func Info(args ...interface{}) {70Default().Info(args...)71}
72
73// Warn uses fmt.Sprint to construct and log a message.
74func Warn(args ...interface{}) {75Default().Warn(args...)76}
77
78// Error uses fmt.Sprint to construct and log a message.
79func Error(args ...interface{}) {80Default().Error(args...)81}
82
83// Panic uses fmt.Sprint to construct and log a message, then panics.
84func Panic(args ...interface{}) {85Default().Panic(args...)86}
87
88// Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.
89func Fatal(args ...interface{}) {90Default().Fatal(args...)91}
92
93// Debugf uses fmt.Sprintf to log a templated message.
94func Debugf(template string, args ...interface{}) {95Default().Debugf(template, args...)96}
97
98// Infof uses fmt.Sprintf to log a templated message.
99func Infof(template string, args ...interface{}) {100Default().Infof(template, args...)101}
102
103// Warnf uses fmt.Sprintf to log a templated message.
104func Warnf(template string, args ...interface{}) {105Default().Warnf(template, args...)106}
107
108// Errorf uses fmt.Sprintf to log a templated message.
109func Errorf(template string, args ...interface{}) {110Default().Errorf(template, args...)111}
112
113// Panicf uses fmt.Sprintf to log a templated message, then panics.
114func Panicf(template string, args ...interface{}) {115Default().Panicf(template, args...)116}
117
118// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
119func Fatalf(template string, args ...interface{}) {120Default().Fatalf(template, args...)121}
122
123// Debugw logs a message with some additional context. The variadic key-value
124// pairs are treated as they are in With.
125//
126// When debug-level logging is disabled, this is much faster than
127// s.With(keysAndValues).Debug(msg)
128func Debugw(msg string, keysAndValues ...interface{}) {129Default().Debugw(msg, keysAndValues...)130}
131
132// Infow logs a message with some additional context. The variadic key-value
133// pairs are treated as they are in With.
134func Infow(msg string, keysAndValues ...interface{}) {135Default().Infow(msg, keysAndValues...)136}
137
138// Warnw logs a message with some additional context. The variadic key-value
139// pairs are treated as they are in With.
140func Warnw(msg string, keysAndValues ...interface{}) {141Default().Warnw(msg, keysAndValues...)142}
143
144// Errorw logs a message with some additional context. The variadic key-value
145// pairs are treated as they are in With.
146func Errorw(msg string, keysAndValues ...interface{}) {147Default().Errorw(msg, keysAndValues...)148}
149
150// Panicw logs a message with some additional context, then panics. The
151// variadic key-value pairs are treated as they are in With.
152func Panicw(msg string, keysAndValues ...interface{}) {153Default().Panicw(msg, keysAndValues...)154}
155
156// Fatalw logs a message with some additional context, then calls os.Exit. The
157// variadic key-value pairs are treated as they are in With.
158func Fatalw(msg string, keysAndValues ...interface{}) {159Default().Fatalw(msg, keysAndValues...)160}
161
162// With adds a variadic number of fields to the logging context.
163// It accepts a mix of strongly-typed zapcore.Field objects and loosely-typed key-value pairs.
164func With(args ...interface{}) *zap.SugaredLogger {165return Default().With(args...)166}
167