This repository has been archived by the owner on Jan 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_client.go
129 lines (109 loc) · 3.56 KB
/
log_client.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
126
127
128
129
package client
import (
"log"
"fmt"
utils "github.com/aloknerurkar/backend_utils"
"os"
"github.com/aloknerurkar/log-post/proto_spec"
"io/ioutil"
)
// Full filename not required as of now.
const LOG_FLAGS = log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile
type LogPostClient struct {
PkgName string
PkgVersion string
remoteSender bool
sndr *sender
trace *log.Logger
info *log.Logger
error *log.Logger
fatal *log.Logger
}
func InitLogger(pkgName, pkgVersion string, logLevel int32, remote_sender bool) *LogPostClient {
client := new(LogPostClient)
client.PkgName = pkgName
client.PkgVersion = pkgVersion
client.remoteSender = remote_sender
endpoints := make([]interface{}, 1)
endpoints[0] = utils.ConnEndpointInfo{
Tls: false,
CertFile: "",
ServerHostOverride: "",
ServerAddr: "localhost:10000",
}
if client.remoteSender {
var err error
client.sndr, err = startSender(endpoints, pkgName, pkgVersion)
if err != nil {
log.Fatalf("Failed starting sender Err:%v", err)
}
}
if logLevel & int32(LogPost.LogLevel_TRACE) > 0 {
if client.remoteSender {
client.trace = log.New(client.sndr.trace, client.PkgId(), LOG_FLAGS)
} else {
client.trace = log.New(os.Stdout, "TRACE\t" + client.PkgId(), LOG_FLAGS)
}
} else {
client.trace = log.New(ioutil.Discard, "", LOG_FLAGS)
}
if logLevel & int32(LogPost.LogLevel_INFO) > 0 {
if client.remoteSender {
client.info = log.New(client.sndr.info, client.PkgId(), LOG_FLAGS)
} else {
client.info = log.New(os.Stdout, "INFO\t" + client.PkgId(), LOG_FLAGS)
}
} else {
client.info = log.New(ioutil.Discard, "", LOG_FLAGS)
}
if logLevel & int32(LogPost.LogLevel_ERROR) > 0 {
if client.remoteSender {
client.error = log.New(client.sndr.error, client.PkgId(), LOG_FLAGS)
} else {
client.error = log.New(os.Stdout, "ERROR\t" + client.PkgId(), LOG_FLAGS)
}
} else {
client.error = log.New(ioutil.Discard, "", LOG_FLAGS)
}
if logLevel & int32(LogPost.LogLevel_FATAL) > 0 {
if client.remoteSender {
client.fatal = log.New(client.sndr.fatal, client.PkgId(), LOG_FLAGS)
} else {
client.fatal = log.New(os.Stdout, "FATAL\t" + client.PkgId(), LOG_FLAGS)
}
} else {
client.fatal = log.New(ioutil.Discard, "", LOG_FLAGS)
}
return client
}
func (l *LogPostClient) PkgId() string {
return l.PkgName + ":" + l.PkgVersion + "\t"
}
func (l *LogPostClient) TraceStart(format string, args... interface{}) {
msg := fmt.Sprintf(format, args...)
msg_wfuncname := fmt.Sprintf("Started FUNC:%s %s\n", utils.MyCaller(), msg)
_ = l.trace.Output(3, msg_wfuncname)
}
func (l *LogPostClient) TraceEnd(format string, args... interface{}) {
msg := fmt.Sprintf(format, args...)
msg_wfuncname := fmt.Sprintf("End FUNC:%s %s\n", utils.MyCaller(), msg)
_ = l.trace.Output(3, msg_wfuncname)
}
func (l *LogPostClient) Info(format string, args... interface{}) {
msg := fmt.Sprintf(format, args...)
msg_wfuncname := fmt.Sprintf("FUNC:%s %s\n", utils.MyCaller(), msg)
_ = l.info.Output(3, msg_wfuncname)
}
func (l *LogPostClient) Error(e error, format string, args... interface{}) error {
msg := fmt.Sprintf(format, args...)
msg_wfuncname := fmt.Sprintf("ERR:%s FUNC:%s %s\n", e.Error(), utils.MyCaller(), msg)
_ = l.error.Output(3, msg_wfuncname)
return e
}
func (l *LogPostClient) Fatal(e error, format string, args... interface{}) {
msg := fmt.Sprintf(format, args...)
msg_wfuncname := fmt.Sprintf("ERR_FATAL:%s FUNC:%s %s\n", e.Error(), utils.MyCaller(), msg)
_ = l.fatal.Output(3, msg_wfuncname)
// Check handling of fatal stuff. Panic/Recover? Currently process exit may cause loss of msg.
os.Exit(1)
}