-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathlogging_logrus.go
93 lines (81 loc) · 2.67 KB
/
logging_logrus.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
package logging
import (
"fmt"
"io/ioutil"
"os"
"time"
syslog "github.com/RackSec/srslog"
logrus_syslog "github.com/shinji62/logrus-syslog-ng"
"github.com/sirupsen/logrus"
)
type LoggingLogrus struct {
Logger *logrus.Logger
syslogServer string
debugFlag bool
logFormatterType string
certPath string
syslogProtocol string
skipSSL bool
}
func NewLogging(SyslogServerFlag string, SysLogProtocolFlag string, LogFormatterFlag string, certP string, DebugFlag bool, skipSSLFlag bool) Logging {
return &LoggingLogrus{
Logger: logrus.New(),
syslogServer: SyslogServerFlag,
logFormatterType: LogFormatterFlag,
syslogProtocol: SysLogProtocolFlag,
certPath: certP,
debugFlag: DebugFlag,
skipSSL: skipSSLFlag,
}
}
// This srslog formatter is based on srslog/formatter.go's RFC3164Formatter.
// The default rsyslog input module expects RFC3164 formatted logs, using srslog's DefaultFormatter with "@cee:" wasn't
// compatible enough to get it working.
// some historical context on the efforts to standardize the syslog message
// format(s): https://www.rsyslog.com/doc/syslog_parsing.html
func CeeFormatter(p syslog.Priority, hostname, tag, content string) string {
timestamp := time.Now().Format(time.Stamp)
msg := fmt.Sprintf("<%d>%s %s %s[%d]: @cee: %s",
p, timestamp, hostname, tag, os.Getpid(), content)
return msg
}
func (l *LoggingLogrus) Connect() bool {
success := false
l.Logger.Formatter = GetLogFormatter(l.logFormatterType)
if !l.debugFlag {
l.Logger.Out = ioutil.Discard
} else {
l.Logger.Out = os.Stdout
}
if l.syslogServer != "" {
var hook logrus.Hook
var err error
if l.syslogProtocol == logrus_syslog.SecureProto {
hook, err = logrus_syslog.NewSyslogHookTls(l.syslogServer, syslog.LOG_INFO, "doppler", l.certPath, l.skipSSL)
} else {
hook, err = logrus_syslog.NewSyslogHook(l.syslogProtocol, l.syslogServer, syslog.LOG_INFO, "doppler")
}
if err != nil {
LogError(fmt.Sprintf("Unable to connect to syslog server [%s]!\n", l.syslogServer), err.Error())
} else {
if l.logFormatterType == "json-cee" {
hook.(*logrus_syslog.SyslogHook).Writer.SetFormatter(CeeFormatter)
}
LogStd(fmt.Sprintf("Received hook to syslog server [%s]!\n", l.syslogServer), false)
l.Logger.Hooks.Add(hook)
success = true
}
}
return success
}
func (l *LoggingLogrus) ShipEvents(eventFields map[string]interface{}, Message string) {
l.Logger.WithFields(eventFields).Info(Message)
}
func GetLogFormatter(logFormatterType string) logrus.Formatter {
switch logFormatterType {
case "text":
return &logrus.TextFormatter{}
default:
return &logrus.JSONFormatter{}
}
}