2019-11-04 15:43:33 +02:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2020-04-16 14:37:45 +02:00
|
|
|
"strings"
|
2020-04-28 07:42:02 +02:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2019-11-04 15:43:33 +02:00
|
|
|
)
|
|
|
|
|
2020-04-16 14:37:45 +02:00
|
|
|
type RemoveSecretFormatterDecorator struct {
|
|
|
|
logrus.TextFormatter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (formatter *RemoveSecretFormatterDecorator) Format(entry *logrus.Entry) (bytes []byte, err error) {
|
|
|
|
formattedMessage, err := formatter.TextFormatter.Format(entry)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
message := string(formattedMessage)
|
|
|
|
|
|
|
|
for _, secret := range secrets {
|
|
|
|
message = strings.Replace(message, secret, "****", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []byte(message), nil
|
|
|
|
}
|
|
|
|
|
2019-11-11 16:31:02 +02:00
|
|
|
// LibraryRepository that is passed into with -ldflags
|
|
|
|
var LibraryRepository string
|
2019-11-04 15:43:33 +02:00
|
|
|
var logger *logrus.Entry
|
2020-04-16 14:37:45 +02:00
|
|
|
var secrets []string
|
2019-11-04 15:43:33 +02:00
|
|
|
|
|
|
|
// Entry returns the logger entry or creates one if none is present.
|
|
|
|
func Entry() *logrus.Entry {
|
|
|
|
if logger == nil {
|
2019-11-11 16:31:02 +02:00
|
|
|
logger = logrus.WithField("library", LibraryRepository)
|
2019-11-04 15:43:33 +02:00
|
|
|
}
|
2020-04-16 14:37:45 +02:00
|
|
|
|
|
|
|
logger.Logger.SetFormatter(&RemoveSecretFormatterDecorator{})
|
|
|
|
|
2019-11-04 15:43:33 +02:00
|
|
|
return logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetVerbose sets the log level with respect to verbose flag.
|
|
|
|
func SetVerbose(verbose bool) {
|
|
|
|
if verbose {
|
|
|
|
//Logger().Debugf("logging set to level: %s", level)
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetStepName sets the stepName field.
|
|
|
|
func SetStepName(stepName string) {
|
|
|
|
logger = Entry().WithField("stepName", stepName)
|
|
|
|
}
|
2020-01-15 13:16:25 +02:00
|
|
|
|
|
|
|
// DeferExitHandler registers a logrus exit handler to allow cleanup activities.
|
|
|
|
func DeferExitHandler(handler func()) {
|
|
|
|
logrus.DeferExitHandler(handler)
|
|
|
|
}
|
2020-04-16 14:37:45 +02:00
|
|
|
|
2020-04-28 07:42:02 +02:00
|
|
|
// RegisterHook registers a logrus hook
|
|
|
|
func RegisterHook(hook logrus.Hook) {
|
|
|
|
logrus.AddHook(hook)
|
|
|
|
}
|
|
|
|
|
2020-04-16 14:37:45 +02:00
|
|
|
func RegisterSecret(secret string) {
|
|
|
|
if len(secret) > 0 {
|
|
|
|
secrets = append(secrets, secret)
|
|
|
|
}
|
|
|
|
}
|