Skip to content

Commit

Permalink
Implement LOG_FORMAT=json
Browse files Browse the repository at this point in the history
Centralized log collection system works better with logs in json format.
E.g. DataDog strongly encourage setting up your logging library to produce your logs in JSON format to avoid the need for custom parsing rules.
So, the next small fix is all we need to get json logs.

Signed-off-by: Sergei Belyaev <[email protected]>
  • Loading branch information
SergeyBelyaevSetronica committed Sep 10, 2020
1 parent 9b42196 commit cdbee08
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/service_cmd/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"math/rand"
"net/http"
"strings"
"time"

stats "github.com/lyft/gostats"
Expand Down Expand Up @@ -43,6 +44,16 @@ func (runner *Runner) Run() {
} else {
logger.SetLevel(logLevel)
}
if strings.ToLower(s.LogFormat) == "json" {
logger.SetFormatter(&logger.JSONFormatter{
TimestampFormat: time.RFC3339Nano,
FieldMap: logger.FieldMap{
logger.FieldKeyTime: "@timestamp",
logger.FieldKeyMsg: "@message",
},
})
}

var localCache *freecache.Cache
if s.LocalCacheSizeInBytes != 0 {
localCache = freecache.NewCache(s.LocalCacheSizeInBytes)
Expand Down
1 change: 1 addition & 0 deletions src/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Settings struct {
RuntimeIgnoreDotFiles bool `envconfig:"RUNTIME_IGNOREDOTFILES" default:"false"`
RuntimeWatchRoot bool `envconfig:"RUNTIME_WATCH_ROOT" default:"true"`
LogLevel string `envconfig:"LOG_LEVEL" default:"WARN"`
LogFormat string `envconfig:"LOG_FORMAT" default:"text"`
RedisSocketType string `envconfig:"REDIS_SOCKET_TYPE" default:"unix"`
RedisUrl string `envconfig:"REDIS_URL" default:"/var/run/nutcracker/ratelimit.sock"`
RedisPoolSize int `envconfig:"REDIS_POOL_SIZE" default:"10"`
Expand Down

0 comments on commit cdbee08

Please sign in to comment.