/
logging.go
49 lines (37 loc) · 997 Bytes
/
logging.go
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
package events
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/canonical/lxd/shared/api"
)
// LoggingServer controls what server to use for messages coming from the logger.
var LoggingServer *Server
// Handler describes an event handler.
type Handler struct {
}
// NewEventHandler creates and returns a new event handler.
func NewEventHandler() logrus.Hook {
return &Handler{}
}
// Fire sends a new logging event.
func (h Handler) Fire(entry *logrus.Entry) error {
if LoggingServer == nil {
return nil
}
return LoggingServer.Send("", api.EventTypeLogging, api.EventLogging{
Message: entry.Message,
Level: entry.Level.String(),
Context: logContextMap(entry.Data),
})
}
// Levels returns the list of supported log levels.
func (h Handler) Levels() []logrus.Level {
return logrus.AllLevels
}
func logContextMap(ctx logrus.Fields) map[string]string {
ctxMap := map[string]string{}
for k, v := range ctx {
ctxMap[k] = fmt.Sprintf("%v", v)
}
return ctxMap
}