forked from netlify/git-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.go
61 lines (52 loc) · 1.72 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
package conf
import (
"os"
"time"
"github.com/sirupsen/logrus"
)
type LoggingConfig struct {
Level string `mapstructure:"log_level" json:"log_level"`
File string `mapstructure:"log_file" json:"log_file"`
DisableColors bool `mapstructure:"disable_colors" split_words:"true" json:"disable_colors"`
QuoteEmptyFields bool `mapstructure:"quote_empty_fields" split_words:"true" json:"quote_empty_fields"`
TSFormat string `mapstructure:"ts_format" json:"ts_format"`
Fields map[string]interface{} `mapstructure:"fields" json:"fields"`
UseNewLogger bool `mapstructure:"use_new_logger" split_words:"true"`
}
func ConfigureLogging(config *LoggingConfig) (*logrus.Entry, error) {
logger := logrus.New()
tsFormat := time.RFC3339Nano
if config.TSFormat != "" {
tsFormat = config.TSFormat
}
// always use the full timestamp
logger.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
DisableTimestamp: false,
TimestampFormat: tsFormat,
DisableColors: config.DisableColors,
QuoteEmptyFields: config.QuoteEmptyFields,
}
// use a file if you want
if config.File != "" {
f, errOpen := os.OpenFile(config.File, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0664)
if errOpen != nil {
return nil, errOpen
}
logger.Out = f
logger.Infof("Set output file to %s", config.File)
}
if config.Level != "" {
level, err := logrus.ParseLevel(config.Level)
if err != nil {
return nil, err
}
logger.SetLevel(level)
logger.Debug("Set log level to: " + logger.Level.String())
}
f := logrus.Fields{}
for k, v := range config.Fields {
f[k] = v
}
return logger.WithFields(f), nil
}