hooks.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. package logrus
  2. // A hook to be fired when logging on the logging levels returned from
  3. // `Levels()` on your implementation of the interface. Note that this is not
  4. // fired in a goroutine or a channel with workers, you should handle such
  5. // functionality yourself if your call is non-blocking and you don't wish for
  6. // the logging calls for levels returned from `Levels()` to block.
  7. type Hook interface {
  8. Levels() []Level
  9. Fire(*Entry) error
  10. }
  11. // Internal type for storing the hooks on a logger instance.
  12. type LevelHooks map[Level][]Hook
  13. // Add a hook to an instance of logger. This is called with
  14. // `log.Hooks.Add(new(MyHook))` where `MyHook` implements the `Hook` interface.
  15. func (hooks LevelHooks) Add(hook Hook) {
  16. for _, level := range hook.Levels() {
  17. hooks[level] = append(hooks[level], hook)
  18. }
  19. }
  20. // Fire all the hooks for the passed level. Used by `entry.log` to fire
  21. // appropriate hooks for a log entry.
  22. func (hooks LevelHooks) Fire(level Level, entry *Entry) error {
  23. for _, hook := range hooks[level] {
  24. if err := hook.Fire(entry); err != nil {
  25. return err
  26. }
  27. }
  28. return nil
  29. }