-
Notifications
You must be signed in to change notification settings - Fork 19
/
net_log.go
executable file
·125 lines (110 loc) · 3.08 KB
/
net_log.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package loglib
import (
"time"
"net"
"log"
"logd/tcp_pack"
"encoding/json"
"runtime"
"strings"
"os"
"os/exec"
"path/filepath"
"sync"
)
type NetLog struct {
conn *net.TCPConn
level int
addr string //tcp address
ip string //self ip
mutex *sync.Mutex
}
func NewNetLog(addr string, level int) *NetLog {
conn, _ := getConnection(addr)
mutex := &sync.Mutex{}
return &NetLog{conn, level, addr, getIp(), mutex}
}
func getIp() string {
out, _ := exec.Command("/bin/sh", "-c", `/sbin/ifconfig | awk -F"[: ]+" '/inet addr/{print $4}' | head -n 1`).Output()
return strings.Trim(string(out), "\n")
}
//获取可执行文件的所在路径
func getBinPath() string {
d, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Println("[get bin path] error:", err)
}
return d
}
func getConnection(addr string) (*net.TCPConn, error) {
tcpAddr, err := net.ResolveTCPAddr("tcp4", addr)
if err != nil {
_, f, l, _ := runtime.Caller(1)
f = strings.Replace(f, getBinPath(), "", -1)
log.Println(f, ":", l, "[GetConnection] resolve tcp address failed", err)
return nil, err
}
conn, err := net.DialTCP("tcp4", nil, tcpAddr)
if err != nil {
_, f, l, _ := runtime.Caller(1)
f = strings.Replace(f, getBinPath(), "", -1)
log.Println(f, ":", l, "[GetConnection] connect to address:" , addr, "failed!")
}
return conn, err
}
func (l *NetLog) logging(level int, msg string) {
if level >= 0 && level < len(prefixes) {
l.mutex.Lock()
if l.conn == nil {
l.conn, _ = getConnection(l.addr) //重连一次
}
if l.conn != nil {
m := map[string]string{"time": time.Now().Format("2006/01/02 15:04:05"), "type": prefixes[level], "msg":msg, "ip": l.ip, "port": HeartBeatPort}
data, err := json.Marshal(m)
if err != nil {
log.Println("marshal net log error:", err)
return
}
data = tcp_pack.Pack(data)
_, err = l.conn.Write(data)
if err != nil {
log.Println("send log failed: ", msg, "error:", err)
}else{
log.Println("send log :" + msg)
}
}else{
log.Println("send log failed: ", msg, "error: no connection")
}
l.mutex.Unlock()
}
}
//设置日志级别,低于该级别的日志将不输出
func (l *NetLog) SetLevel(level int) bool {
if level >= 0 && level < len(prefixes) {
l.level = level
return true
}else {
log.Println("invalid log level")
}
return false
}
func (l *NetLog) Debug(msg string) {
if l.level == DEBUG {
l.logging(DEBUG, msg)
}
}
func (l *NetLog) Info(msg string) {
if l.level <= INFO {
l.logging(INFO, msg)
}
}
func (l *NetLog) Warning(msg string) {
if l.level <= WARNING {
l.logging(WARNING, msg)
}
}
func (l *NetLog) Error(msg string) {
if l.level <= ERROR {
l.logging(ERROR, msg)
}
}