Skip to content

Commit

Permalink
Merge pull request #184 from seankhliao/jsonlog
Browse files Browse the repository at this point in the history
add log format option
  • Loading branch information
bboreham authored Jun 25, 2020
2 parents 384f100 + dd502cc commit 4b18475
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
52 changes: 52 additions & 0 deletions logging/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package logging

import (
"flag"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// Format is a settable identifier for the output format of logs
type Format struct {
s string
Logrus logrus.Formatter
}

// RegisterFlags adds the log format flag to the provided flagset.
func (f *Format) RegisterFlags(fs *flag.FlagSet) {
f.Set("logfmt")
fs.Var(f, "log.format", "Output log messages in the given format. Valid formats: [logfmt, json]")
}

func (f Format) String() string {
return f.s
}

// UnmarshalYAML implements yaml.Unmarshaler.
func (f *Format) UnmarshalYAML(unmarshal func(interface{}) error) error {
var format string
if err := unmarshal(&format); err != nil {
return err
}
return f.Set(format)
}

// MarshalYAML implements yaml.Marshaler.
func (f Format) MarshalYAML() (interface{}, error) {
return f.String(), nil
}

// Set updates the value of the output format. Implements flag.Value
func (f *Format) Set(s string) error {
switch s {
case "logfmt":
f.Logrus = &logrus.JSONFormatter{}
case "json":
f.Logrus = &logrus.JSONFormatter{}
default:
return errors.Errorf("unrecognized log format %q", s)
}
f.s = s
return nil
}
17 changes: 14 additions & 3 deletions logging/gokit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,25 @@ import (
"github.com/go-kit/kit/log/level"
)

// NewGoKit creates a new Interface backed by a GoKit logger
func NewGoKit(l Level) Interface {
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
// NewGoKitFormat creates a new Interface backed by a GoKit logger
// format can be "json" or defaults to logfmt
func NewGoKitFormat(l Level, f Format) Interface {
var logger log.Logger
if f.s == "json" {
logger = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
} else {
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
}
logger = level.NewFilter(logger, l.Gokit)
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
return gokit{logger}
}

// NewGoKit creates a new Interface backed by a GoKit logger
func NewGoKit(l Level) Interface {
return NewGoKitFormat(l, Format{s: "logfmt"})
}

// GoKit wraps an existing gokit Logger.
func GoKit(logger log.Logger) Interface {
return gokit{logger}
Expand Down
11 changes: 9 additions & 2 deletions logging/logrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ import (
"github.com/sirupsen/logrus"
)

// NewLogrus makes a new Interface backed by a logrus logger
func NewLogrus(level Level) Interface {
// NewLogrusFormat makes a new Interface backed by a logrus logger
// format can be "json" or defaults to logfmt
func NewLogrusFormat(level Level, f Format) Interface {
log := logrus.New()
log.Out = os.Stderr
log.Level = level.Logrus
log.Formatter = f.Logrus
return logrusLogger{log}
}

// NewLogrus makes a new Interface backed by a logrus logger
func NewLogrus(level Level) Interface {
return NewLogrusFormat(level, Format{Logrus: &logrus.TextFormatter{}})
}

// Logrus wraps an existing Logrus logger.
func Logrus(l *logrus.Logger) Interface {
return logrusLogger{l}
Expand Down
6 changes: 4 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ type Config struct {
GRPCServerTime time.Duration `yaml:"grpc_server_keepalive_time"`
GRPCServerTimeout time.Duration `yaml:"grpc_server_keepalive_timeout"`

LogLevel logging.Level `yaml:"log_level"`
Log logging.Interface `yaml:"-"`
LogFormat logging.Format `yaml:"log_format"`
LogLevel logging.Level `yaml:"log_level"`
Log logging.Interface `yaml:"-"`

// If not set, default signal handler is used.
SignalHandler SignalHandler `yaml:"-"`
Expand Down Expand Up @@ -117,6 +118,7 @@ func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
f.DurationVar(&cfg.GRPCServerTime, "server.grpc.keepalive.time", time.Hour*2, "Duration after which a keepalive probe is sent in case of no activity over the connection., Default: 2h")
f.DurationVar(&cfg.GRPCServerTimeout, "server.grpc.keepalive.timeout", time.Second*20, "After having pinged for keepalive check, the duration after which an idle connection should be closed, Default: 20s")
f.StringVar(&cfg.PathPrefix, "server.path-prefix", "", "Base path to serve all API routes from (e.g. /v1/)")
cfg.LogFormat.RegisterFlags(f)
cfg.LogLevel.RegisterFlags(f)
}

Expand Down

0 comments on commit 4b18475

Please sign in to comment.