Skip to content

Commit

Permalink
add log format option
Browse files Browse the repository at this point in the history
  • Loading branch information
seankhliao committed Apr 1, 2020
1 parent 2708ba4 commit 00af769
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
5 changes: 4 additions & 1 deletion logging/gokit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
)

// NewGoKit creates a new Interface backed by a GoKit logger
func NewGoKit(l Level) Interface {
func NewGoKit(l Level, format string) Interface {
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
if format == "json" {
logger = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
}
logger = level.NewFilter(logger, l.Gokit)
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
return gokit{logger}
Expand Down
5 changes: 4 additions & 1 deletion logging/logrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (
)

// NewLogrus makes a new Interface backed by a logrus logger
func NewLogrus(level Level) Interface {
func NewLogrus(level Level, format string) Interface {
log := logrus.New()
log.Out = os.Stderr
log.Level = level.Logrus
if format == "json" {
log.Formatter = &logrus.JSONFormatter{}
}
return logrusLogger{log}
}

Expand Down
7 changes: 4 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,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 string `yaml:"log_format"`
LogLevel logging.Level `yaml:"log_level"`
Log logging.Interface `yaml:"-"`

PathPrefix string `yaml:"http_path_prefix"`
}
Expand Down Expand Up @@ -141,7 +142,7 @@ func New(cfg Config) (*Server, error) {
// logrus.
log := cfg.Log
if log == nil {
log = logging.NewLogrus(cfg.LogLevel)
log = logging.NewLogrus(cfg.LogLevel, cfg.LogFormat)
}

log.WithField("http", httpListener.Addr()).WithField("grpc", grpcListener.Addr()).Infof("server listening on addresses")
Expand Down

0 comments on commit 00af769

Please sign in to comment.