logger.go 690 B

12345678910111213141516171819202122232425262728293031
  1. package nebula
  2. import (
  3. "github.com/sirupsen/logrus"
  4. )
  5. type ContextualError struct {
  6. RealError error
  7. Fields map[string]interface{}
  8. Context string
  9. }
  10. func NewContextualError(msg string, fields map[string]interface{}, realError error) ContextualError {
  11. return ContextualError{Context: msg, Fields: fields, RealError: realError}
  12. }
  13. func (ce ContextualError) Error() string {
  14. return ce.RealError.Error()
  15. }
  16. func (ce ContextualError) Unwrap() error {
  17. return ce.RealError
  18. }
  19. func (ce *ContextualError) Log(lr *logrus.Logger) {
  20. if ce.RealError != nil {
  21. lr.WithFields(ce.Fields).WithError(ce.RealError).Error(ce.Context)
  22. } else {
  23. lr.WithFields(ce.Fields).Error(ce.Context)
  24. }
  25. }