-
Notifications
You must be signed in to change notification settings - Fork 8
/
udplog.go
78 lines (64 loc) · 1.66 KB
/
udplog.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package log
import (
"encoding/json"
"fmt"
"net"
"time"
)
const (
DefaultHost = "127.0.0.1"
DefaultPort = 55647
DefaultCategory = "go_logging"
)
type udpLogRecord struct {
AppName string `json:"appname"`
HostName string `json:"hostname"`
LogLevel string `json:"logLevel"`
FileName string `json:"filename"`
FuncName string `json:"funcName"`
LineNo int `json:"lineno"`
Message string `json:"message"`
Timestamp float64 `json:"timestamp"`
}
// udpLogger is a type of writerLogger that sends messages in a special format to a udplog server.
type udpLogger struct {
*writerLogger // provides Writer() through embedding
}
func NewUDPLogger(conf Config) (Logger, error) {
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%v", DefaultHost, DefaultPort))
if err != nil {
return nil, err
}
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
return nil, err
}
sev, err := SeverityFromString(conf.Severity)
if err != nil {
return nil, err
}
return &udpLogger{&writerLogger{sev, conn}}, nil
}
func (l *udpLogger) SetSeverity(sev Severity) {
l.sev = sev
}
func (l *udpLogger) GetSeverity() Severity {
return l.sev
}
func (l *udpLogger) FormatMessage(sev Severity, caller *CallerInfo, format string, args ...interface{}) string {
rec := &udpLogRecord{
AppName: appname,
HostName: hostname,
LogLevel: sev.String(),
FileName: caller.FilePath,
FuncName: caller.FuncName,
LineNo: caller.LineNo,
Message: fmt.Sprintf(format, args...),
Timestamp: float64(time.Now().UnixNano()) / 1000000000,
}
dump, err := json.Marshal(rec)
if err != nil {
return ""
}
return fmt.Sprintf("%s:%s", DefaultCategory, dump)
}