github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/log/fatalHook.go (about) 1 package log 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 "path/filepath" 8 9 "github.com/sirupsen/logrus" 10 ) 11 12 // FatalHook provides a logrus hook which persists details about a fatal error into the file system. 13 // This is helpful in order to transfer the error details to an orchestrating CI/CD system 14 // and by that make it possible to provide better error messages to the user. 15 type FatalHook struct { 16 Path string 17 CorrelationID string 18 } 19 20 // Levels returns the supported log level of the hook. 21 func (f *FatalHook) Levels() []logrus.Level { 22 return []logrus.Level{logrus.FatalLevel} 23 } 24 25 // Fire persists the error message of the fatal error as json file into the file system. 26 func (f *FatalHook) Fire(entry *logrus.Entry) error { 27 details := entry.Data 28 if details == nil { 29 details = logrus.Fields{} 30 } 31 32 details["message"] = entry.Message 33 details["error"] = fmt.Sprint(details["error"]) 34 details["category"] = GetErrorCategory().String() 35 details["result"] = "failure" 36 details["correlationId"] = f.CorrelationID 37 details["time"] = entry.Time 38 39 fileName := "errorDetails.json" 40 if details["stepName"] != nil { 41 fileName = fmt.Sprintf("%v_%v", fmt.Sprint(details["stepName"]), fileName) 42 // ToDo: If step is called x times, and it fails multiple times the error is overwritten 43 } 44 filePath := filepath.Join(f.Path, fileName) 45 errDetails, _ := json.Marshal(&details) 46 // Logging information needed for error reporting - do not modify. 47 Entry().Infof("fatal error: errorDetails%v", string(errDetails)) 48 // Sets the fatal error details in the logging framework to be consumed in the stepTelemetryData 49 SetFatalErrorDetail(errDetails) 50 _, err := os.ReadFile(filePath) 51 if err != nil { 52 // do not overwrite file in case it already exists 53 // this helps to report the first error which occurred - instead of the last one 54 os.WriteFile(filePath, errDetails, 0666) 55 } 56 57 return nil 58 }