utils/log/log.go (87 lines of code) (raw):
// Copyright (c) 2016-2019 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package log
// This package wraps logger functionality that is being used
// in kraken providing seamless migration tooling if needed
// and hides out some initialization details
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
_default *zap.SugaredLogger
)
// configure a default logger
func init() {
zapConfig := zap.NewProductionConfig()
zapConfig.Encoding = "console"
zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
zapConfig.DisableStacktrace = true
ConfigureLogger(zapConfig)
}
// ConfigureLogger configures a global zap logger instance.
func ConfigureLogger(zapConfig zap.Config) *zap.SugaredLogger {
logger, err := zapConfig.Build()
if err != nil {
panic(err)
}
// Skip this wrapper in a call stack.
logger = logger.WithOptions(zap.AddCallerSkip(1))
_default = logger.Sugar()
return _default
}
// SetGlobalLogger sets the global logger.
func SetGlobalLogger(l *zap.SugaredLogger) {
_default = l
}
// Default returns the default global logger.
func Default() *zap.SugaredLogger {
return _default
}
// Debug uses fmt.Sprint to construct and log a message.
func Debug(args ...interface{}) {
Default().Debug(args...)
}
// Info uses fmt.Sprint to construct and log a message.
func Info(args ...interface{}) {
Default().Info(args...)
}
// Warn uses fmt.Sprint to construct and log a message.
func Warn(args ...interface{}) {
Default().Warn(args...)
}
// Error uses fmt.Sprint to construct and log a message.
func Error(args ...interface{}) {
Default().Error(args...)
}
// Panic uses fmt.Sprint to construct and log a message, then panics.
func Panic(args ...interface{}) {
Default().Panic(args...)
}
// Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.
func Fatal(args ...interface{}) {
Default().Fatal(args...)
}
// Debugf uses fmt.Sprintf to log a templated message.
func Debugf(template string, args ...interface{}) {
Default().Debugf(template, args...)
}
// Infof uses fmt.Sprintf to log a templated message.
func Infof(template string, args ...interface{}) {
Default().Infof(template, args...)
}
// Warnf uses fmt.Sprintf to log a templated message.
func Warnf(template string, args ...interface{}) {
Default().Warnf(template, args...)
}
// Errorf uses fmt.Sprintf to log a templated message.
func Errorf(template string, args ...interface{}) {
Default().Errorf(template, args...)
}
// Panicf uses fmt.Sprintf to log a templated message, then panics.
func Panicf(template string, args ...interface{}) {
Default().Panicf(template, args...)
}
// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
func Fatalf(template string, args ...interface{}) {
Default().Fatalf(template, args...)
}
// Debugw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
//
// When debug-level logging is disabled, this is much faster than
// s.With(keysAndValues).Debug(msg)
func Debugw(msg string, keysAndValues ...interface{}) {
Default().Debugw(msg, keysAndValues...)
}
// Infow logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func Infow(msg string, keysAndValues ...interface{}) {
Default().Infow(msg, keysAndValues...)
}
// Warnw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func Warnw(msg string, keysAndValues ...interface{}) {
Default().Warnw(msg, keysAndValues...)
}
// Errorw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func Errorw(msg string, keysAndValues ...interface{}) {
Default().Errorw(msg, keysAndValues...)
}
// Panicw logs a message with some additional context, then panics. The
// variadic key-value pairs are treated as they are in With.
func Panicw(msg string, keysAndValues ...interface{}) {
Default().Panicw(msg, keysAndValues...)
}
// Fatalw logs a message with some additional context, then calls os.Exit. The
// variadic key-value pairs are treated as they are in With.
func Fatalw(msg string, keysAndValues ...interface{}) {
Default().Fatalw(msg, keysAndValues...)
}
// With adds a variadic number of fields to the logging context.
// It accepts a mix of strongly-typed zapcore.Field objects and loosely-typed key-value pairs.
func With(args ...interface{}) *zap.SugaredLogger {
return Default().With(args...)
}