* 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>
115 lines
3.8 KiB
Go
115 lines
3.8 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 (
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// Fields Type to pass when we want to call WithFields for structured logging
|
|
type Fields map[string]interface{}
|
|
|
|
const (
|
|
//Debug has verbose message
|
|
Debug = "debug"
|
|
//Info is default log level
|
|
Info = "info"
|
|
//Warn is for logging messages about possible issues
|
|
Warn = "warn"
|
|
//Error is for logging errors
|
|
Error = "error"
|
|
//Fatal is for logging fatal messages. The sytem shutsdown after logging the message.
|
|
Fatal = "fatal"
|
|
)
|
|
|
|
// Logger is the common interface for logging.
|
|
type Logger interface {
|
|
Debugf(format string, args ...interface{})
|
|
|
|
Infof(format string, args ...interface{})
|
|
|
|
Warnf(format string, args ...interface{})
|
|
|
|
Errorf(format string, args ...interface{})
|
|
|
|
Fatalf(format string, args ...interface{})
|
|
|
|
Panicf(format string, args ...interface{})
|
|
|
|
WithFields(keyValues Fields) Logger
|
|
}
|
|
|
|
// Configuration stores the config for the logger
|
|
// For some loggers there can only be one level across writers, for such the level of Console is picked by default
|
|
type Configuration struct {
|
|
EnableConsole bool
|
|
ConsoleJSONFormat bool
|
|
ConsoleLevel string
|
|
EnableFile bool
|
|
FileJSONFormat bool
|
|
FileLevel string
|
|
|
|
// Filename is the file to write logs to. Backup log files will be retained
|
|
// in the same directory. It uses <processname>-lumberjack.log in
|
|
// os.TempDir() if empty.
|
|
Filename string
|
|
|
|
// MaxSize is the maximum size in megabytes of the log file before it gets
|
|
// rotated. It defaults to 100 megabytes.
|
|
MaxSizeMB int
|
|
|
|
// MaxAge is the maximum number of days to retain old log files based on the
|
|
// timestamp encoded in their filename. Note that a day is defined as 24
|
|
// hours and may not exactly correspond to calendar days due to daylight
|
|
// savings, leap seconds, etc. The default is 7 days.
|
|
MaxAgeDays int
|
|
|
|
// MaxBackups is the maximum number of old log files to retain. The default
|
|
// is to retain all old log files (though MaxAge may still cause them to get
|
|
// deleted.)
|
|
MaxBackups int
|
|
|
|
// LocalTime determines if the time used for formatting the timestamps in
|
|
// backup files is the computer's local time. The default is to use UTC
|
|
// time.
|
|
LocalTime bool
|
|
}
|
|
|
|
// GetDefaultLogger creates a default logger.
|
|
func GetDefaultLogger() Logger {
|
|
return NewLogrusLogger(logrus.StandardLogger())
|
|
}
|
|
|
|
// normalizeConfig to enforce default value in configuration.
|
|
func normalizeConfig(config *Configuration) {
|
|
if config.MaxSizeMB <= 0 {
|
|
config.MaxSizeMB = 100
|
|
}
|
|
|
|
if config.MaxAgeDays <= 0 {
|
|
config.MaxAgeDays = 7
|
|
}
|
|
|
|
if config.MaxBackups < 0 {
|
|
config.MaxBackups = 0
|
|
}
|
|
}
|