2020-06-30 20:48:58 +02:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
2020-09-18 16:20:09 +02:00
|
|
|
"errors"
|
2021-11-04 02:54:04 +01:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2020-09-18 16:20:09 +02:00
|
|
|
|
2020-06-30 20:48:58 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-04 02:54:04 +01:00
|
|
|
"github.com/slackhq/nebula/config"
|
2020-06-30 20:48:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type ContextualError struct {
|
|
|
|
RealError error
|
|
|
|
Fields map[string]interface{}
|
|
|
|
Context string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewContextualError(msg string, fields map[string]interface{}, realError error) ContextualError {
|
|
|
|
return ContextualError{Context: msg, Fields: fields, RealError: realError}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ce ContextualError) Error() string {
|
2020-09-18 16:20:09 +02:00
|
|
|
if ce.RealError == nil {
|
|
|
|
return ce.Context
|
|
|
|
}
|
2020-06-30 20:48:58 +02:00
|
|
|
return ce.RealError.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ce ContextualError) Unwrap() error {
|
2020-09-18 16:20:09 +02:00
|
|
|
if ce.RealError == nil {
|
|
|
|
return errors.New(ce.Context)
|
|
|
|
}
|
2020-06-30 20:48:58 +02:00
|
|
|
return ce.RealError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ce *ContextualError) Log(lr *logrus.Logger) {
|
|
|
|
if ce.RealError != nil {
|
|
|
|
lr.WithFields(ce.Fields).WithError(ce.RealError).Error(ce.Context)
|
|
|
|
} else {
|
|
|
|
lr.WithFields(ce.Fields).Error(ce.Context)
|
|
|
|
}
|
|
|
|
}
|
2021-11-04 02:54:04 +01: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
|
|
|
|
}
|