-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpc.go
124 lines (91 loc) · 2.64 KB
/
rpc.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
package app
import (
"fmt"
"io"
"os"
v1 "github.com/roadrunner-server/api/v4/build/applogger/v1"
"go.uber.org/zap"
)
/*
https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
public function emergency($message, array $context = array());
public function alert($message, array $context = array());
public function critical($message, array $context = array());
public function error($message, array $context = array());
public function warning($message, array $context = array());
public function notice($message, array $context = array());
public function info($message, array $context = array());
public function debug($message, array $context = array());
public function log($level, $message, array $context = array());
*/
type RPC struct {
log *zap.Logger
}
func (r *RPC) Error(in string, _ *bool) error {
r.log.Error(in)
return nil
}
func (r *RPC) ErrorWithContext(in *v1.LogEntry, _ *v1.Response) error {
r.log.Error(in.GetMessage(), format(in.GetLogAttrs())...)
return nil
}
func (r *RPC) Info(in string, _ *bool) error {
r.log.Info(in)
return nil
}
func (r *RPC) InfoWithContext(in *v1.LogEntry, _ *v1.Response) error {
r.log.Info(in.GetMessage(), format(in.GetLogAttrs())...)
return nil
}
func (r *RPC) Warning(in string, _ *bool) error {
r.log.Warn(in)
return nil
}
func (r *RPC) WarningWithContext(in *v1.LogEntry, _ *v1.Response) error {
r.log.Warn(in.GetMessage(), format(in.GetLogAttrs())...)
return nil
}
func (r *RPC) Debug(in string, _ *bool) error {
r.log.Debug(in)
return nil
}
func (r *RPC) DebugWithContext(in *v1.LogEntry, _ *v1.Response) error {
r.log.Debug(in.GetMessage(), format(in.GetLogAttrs())...)
return nil
}
func (r *RPC) Log(in string, _ *bool) error {
_, err := io.WriteString(os.Stderr, in)
if err != nil {
return err
}
return nil
}
func (r *RPC) LogWithContext(in *v1.LogEntry, _ *v1.Response) error {
// special case when we don't have any attributes
if len(in.GetLogAttrs()) == 0 {
_, err := io.WriteString(os.Stderr, in.GetMessage())
if err != nil {
return err
}
return nil
}
_, err := io.WriteString(os.Stderr, formatRaw(in.GetMessage(), in.GetLogAttrs()))
if err != nil {
return err
}
return nil
}
func formatRaw(msg string, args []*v1.LogAttrs) string {
res := ""
for i := 0; i < len(args); i++ {
res += fmt.Sprintf("%s:%s,", args[i].GetKey(), args[i].GetValue())
}
return fmt.Sprintf("%s %s", msg, res[:len(res)-2]) // remove last comma
}
func format(args []*v1.LogAttrs) []zap.Field {
fields := make([]zap.Field, 0, len(args))
for _, v := range args {
fields = append(fields, zap.String(v.GetKey(), v.GetValue()))
}
return fields
}