-
Notifications
You must be signed in to change notification settings - Fork 1
/
logging.go
147 lines (122 loc) · 3.61 KB
/
logging.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
package observance
import (
"io"
"os"
"time"
logrusSentry "github.com/evalphobia/logrus_sentry"
"github.com/sirupsen/logrus"
)
// Logger is a general interface to be implemented for multiple loggers.
type Logger interface {
Level() string
Trace(msg interface{})
Debug(msg interface{})
Info(msg interface{})
Warn(msg interface{})
Error(msg interface{})
WithField(key string, value interface{}) Logger
WithFields(fields Fields) Logger
WithError(err error) Logger
SetOutput(w io.Writer)
}
// Fields is a type alias to ease reading.
type Fields = map[string]interface{}
// LogrusLogger wraps Logrus to provide an implementation of the Logger interface.
type LogrusLogger struct {
basicLogger *logrus.Logger
logger *logrus.Entry
}
// Level returns the log level that was set for the logger.
// Only entries with that level or above with be logged.
func (l *LogrusLogger) Level() string {
return l.basicLogger.Level.String()
}
// Trace writes a log entry with level "trace".
func (l *LogrusLogger) Trace(msg interface{}) {
l.logger.Trace(msg)
}
// Debug writes a log entry with level "debug".
func (l *LogrusLogger) Debug(msg interface{}) {
l.logger.Debug(msg)
}
// Info writes a log entry with level "info".
func (l *LogrusLogger) Info(msg interface{}) {
l.logger.Info(msg)
}
// Warn writes a log entry with level "warning".
func (l *LogrusLogger) Warn(msg interface{}) {
l.logger.Warn(msg)
}
// Error writes a log entry with level "error".
func (l *LogrusLogger) Error(msg interface{}) {
l.logger.Error(msg)
}
// WithField adds an additional field for logging.
func (l *LogrusLogger) WithField(key string, value interface{}) Logger {
return &LogrusLogger{
basicLogger: l.basicLogger,
logger: l.logger.WithField(key, value),
}
}
// WithFields allows to add multiple additional fields to the logging.
// The argument needs to be of type Fields (map[string]interface{}).
func (l *LogrusLogger) WithFields(fields Fields) Logger {
return &LogrusLogger{
basicLogger: l.basicLogger,
logger: l.logger.WithFields(logrus.Fields(fields)),
}
}
// WithError adds an error for logging.
func (l *LogrusLogger) WithError(err error) Logger {
return &LogrusLogger{
basicLogger: l.basicLogger,
logger: l.logger.WithError(err),
}
}
// SetOutput changes where the logs are written to. The default is Stdout.
func (l *LogrusLogger) SetOutput(w io.Writer) {
l.basicLogger.SetOutput(w)
}
// NewLogrus creates a Logrus logger that fulfils the Logger interface with Sentry integration.
// All log messages will contain app name, pid and hostname/containerID.
func NewLogrus(logLevel string, appName string, sentryURL string, version string) (Logger, error) {
logrusLogLevel, err := logrus.ParseLevel(logLevel)
if err != nil {
return nil, err
}
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
basicLogger := &logrus.Logger{
Out: os.Stdout,
Formatter: &logrus.JSONFormatter{TimestampFormat: time.RFC3339Nano},
Hooks: make(logrus.LevelHooks),
Level: logrusLogLevel,
}
if sentryURL != "" {
hook, err := logrusSentry.NewAsyncSentryHook(sentryURL, []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
})
if err != nil {
return nil, err
}
if version != "" {
hook.SetRelease(version)
}
// default timeout of 100ms was too low for first event that is fired
hook.Timeout = 500 * time.Millisecond
basicLogger.Hooks.Add(hook)
}
logger := basicLogger.WithFields(logrus.Fields{
"name": appName,
"pid": os.Getpid(),
"hostname": hostname,
})
return &LogrusLogger{
basicLogger: basicLogger,
logger: logger,
}, nil
}