showuserlog.go 778 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package log
  2. import (
  3. "fmt"
  4. "github.com/Sirupsen/logrus"
  5. "os"
  6. )
  7. // ShowuserlogHook writes all levels of logrus entries to a file for later analysis
  8. type ShowuserlogHook struct {
  9. Level logrus.Level
  10. }
  11. func NewShowuserlogHook(l logrus.Level) (*ShowuserlogHook, error) {
  12. return &ShowuserlogHook{l}, nil
  13. }
  14. func (hook *ShowuserlogHook) Fire(entry *logrus.Entry) error {
  15. line, err := entry.String()
  16. if err != nil {
  17. fmt.Fprintf(os.Stderr, "Unable to read entry, %v", err)
  18. return err
  19. }
  20. if entry.Level <= hook.Level {
  21. fmt.Printf("> %s", line)
  22. }
  23. return nil
  24. }
  25. func (hook *ShowuserlogHook) Levels() []logrus.Level {
  26. return []logrus.Level{
  27. logrus.DebugLevel,
  28. logrus.InfoLevel,
  29. logrus.WarnLevel,
  30. logrus.ErrorLevel,
  31. logrus.FatalLevel,
  32. logrus.PanicLevel,
  33. }
  34. }