File: logs_windows.go

package info (click to toggle)
nebula 1.6.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,376 kB
  • sloc: makefile: 149; sh: 100; python: 16
file content (54 lines) | stat: -rw-r--r-- 1,078 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main

import (
	"fmt"
	"io/ioutil"
	"os"

	"github.com/kardianos/service"
	"github.com/sirupsen/logrus"
)

// HookLogger routes the logrus logs through the service logger so that they end up in the Windows Event Viewer
// logrus output will be discarded
func HookLogger(l *logrus.Logger) {
	l.AddHook(newLogHook(logger))
	l.SetOutput(ioutil.Discard)
}

type logHook struct {
	sl service.Logger
}

func newLogHook(sl service.Logger) *logHook {
	return &logHook{sl: sl}
}

func (h *logHook) Fire(entry *logrus.Entry) error {
	line, err := entry.String()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to read entry, %v", err)
		return err
	}

	switch entry.Level {
	case logrus.PanicLevel:
		return h.sl.Error(line)
	case logrus.FatalLevel:
		return h.sl.Error(line)
	case logrus.ErrorLevel:
		return h.sl.Error(line)
	case logrus.WarnLevel:
		return h.sl.Warning(line)
	case logrus.InfoLevel:
		return h.sl.Info(line)
	case logrus.DebugLevel:
		return h.sl.Info(line)
	default:
		return nil
	}
}

func (h *logHook) Levels() []logrus.Level {
	return logrus.AllLevels
}