* Add generic logger support The current KCL has tight coupling with logrus and it causes issue for customer to use different logging system such as zap log. The issue has been opened via: https://github.com/vmware/vmware-go-kcl/issues/27 This change is to created a logger interface be able to abstract above logrus and zap log. It makes easy to add support for other logging system in the fugure. The work is based on: https://www.mountedthoughts.com/golang-logger-interface/ Some updates are made in order to make logging system easily injectable and add more unit tests. Tested against real kinesis and dyamodb as well. Signed-off-by: Tao Jiang <taoj@vmware.com> * Add lumberjack configuration options to have fine grained control Update the file log configuratio by adding most of luberjack configuration to avoid hardcode default value. Let user to specify the value because log retention and rotation are very important for prod environment. Signed-off-by: Tao Jiang <taoj@vmware.com>
151 lines
4.6 KiB
Go
151 lines
4.6 KiB
Go
/*
|
|
* Copyright (c) 2019 VMware, Inc.
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
|
* associated documentation files (the "Software"), to deal in the Software without restriction, including
|
|
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is furnished to do
|
|
* so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
* portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
|
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
// Note: The implementation comes from https://www.mountedthoughts.com/golang-logger-interface/
|
|
// https://github.com/amitrai48/logger
|
|
|
|
package logger
|
|
|
|
import (
|
|
"os"
|
|
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap/zapcore"
|
|
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
|
)
|
|
|
|
type ZapLogger struct {
|
|
sugaredLogger *zap.SugaredLogger
|
|
}
|
|
|
|
// NewZapLogger adapts existing sugared zap logger to Logger interface.
|
|
// The call is responsible for configuring sugard zap logger appropriately.
|
|
//
|
|
// Note: Sugar wraps the Logger to provide a more ergonomic, but slightly slower,
|
|
// API. Sugaring a Logger is quite inexpensive, so it's reasonable for a
|
|
// single application to use both Loggers and SugaredLoggers, converting
|
|
// between them on the boundaries of performance-sensitive code.
|
|
//
|
|
// Base zap logger can be convert to SugaredLogger by calling to add a wrapper:
|
|
// sugaredLogger := log.Sugar()
|
|
//
|
|
func NewZapLogger(logger *zap.SugaredLogger) Logger {
|
|
return &ZapLogger{
|
|
sugaredLogger: logger,
|
|
}
|
|
}
|
|
|
|
// NewZapLoggerWithConfig creates and configs Logger instance backed by
|
|
// zap Sugared logger.
|
|
func NewZapLoggerWithConfig(config Configuration) Logger {
|
|
cores := []zapcore.Core{}
|
|
|
|
if config.EnableConsole {
|
|
level := getZapLevel(config.ConsoleLevel)
|
|
writer := zapcore.Lock(os.Stdout)
|
|
core := zapcore.NewCore(getEncoder(config.ConsoleJSONFormat), writer, level)
|
|
cores = append(cores, core)
|
|
}
|
|
|
|
if config.EnableFile {
|
|
level := getZapLevel(config.FileLevel)
|
|
writer := zapcore.AddSync(&lumberjack.Logger{
|
|
Filename: config.Filename,
|
|
MaxSize: config.MaxSizeMB,
|
|
Compress: true,
|
|
MaxAge: config.MaxAgeDays,
|
|
MaxBackups: config.MaxBackups,
|
|
LocalTime: config.LocalTime,
|
|
})
|
|
core := zapcore.NewCore(getEncoder(config.FileJSONFormat), writer, level)
|
|
cores = append(cores, core)
|
|
}
|
|
|
|
combinedCore := zapcore.NewTee(cores...)
|
|
|
|
// AddCallerSkip skips 2 number of callers, this is important else the file that gets
|
|
// logged will always be the wrapped file. In our case zap.go
|
|
logger := zap.New(combinedCore,
|
|
zap.AddCallerSkip(2),
|
|
zap.AddCaller(),
|
|
).Sugar()
|
|
|
|
return &ZapLogger{
|
|
sugaredLogger: logger,
|
|
}
|
|
}
|
|
|
|
func (l *ZapLogger) Debugf(format string, args ...interface{}) {
|
|
l.sugaredLogger.Debugf(format, args...)
|
|
}
|
|
|
|
func (l *ZapLogger) Infof(format string, args ...interface{}) {
|
|
l.sugaredLogger.Infof(format, args...)
|
|
}
|
|
|
|
func (l *ZapLogger) Warnf(format string, args ...interface{}) {
|
|
l.sugaredLogger.Warnf(format, args...)
|
|
}
|
|
|
|
func (l *ZapLogger) Errorf(format string, args ...interface{}) {
|
|
l.sugaredLogger.Errorf(format, args...)
|
|
}
|
|
|
|
func (l *ZapLogger) Fatalf(format string, args ...interface{}) {
|
|
l.sugaredLogger.Fatalf(format, args...)
|
|
}
|
|
|
|
func (l *ZapLogger) Panicf(format string, args ...interface{}) {
|
|
l.sugaredLogger.Fatalf(format, args...)
|
|
}
|
|
|
|
func (l *ZapLogger) WithFields(fields Fields) Logger {
|
|
var f = make([]interface{}, 0)
|
|
for k, v := range fields {
|
|
f = append(f, k)
|
|
f = append(f, v)
|
|
}
|
|
newLogger := l.sugaredLogger.With(f...)
|
|
return &ZapLogger{newLogger}
|
|
}
|
|
|
|
func getEncoder(isJSON bool) zapcore.Encoder {
|
|
encoderConfig := zap.NewProductionEncoderConfig()
|
|
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
|
if isJSON {
|
|
return zapcore.NewJSONEncoder(encoderConfig)
|
|
}
|
|
return zapcore.NewConsoleEncoder(encoderConfig)
|
|
}
|
|
|
|
func getZapLevel(level string) zapcore.Level {
|
|
switch level {
|
|
case Info:
|
|
return zapcore.InfoLevel
|
|
case Warn:
|
|
return zapcore.WarnLevel
|
|
case Debug:
|
|
return zapcore.DebugLevel
|
|
case Error:
|
|
return zapcore.ErrorLevel
|
|
case Fatal:
|
|
return zapcore.FatalLevel
|
|
default:
|
|
return zapcore.InfoLevel
|
|
}
|
|
}
|