-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_socket.go
65 lines (55 loc) · 1.17 KB
/
item_socket.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
package log4g
import (
"encoding/json"
"fmt"
"net"
)
func newSocketLoggerItem(level Level, prefix string, flag int, lc *loggerConfig, calldepth int) LoggerItem {
socketLogger := new(SocketLoggerItem)
var err error
if lc.Network == "" {
lc.Network = "udp"
}
socketLogger.conn, err = net.Dial(lc.Network, lc.Address)
if err != nil {
fmt.Println(err)
return nil
}
socketLogger.lc = lc
socketLogger.GenericLoggerItem = newLoggerItem(level, prefix, flag, socketLogger, calldepth)
return socketLogger
}
type SocketLoggerItem struct {
*GenericLoggerItem
conn net.Conn
lc *loggerConfig
}
func (l *SocketLoggerItem) Write(p []byte) (n int, err error) {
if l.conn == nil {
return
}
if p[len(p)-1] == '\n' {
p = p[0 : len(p)-1]
}
if l.lc.Codec == "json" {
rec := make(map[string]interface{})
rec[l.lc.JsonKey] = string(p)
if l.lc.JsonExt != "" {
var kv map[string]interface{}
json.Unmarshal([]byte(l.lc.JsonExt), &kv)
for k, v := range kv {
rec[k] = v
}
}
p, _ = json.Marshal(rec)
}
if l.lc.Network == "tcp" {
p = append(p, '\n')
}
return l.conn.Write(p)
}
func (l *SocketLoggerItem) Close() {
if l.conn != nil {
l.conn.Close()
}
}