2019-11-04 15:43:33 +02:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2020-05-06 12:17:56 +02:00
|
|
|
"fmt"
|
2020-05-06 13:35:40 +02:00
|
|
|
"io"
|
2020-11-02 11:08:34 +02:00
|
|
|
"net/url"
|
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
|
|
|
)
|
|
|
|
|
2022-11-08 09:47:38 +02:00
|
|
|
// PiperLogFormatter is the custom formatter of piper
|
2020-05-06 12:17:56 +02:00
|
|
|
type PiperLogFormatter struct {
|
2020-04-16 14:37:45 +02:00
|
|
|
logrus.TextFormatter
|
2020-05-06 12:17:56 +02:00
|
|
|
logFormat string
|
2020-04-16 14:37:45 +02:00
|
|
|
}
|
|
|
|
|
2020-05-06 12:17:56 +02:00
|
|
|
const (
|
|
|
|
logFormatPlain = "plain"
|
|
|
|
logFormatDefault = "default"
|
|
|
|
logFormatWithTimestamp = "timestamp"
|
|
|
|
)
|
2020-04-16 14:37:45 +02:00
|
|
|
|
2022-11-08 09:47:38 +02:00
|
|
|
// Format the log message
|
2020-05-06 12:17:56 +02:00
|
|
|
func (formatter *PiperLogFormatter) Format(entry *logrus.Entry) (bytes []byte, err error) {
|
|
|
|
message := ""
|
|
|
|
|
2020-05-06 16:27:34 +02:00
|
|
|
stepName := entry.Data["stepName"]
|
|
|
|
if stepName == nil {
|
|
|
|
stepName = "(noStepName)"
|
|
|
|
}
|
|
|
|
|
2020-05-27 09:31:41 +02:00
|
|
|
errorMessageSnippet := ""
|
|
|
|
if entry.Data[logrus.ErrorKey] != nil {
|
|
|
|
errorMessageSnippet = fmt.Sprintf(" - %s", entry.Data[logrus.ErrorKey])
|
2020-05-26 11:06:52 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 14:06:09 +02:00
|
|
|
level, _ := entry.Level.MarshalText()
|
|
|
|
levelString := string(level)
|
|
|
|
if levelString == "warning" {
|
|
|
|
levelString = "warn"
|
|
|
|
}
|
|
|
|
|
2020-05-06 12:17:56 +02:00
|
|
|
switch formatter.logFormat {
|
|
|
|
case logFormatDefault:
|
2020-05-27 14:06:09 +02:00
|
|
|
message = fmt.Sprintf("%-5s %-6s - %s%s\n", levelString, stepName, entry.Message, errorMessageSnippet)
|
2020-05-06 12:17:56 +02:00
|
|
|
case logFormatWithTimestamp:
|
2020-05-27 14:06:09 +02:00
|
|
|
message = fmt.Sprintf("%s %-5s %-6s %s%s\n", entry.Time.Format("15:04:05"), levelString, stepName, entry.Message, errorMessageSnippet)
|
2020-05-06 12:17:56 +02:00
|
|
|
case logFormatPlain:
|
2020-05-27 09:31:41 +02:00
|
|
|
message = fmt.Sprintf("%s%s\n", entry.Message, errorMessageSnippet)
|
2020-05-06 12:17:56 +02:00
|
|
|
default:
|
|
|
|
formattedMessage, err := formatter.TextFormatter.Format(entry)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
message = string(formattedMessage)
|
2020-04-16 14:37:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-05-10 19:18:16 +02:00
|
|
|
var LibraryName 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)
|
2020-05-06 12:17:56 +02:00
|
|
|
logger.Logger.SetFormatter(&PiperLogFormatter{})
|
2019-11-04 15:43:33 +02:00
|
|
|
}
|
2020-04-16 14:37:45 +02:00
|
|
|
|
2019-11-04 15:43:33 +02:00
|
|
|
return logger
|
|
|
|
}
|
|
|
|
|
2020-05-06 13:35:40 +02:00
|
|
|
// Writer returns an io.Writer into which a tool's output can be redirected.
|
|
|
|
func Writer() io.Writer {
|
|
|
|
return &logrusWriter{logger: Entry()}
|
|
|
|
}
|
|
|
|
|
2019-11-04 15:43:33 +02:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 12:17:56 +02:00
|
|
|
// SetFormatter specifies the log format to use for piper's output
|
|
|
|
func SetFormatter(logFormat string) {
|
|
|
|
Entry().Logger.SetFormatter(&PiperLogFormatter{logFormat: logFormat})
|
|
|
|
}
|
|
|
|
|
2019-11-04 15:43:33 +02:00
|
|
|
// 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-11-02 11:08:34 +02:00
|
|
|
// RegisterSecret registers a value which should be masked in every log message
|
2020-04-16 14:37:45 +02:00
|
|
|
func RegisterSecret(secret string) {
|
|
|
|
if len(secret) > 0 {
|
|
|
|
secrets = append(secrets, secret)
|
2020-11-02 11:08:34 +02:00
|
|
|
encoded := url.QueryEscape(secret)
|
|
|
|
if secret != encoded {
|
|
|
|
secrets = append(secrets, encoded)
|
|
|
|
}
|
2020-04-16 14:37:45 +02:00
|
|
|
}
|
|
|
|
}
|