forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rfc3164_formatter.go
56 lines (45 loc) · 1.74 KB
/
rfc3164_formatter.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package syslogexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/syslogexporter"
import (
"fmt"
"strconv"
"go.opentelemetry.io/collector/pdata/plog"
)
type rfc3164Formatter struct {
}
func newRFC3164Formatter() *rfc3164Formatter {
return &rfc3164Formatter{}
}
func (f *rfc3164Formatter) format(logRecord plog.LogRecord) string {
priorityString := f.formatPriority(logRecord)
timestampString := f.formatTimestamp(logRecord)
hostnameString := f.formatHostname(logRecord)
appnameString := f.formatAppname(logRecord)
messageString := f.formatMessage(logRecord)
appnameMessageDelimiter := ""
if len(appnameString) > 0 && messageString != emptyMessage {
appnameMessageDelimiter = " "
}
formatted := fmt.Sprintf("<%s>%s %s %s%s%s\n", priorityString, timestampString, hostnameString, appnameString, appnameMessageDelimiter, messageString)
return formatted
}
func (f *rfc3164Formatter) formatPriority(logRecord plog.LogRecord) string {
return getAttributeValueOrDefault(logRecord, priority, strconv.Itoa(defaultPriority))
}
func (f *rfc3164Formatter) formatTimestamp(logRecord plog.LogRecord) string {
return logRecord.Timestamp().AsTime().Format("Jan 02 15:04:05")
}
func (f *rfc3164Formatter) formatHostname(logRecord plog.LogRecord) string {
return getAttributeValueOrDefault(logRecord, hostname, emptyValue)
}
func (f *rfc3164Formatter) formatAppname(logRecord plog.LogRecord) string {
value := getAttributeValueOrDefault(logRecord, app, "")
if value != "" {
value += ":"
}
return value
}
func (f *rfc3164Formatter) formatMessage(logRecord plog.LogRecord) string {
return getAttributeValueOrDefault(logRecord, message, emptyMessage)
}