github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/log/log.go (about) 1 package log 2 3 import ( 4 "fmt" 5 "io" 6 "net/url" 7 "strings" 8 9 "github.com/sirupsen/logrus" 10 ) 11 12 // PiperLogFormatter is the custom formatter of piper 13 type PiperLogFormatter struct { 14 logrus.TextFormatter 15 logFormat string 16 } 17 18 const ( 19 logFormatPlain = "plain" 20 logFormatDefault = "default" 21 logFormatWithTimestamp = "timestamp" 22 ) 23 24 // Format the log message 25 func (formatter *PiperLogFormatter) Format(entry *logrus.Entry) (bytes []byte, err error) { 26 message := "" 27 28 stepName := entry.Data["stepName"] 29 if stepName == nil { 30 stepName = "(noStepName)" 31 } 32 33 errorMessageSnippet := "" 34 if entry.Data[logrus.ErrorKey] != nil { 35 errorMessageSnippet = fmt.Sprintf(" - %s", entry.Data[logrus.ErrorKey]) 36 } 37 38 level, _ := entry.Level.MarshalText() 39 levelString := string(level) 40 if levelString == "warning" { 41 levelString = "warn" 42 } 43 44 switch formatter.logFormat { 45 case logFormatDefault: 46 message = fmt.Sprintf("%-5s %-6s - %s%s\n", levelString, stepName, entry.Message, errorMessageSnippet) 47 case logFormatWithTimestamp: 48 message = fmt.Sprintf("%s %-5s %-6s %s%s\n", entry.Time.Format("15:04:05"), levelString, stepName, entry.Message, errorMessageSnippet) 49 case logFormatPlain: 50 message = fmt.Sprintf("%s%s\n", entry.Message, errorMessageSnippet) 51 default: 52 formattedMessage, err := formatter.TextFormatter.Format(entry) 53 if err != nil { 54 return nil, err 55 } 56 message = string(formattedMessage) 57 } 58 59 for _, secret := range secrets { 60 message = strings.Replace(message, secret, "****", -1) 61 } 62 63 return []byte(message), nil 64 } 65 66 // LibraryRepository that is passed into with -ldflags 67 var LibraryRepository string 68 var LibraryName string 69 var logger *logrus.Entry 70 var secrets []string 71 72 // Entry returns the logger entry or creates one if none is present. 73 func Entry() *logrus.Entry { 74 if logger == nil { 75 logger = logrus.WithField("library", LibraryRepository) 76 logger.Logger.SetFormatter(&PiperLogFormatter{}) 77 } 78 79 return logger 80 } 81 82 // Writer returns an io.Writer into which a tool's output can be redirected. 83 func Writer() io.Writer { 84 return &logrusWriter{logger: Entry()} 85 } 86 87 // SetVerbose sets the log level with respect to verbose flag. 88 func SetVerbose(verbose bool) { 89 if verbose { 90 //Logger().Debugf("logging set to level: %s", level) 91 logrus.SetLevel(logrus.DebugLevel) 92 } 93 } 94 95 // SetFormatter specifies the log format to use for piper's output 96 func SetFormatter(logFormat string) { 97 Entry().Logger.SetFormatter(&PiperLogFormatter{logFormat: logFormat}) 98 } 99 100 // SetStepName sets the stepName field. 101 func SetStepName(stepName string) { 102 logger = Entry().WithField("stepName", stepName) 103 } 104 105 // DeferExitHandler registers a logrus exit handler to allow cleanup activities. 106 func DeferExitHandler(handler func()) { 107 logrus.DeferExitHandler(handler) 108 } 109 110 // RegisterHook registers a logrus hook 111 func RegisterHook(hook logrus.Hook) { 112 logrus.AddHook(hook) 113 } 114 115 // RegisterSecret registers a value which should be masked in every log message 116 func RegisterSecret(secret string) { 117 if len(secret) > 0 { 118 secrets = append(secrets, secret) 119 encoded := url.QueryEscape(secret) 120 if secret != encoded { 121 secrets = append(secrets, encoded) 122 } 123 } 124 }