-
Notifications
You must be signed in to change notification settings - Fork 23
/
directhook.go
276 lines (238 loc) · 6.46 KB
/
directhook.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* === This file is part of ALICE O² ===
*
* Copyright 2019 CERN and copyright holders of ALICE O².
* Author: Teo Mrnjavac <teo.mrnjavac@cern.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In applying this license CERN does not waive the privileges and
* immunities granted to it by virtue of its status as an
* Intergovernmental Organization or submit itself to any jurisdiction.
*/
package infologger
import (
"errors"
"fmt"
"net"
"os"
"os/user"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"github.com/AliceO2Group/Control/common/utils"
"github.com/sirupsen/logrus"
)
const INFOLOGGER_MAX_MESSAGE_SIZE = 1024
var (
hostname string
pid string
username string
)
var lineBreaksRe = regexp.MustCompile(`\r?\n`)
func init() {
var err error
pid = fmt.Sprintf("%d", os.Getpid())
unixUser, _ := user.Current()
if unixUser != nil {
username = unixUser.Username
}
hostname, err = os.Hostname()
if err != nil {
return
}
// We only take the short hostname
hostname = strings.Split(hostname, ".")[0]
}
type sender struct {
stream net.Conn
}
func newSender(path string) *sender {
stream, err := net.Dial("unix", path)
if err != nil {
fmt.Printf("cannot dial unix socket %s\n", path)
return nil
}
return &sender{
stream: stream,
}
}
func (s *sender) Close() error {
return s.stream.Close()
}
func (s *sender) format(fields map[string]string, version protoVersion) string {
stringLog := string("*" + version)
currentProtocol := protocols[version]
for _, fSpec := range *currentProtocol {
stringLog += "#"
if value, ok := fields[fSpec.name]; ok {
stringLog += value
}
}
// We sanitize away all linebreaks from the payload, and then we append one.
// This is necessary because IL treats \n as a message terminator character.
stringLog = lineBreaksRe.ReplaceAllString(stringLog, " ")
return stringLog + "\n"
}
func (s *sender) Send(fields map[string]string) error {
if s.stream != nil {
_, err := s.stream.Write([]byte(s.format(fields, v14)))
return err
}
return errors.New("could not send log message: InfoLogger socket not available")
}
type DirectHook struct {
il *sender
system string
facility string
role string
}
func paddedAbstractSocket(name string) string {
const targetLen = 108 // Linux constant
out := make([]byte, targetLen)
for i := 0; i < targetLen; i++ {
if i < len(name) {
out[i] = name[i]
} else {
out[i] = '\x00'
}
}
return string(out)
}
func guessSocketPath() string {
if runtime.GOOS == "linux" {
return paddedAbstractSocket("@infoLoggerD")
} else { // macOS
return "/tmp/infoLoggerD.socket"
}
}
func NewDirectHook(defaultSystem string, defaultFacility string) (*DirectHook, error) {
socketPath := guessSocketPath()
sender := newSender(socketPath)
if sender == nil {
return nil, fmt.Errorf("cannot instantiate InfoLogger hook on socket %s", socketPath)
}
return &DirectHook{
il: sender,
system: defaultSystem,
facility: defaultFacility,
role: hostname,
}, nil
}
func NewDirectHookWithRole(defaultSystem string, defaultFacility string, defaultRole string) (*DirectHook, error) {
dh, err := NewDirectHook(defaultSystem, defaultFacility)
if dh != nil {
dh.role = defaultRole
}
return dh, err
}
func (h *DirectHook) Levels() []logrus.Level {
// Everything except Trace
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
// logrus.TraceLevel,
}
}
func (h *DirectHook) Fire(e *logrus.Entry) error {
// Supported field names:
// Severity, Level, ErrorCode, SourceFile, SourceLine,
// Facility, Role, System, Detector, Partition, Run
// Filled automatically by InfoLogger, do not set: PID, hostName, userName
payload := make(map[string]string)
payload["severity"] = logrusLevelToInfoLoggerSeverity(e.Level)
if _, hasLevel := payload["level"]; !hasLevel {
payload["level"] = logrusEntryToInfoLoggerLevel(e)
}
payload["timestamp"] = utils.NewUnixTimestamp()
payload["hostname"] = hostname
payload["pid"] = pid
payload["username"] = username
if e.HasCaller() {
payload["errsource"] = e.Caller.File
payload["errline"] = strconv.Itoa(e.Caller.Line)
}
var message strings.Builder
message.WriteString(e.Message)
unmappableFields := make(sort.StringSlice, 0)
if e.Data != nil {
for k, v := range e.Data {
if k == "prefix" {
continue
}
if k == "nohooks" {
if vBool, ok := v.(bool); ok {
if vBool {
return nil
}
}
}
var vStr string
switch v.(type) {
case string:
vStr = v.(string)
case []byte:
vStr = string(v.([]byte)[:])
case fmt.Stringer:
vStr = v.(fmt.Stringer).String()
default:
vStr = fmt.Sprintf("%v", v)
}
if _, ok := Fields[k]; ok {
if k == Facility {
payload[Facility] = buildFineFacility(vStr, e.Data)
} else {
payload[k] = vStr
}
} else { // data structure key not mappable to InfoLogger field
unmappableFields = append(unmappableFields, fmt.Sprintf(" %s=\"%s\"", k, vStr))
}
}
}
// If a System, Facility or Role isn't passed manually, we fall back on defaults
if _, ok := e.Data[System]; !ok {
payload[System] = h.system
}
if _, ok := e.Data[Facility]; !ok {
payload[Facility] = buildFineFacility(h.facility, e.Data)
}
if _, ok := e.Data[Role]; !ok {
payload[Role] = h.role
}
unmappableFields.Sort()
for _, v := range unmappableFields {
message.WriteString(v)
}
messageStr := message.String()
// IL won't be happy if we send a message longer than 1024 bytes
if len(messageStr) > INFOLOGGER_MAX_MESSAGE_SIZE {
messageStr = messageStr[:INFOLOGGER_MAX_MESSAGE_SIZE]
}
payload["message"] = messageStr
if h.il != nil {
err := h.il.Send(payload)
if err != nil {
return fmt.Errorf("infoLogger hook error: %s", err.Error())
}
} else {
return fmt.Errorf("infoLogger hook error: sender not available")
}
return nil
}