mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Add support for json formatted logs (#1245)
This commit is contained in:
committed by
Reed Allman
parent
3920e15769
commit
4d30b9de09
@@ -9,15 +9,26 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func SetLogFormat(format string) {
|
||||
if format != "text" && format != "json" {
|
||||
logrus.WithFields(logrus.Fields{"format": format}).Warn("Unknown log format specified, using text. Possible options are json and text.")
|
||||
}
|
||||
|
||||
if format == "json" {
|
||||
logrus.SetFormatter(&logrus.JSONFormatter{})
|
||||
} else {
|
||||
// show full timestamps
|
||||
formatter := &logrus.TextFormatter{
|
||||
FullTimestamp: true,
|
||||
}
|
||||
logrus.SetFormatter(formatter)
|
||||
}
|
||||
}
|
||||
|
||||
func SetLogLevel(ll string) {
|
||||
if ll == "" {
|
||||
ll = "info"
|
||||
}
|
||||
// show full timestamps
|
||||
formatter := &logrus.TextFormatter{
|
||||
FullTimestamp: true,
|
||||
}
|
||||
logrus.SetFormatter(formatter)
|
||||
|
||||
logrus.WithFields(logrus.Fields{"level": ll}).Info("Setting log level to")
|
||||
logLevel, err := logrus.ParseLevel(ll)
|
||||
|
||||
@@ -48,6 +48,9 @@ const (
|
||||
// forcing usage through WithXxx configuration methods and documenting there vs.
|
||||
// expecting users to use os.SetEnv(EnvLogLevel, "debug") // why ?
|
||||
|
||||
// EnvLogFormat sets the stderr logging format, text or json only
|
||||
EnvLogFormat = "FN_LOG_FORMAT"
|
||||
|
||||
// EnvLogLevel sets the stderr logging level
|
||||
EnvLogLevel = "FN_LOG_LEVEL"
|
||||
|
||||
@@ -116,6 +119,9 @@ const (
|
||||
// EnvMaxRequestSize sets the limit in bytes for any API request's length.
|
||||
EnvMaxRequestSize = "FN_MAX_REQUEST_SIZE"
|
||||
|
||||
// DefaultLogFormat is text
|
||||
DefaultLogFormat = "text"
|
||||
|
||||
// DefaultLogLevel is info
|
||||
DefaultLogLevel = "info"
|
||||
|
||||
@@ -246,6 +252,7 @@ func NewFromEnv(ctx context.Context, opts ...Option) *Server {
|
||||
}
|
||||
opts = append(opts, WithWebPort(getEnvInt(EnvPort, DefaultPort)))
|
||||
opts = append(opts, WithGRPCPort(getEnvInt(EnvGRPCPort, DefaultGRPCPort)))
|
||||
opts = append(opts, WithLogFormat(getEnv(EnvLogFormat, DefaultLogFormat)))
|
||||
opts = append(opts, WithLogLevel(getEnv(EnvLogLevel, DefaultLogLevel)))
|
||||
opts = append(opts, WithLogDest(getEnv(EnvLogDest, DefaultLogDest), getEnv(EnvLogPrefix, "")))
|
||||
opts = append(opts, WithZipkin(getEnv(EnvZipkinURL, "")))
|
||||
@@ -309,6 +316,14 @@ func WithGRPCPort(port int) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// WithLogFormat maps EnvLogFormat
|
||||
func WithLogFormat(format string) Option {
|
||||
return func(ctx context.Context, s *Server) error {
|
||||
common.SetLogFormat(format)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithLogLevel maps EnvLogLevel
|
||||
func WithLogLevel(ll string) Option {
|
||||
return func(ctx context.Context, s *Server) error {
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
|
||||
func testServer(ds models.Datastore, mq models.MessageQueue, logDB models.LogStore, rnr agent.Agent, nodeType NodeType, opts ...Option) *Server {
|
||||
return New(context.Background(), append(opts,
|
||||
WithLogFormat("text"),
|
||||
WithLogLevel("debug"),
|
||||
WithDatastore(ds),
|
||||
WithMQ(mq),
|
||||
|
||||
@@ -179,6 +179,7 @@ func SetUpAPINode(ctx context.Context) (*server.Server, error) {
|
||||
opts := make([]server.Option, 0)
|
||||
opts = append(opts, server.WithWebPort(8085))
|
||||
opts = append(opts, server.WithType(nodeType))
|
||||
opts = append(opts, server.WithLogFormat(getEnv(server.EnvLogFormat, server.DefaultLogFormat)))
|
||||
opts = append(opts, server.WithLogLevel(getEnv(server.EnvLogLevel, server.DefaultLogLevel)))
|
||||
opts = append(opts, server.WithLogDest(getEnv(server.EnvLogDest, server.DefaultLogDest), "API"))
|
||||
opts = append(opts, server.WithDBURL(getEnv(server.EnvDBURL, defaultDB)))
|
||||
@@ -196,6 +197,7 @@ func SetUpLBNode(ctx context.Context) (*server.Server, error) {
|
||||
opts := make([]server.Option, 0)
|
||||
opts = append(opts, server.WithWebPort(8081))
|
||||
opts = append(opts, server.WithType(nodeType))
|
||||
opts = append(opts, server.WithLogFormat(getEnv(server.EnvLogFormat, server.DefaultLogFormat)))
|
||||
opts = append(opts, server.WithLogLevel(getEnv(server.EnvLogLevel, server.DefaultLogLevel)))
|
||||
opts = append(opts, server.WithLogDest(getEnv(server.EnvLogDest, server.DefaultLogDest), "LB"))
|
||||
opts = append(opts, server.WithDBURL(""))
|
||||
@@ -243,6 +245,7 @@ func SetUpPureRunnerNode(ctx context.Context, nodeNum int) (*server.Server, erro
|
||||
opts = append(opts, server.WithWebPort(8082+nodeNum))
|
||||
opts = append(opts, server.WithGRPCPort(9190+nodeNum))
|
||||
opts = append(opts, server.WithType(nodeType))
|
||||
opts = append(opts, server.WithLogFormat(getEnv(server.EnvLogFormat, server.DefaultLogFormat)))
|
||||
opts = append(opts, server.WithLogLevel(getEnv(server.EnvLogLevel, server.DefaultLogLevel)))
|
||||
opts = append(opts, server.WithLogDest(getEnv(server.EnvLogDest, server.DefaultLogDest), "PURE-RUNNER"))
|
||||
opts = append(opts, server.WithDBURL(""))
|
||||
|
||||
Reference in New Issue
Block a user