2020-07-01 02:48:58 +08:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
2021-11-04 09:54:04 +08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2020-09-18 22:20:09 +08:00
|
|
|
|
2020-07-01 02:48:58 +08:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-04 09:54:04 +08:00
|
|
|
"github.com/slackhq/nebula/config"
|
2020-07-01 02:48:58 +08:00
|
|
|
)
|
|
|
|
|
2021-11-04 09:54:04 +08:00
|
|
|
func configLogger(l *logrus.Logger, c *config.C) error {
|
|
|
|
// set up our logging level
|
|
|
|
logLevel, err := logrus.ParseLevel(strings.ToLower(c.GetString("logging.level", "info")))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s; possible levels: %s", err, logrus.AllLevels)
|
|
|
|
}
|
|
|
|
l.SetLevel(logLevel)
|
|
|
|
|
|
|
|
disableTimestamp := c.GetBool("logging.disable_timestamp", false)
|
|
|
|
timestampFormat := c.GetString("logging.timestamp_format", "")
|
|
|
|
fullTimestamp := (timestampFormat != "")
|
|
|
|
if timestampFormat == "" {
|
|
|
|
timestampFormat = time.RFC3339
|
|
|
|
}
|
|
|
|
|
|
|
|
logFormat := strings.ToLower(c.GetString("logging.format", "text"))
|
|
|
|
switch logFormat {
|
|
|
|
case "text":
|
|
|
|
l.Formatter = &logrus.TextFormatter{
|
|
|
|
TimestampFormat: timestampFormat,
|
|
|
|
FullTimestamp: fullTimestamp,
|
|
|
|
DisableTimestamp: disableTimestamp,
|
|
|
|
}
|
|
|
|
case "json":
|
|
|
|
l.Formatter = &logrus.JSONFormatter{
|
|
|
|
TimestampFormat: timestampFormat,
|
|
|
|
DisableTimestamp: disableTimestamp,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown log format `%s`. possible formats: %s", logFormat, []string{"text", "json"})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|