-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_logrus.go
75 lines (64 loc) · 1.86 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
package logging
import (
"fmt"
"io/ioutil"
"os"
syslog "github.com/RackSec/srslog"
"github.com/Sirupsen/logrus"
logrus_syslog "github.com/shinji62/logrus-syslog-ng"
)
type LoggingLogrus struct {
Logger *logrus.Logger
syslogServer string
debugFlag bool
logFormatterType string
certPath string
syslogProtocol string
}
func NewLogging(SyslogServerFlag string, SysLogProtocolFlag string, LogFormatterFlag string, certP string, DebugFlag bool) Logging {
return &LoggingLogrus{
Logger: logrus.New(),
syslogServer: SyslogServerFlag,
logFormatterType: LogFormatterFlag,
syslogProtocol: SysLogProtocolFlag,
certPath: certP,
debugFlag: DebugFlag,
}
}
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)
} 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 {
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{}
}
}