Add support for json formatted logs (#1245)

This commit is contained in:
Matt Stephenson
2018-09-26 16:04:55 -07:00
committed by Reed Allman
parent 3920e15769
commit 4d30b9de09
4 changed files with 35 additions and 5 deletions

View File

@@ -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 {

View File

@@ -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),