github.com/slackhq/nebula@v1.9.0/logger.go (about)

     1  package nebula
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/sirupsen/logrus"
     9  	"github.com/slackhq/nebula/config"
    10  )
    11  
    12  func configLogger(l *logrus.Logger, c *config.C) error {
    13  	// set up our logging level
    14  	logLevel, err := logrus.ParseLevel(strings.ToLower(c.GetString("logging.level", "info")))
    15  	if err != nil {
    16  		return fmt.Errorf("%s; possible levels: %s", err, logrus.AllLevels)
    17  	}
    18  	l.SetLevel(logLevel)
    19  
    20  	disableTimestamp := c.GetBool("logging.disable_timestamp", false)
    21  	timestampFormat := c.GetString("logging.timestamp_format", "")
    22  	fullTimestamp := (timestampFormat != "")
    23  	if timestampFormat == "" {
    24  		timestampFormat = time.RFC3339
    25  	}
    26  
    27  	logFormat := strings.ToLower(c.GetString("logging.format", "text"))
    28  	switch logFormat {
    29  	case "text":
    30  		l.Formatter = &logrus.TextFormatter{
    31  			TimestampFormat:  timestampFormat,
    32  			FullTimestamp:    fullTimestamp,
    33  			DisableTimestamp: disableTimestamp,
    34  		}
    35  	case "json":
    36  		l.Formatter = &logrus.JSONFormatter{
    37  			TimestampFormat:  timestampFormat,
    38  			DisableTimestamp: disableTimestamp,
    39  		}
    40  	default:
    41  		return fmt.Errorf("unknown log format `%s`. possible formats: %s", logFormat, []string{"text", "json"})
    42  	}
    43  
    44  	return nil
    45  }