-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogrus_socketio.go
More file actions
41 lines (32 loc) · 911 Bytes
/
Copy pathlogrus_socketio.go
File metadata and controls
41 lines (32 loc) · 911 Bytes
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
package logrus_socketio
import (
"github.com/Sirupsen/logrus"
"github.com/zhouhui8915/go-socket.io-client"
)
type SocketIOHook struct {
Client *socketio_client.Client
EventName string
LogExtraFields map[string]interface{}
}
func NewSocketIOHook(uri string, event string, extraLogFields map[string]interface{}) (*SocketIOHook, error) {
opts := &socketio_client.Options{
Transport: "websocket",
Query: make(map[string]string),
}
client, err := socketio_client.NewClient(uri, opts)
if err != nil {
return &SocketIOHook{}, err
}
return &SocketIOHook{client, event, extraLogFields}, nil
}
func (hook *SocketIOHook) Fire(entry *logrus.Entry) error {
line, err := entry.WithFields(hook.LogExtraFields).String()
if err != nil {
return err
}
hook.Client.Emit(hook.EventName, line)
return nil
}
func (hook *SocketIOHook) Levels() []logrus.Level {
return logrus.AllLevels
}