forked from alpacahq/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom_log.go
72 lines (58 loc) · 2 KB
/
custom_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
package quickfix
import (
"fmt"
)
const (
LogIncomingMessage string = "FIX incoming"
LogOutgoingMessage string = "FIX outgoing"
LogPrefixGlobal string = "GLOBAL"
)
type customLog struct {
sessionPrefix string
logFunc func(prefix, msg string, keysAndValues ...LogParam)
logErrorFunc func(prefix, msg string, err error, keysAndValues ...LogParam)
}
func (l customLog) OnIncoming(s []byte) {
l.logFunc(l.sessionPrefix, LogIncomingMessage, LogString("incomingMessage", makeReadable(s)))
}
func (l customLog) OnOutgoing(s []byte) {
l.logFunc(l.sessionPrefix, LogOutgoingMessage, LogString("outgoingMessage", makeReadable(s)))
}
func (l customLog) OnEvent(s string) {
l.logFunc(l.sessionPrefix, s)
}
func (l customLog) OnEventf(format string, a ...interface{}) {
l.OnEvent(fmt.Sprintf(format, a...))
}
func (l customLog) OnErrorEvent(message string, err error) {
l.logErrorFunc(l.sessionPrefix, message, err)
}
func (l customLog) OnEventParams(message string, v ...LogParam) {
l.logFunc(l.sessionPrefix, message, v...)
}
func (l customLog) OnErrorEventParams(message string, err error, v ...LogParam) {
l.logErrorFunc(l.sessionPrefix, message, err, v...)
}
type customLogFactory struct {
logFunc func(prefix, msg string, keysAndValues ...LogParam)
logErrorFunc func(prefix, msg string, err error, keysAndValues ...LogParam)
}
func (f customLogFactory) Create() (Log, error) {
log := customLog{LogPrefixGlobal, f.logFunc, f.logErrorFunc}
return log, nil
}
func (f customLogFactory) CreateSessionLog(sessionID SessionID) (Log, error) {
log := customLog{sessionID.String(), f.logFunc, f.logErrorFunc}
return log, nil
}
// NewCustomLogFactory creates an instance of LogFactory that
// logs messages and events using the provided log function.
func NewCustomLogFactory(
logFunc func(prefix, msg string, keysAndValues ...LogParam),
logErrorFunc func(prefix, msg string, err error, keysAndValues ...LogParam),
) LogFactory {
return customLogFactory{
logFunc: logFunc,
logErrorFunc: logErrorFunc,
}
}