-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
54 lines (44 loc) · 1.67 KB
/
logger.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
package logger
import (
"github.com/sirupsen/logrus"
)
const (
serviceStartedEvent = "service_started"
)
// UPPLogger wraps logrus logger providing the same functionality as logrus with a few UPP specifics.
type UPPLogger struct {
*logrus.Logger
keyConf *KeyNamesConfig
}
// NewUPPLogger initializes UPP logger with structured logging format.
func NewUPPLogger(serviceName string, logLevel string, kconf ...KeyNamesConfig) *UPPLogger {
keyConf := GetDefaultKeyNamesConfig()
if len(kconf) > 0 {
keyConf = GetFullKeyNameConfig(kconf[0])
}
logrusLog := logrus.New()
formatter := newFTJSONFormatter(serviceName, keyConf)
logrusLog.Formatter = formatter
parsedLogLevel, err := logrus.ParseLevel(logLevel)
if err != nil {
logrusLog.WithField("logLevel", logLevel).WithError(err).Error("Incorrect log level. Using INFO instead.")
parsedLogLevel = logrus.InfoLevel
}
logrusLog.SetLevel(parsedLogLevel)
return &UPPLogger{Logger: logrusLog, keyConf: keyConf}
}
// NewUPPInfoLogger initializes UPPLogger with log level INFO.
func NewUPPInfoLogger(serviceName string, kconf ...KeyNamesConfig) *UPPLogger {
return NewUPPLogger(serviceName, logrus.InfoLevel.String(), kconf...)
}
// NewUnstructuredLogger initializes plain logrus log without taking into account UPP log formatting.
func NewUnstructuredLogger() *UPPLogger {
return &UPPLogger{Logger: logrus.New(), keyConf: GetDefaultKeyNamesConfig()}
}
// LogServiceStartedEvent logs service started event with level INFO.
func (ulog *UPPLogger) LogServiceStartedEvent(port int) {
fields := map[string]interface{}{
ulog.keyConf.KeyEventName: serviceStartedEvent,
}
ulog.WithFields(fields).Infof("Service running on port [%d]", port)
}