github.com/npaton/distribution@v2.3.1-rc.0+incompatible/registry/handlers/hooks.go (about) 1 package handlers 2 3 import ( 4 "bytes" 5 "errors" 6 "fmt" 7 "strings" 8 "text/template" 9 10 "github.com/Sirupsen/logrus" 11 ) 12 13 // logHook is for hooking Panic in web application 14 type logHook struct { 15 LevelsParam []string 16 Mail *mailer 17 } 18 19 // Fire forwards an error to LogHook 20 func (hook *logHook) Fire(entry *logrus.Entry) error { 21 addr := strings.Split(hook.Mail.Addr, ":") 22 if len(addr) != 2 { 23 return errors.New("Invalid Mail Address") 24 } 25 host := addr[0] 26 subject := fmt.Sprintf("[%s] %s: %s", entry.Level, host, entry.Message) 27 28 html := ` 29 {{.Message}} 30 31 {{range $key, $value := .Data}} 32 {{$key}}: {{$value}} 33 {{end}} 34 ` 35 b := bytes.NewBuffer(make([]byte, 0)) 36 t := template.Must(template.New("mail body").Parse(html)) 37 if err := t.Execute(b, entry); err != nil { 38 return err 39 } 40 body := fmt.Sprintf("%s", b) 41 42 return hook.Mail.sendMail(subject, body) 43 } 44 45 // Levels contains hook levels to be catched 46 func (hook *logHook) Levels() []logrus.Level { 47 levels := []logrus.Level{} 48 for _, v := range hook.LevelsParam { 49 lv, _ := logrus.ParseLevel(v) 50 levels = append(levels, lv) 51 } 52 return levels 53 }