github.com/slackhq/nebula@v1.9.0/cmd/nebula-service/logs_windows.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  
     8  	"github.com/kardianos/service"
     9  	"github.com/sirupsen/logrus"
    10  )
    11  
    12  // HookLogger routes the logrus logs through the service logger so that they end up in the Windows Event Viewer
    13  // logrus output will be discarded
    14  func HookLogger(l *logrus.Logger) {
    15  	l.AddHook(newLogHook(logger))
    16  	l.SetOutput(ioutil.Discard)
    17  }
    18  
    19  type logHook struct {
    20  	sl service.Logger
    21  }
    22  
    23  func newLogHook(sl service.Logger) *logHook {
    24  	return &logHook{sl: sl}
    25  }
    26  
    27  func (h *logHook) Fire(entry *logrus.Entry) error {
    28  	line, err := entry.String()
    29  	if err != nil {
    30  		fmt.Fprintf(os.Stderr, "Unable to read entry, %v", err)
    31  		return err
    32  	}
    33  
    34  	switch entry.Level {
    35  	case logrus.PanicLevel:
    36  		return h.sl.Error(line)
    37  	case logrus.FatalLevel:
    38  		return h.sl.Error(line)
    39  	case logrus.ErrorLevel:
    40  		return h.sl.Error(line)
    41  	case logrus.WarnLevel:
    42  		return h.sl.Warning(line)
    43  	case logrus.InfoLevel:
    44  		return h.sl.Info(line)
    45  	case logrus.DebugLevel:
    46  		return h.sl.Info(line)
    47  	default:
    48  		return nil
    49  	}
    50  }
    51  
    52  func (h *logHook) Levels() []logrus.Level {
    53  	return logrus.AllLevels
    54  }