diff --git a/README.md b/README.md index 11c454b..c8439ed 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ NewLogger(wr io.Writer, formatter Formatter, hooks ...Hook) Logger ``` Available formatters: -* `suplog.JSONFormatter` — suplogs all log entries as JSON objects -* `suplog.TextFormatter` — suplogs log entries as text lines for TTY or without TTY colors. +* `suplog.TextFormatter` — suplogs log entries as text lines for TTY or without TTY colors (`LOG_FORMATTER=text`) +* `suplog.JSONFormatter` — suplogs all log entries as JSON objects (`LOG_FORMATTER=json`) Available hooks: * [github.com/xlab/suplog/hooks/debug](https://github.com/xlab/suplog/blob/master/hooks/debug/hook.go#L14) diff --git a/suplog.go b/suplog.go index 66498f2..b7849f3 100644 --- a/suplog.go +++ b/suplog.go @@ -15,10 +15,16 @@ import ( "github.com/xlab/suplog/stackcache" ) -// NewLogger constructs a new suplogger. +// NewLogger constructs a new suplogger. The default formatter would be TextFormatter +// if not overrident by and env variable. func NewLogger(wr io.Writer, formatter Formatter, hooks ...Hook) Logger { if formatter == nil { - formatter = new(TextFormatter) + switch os.Getenv("LOG_FORMATTER") { + case "json": + formatter = new(JSONFormatter) + default: + formatter = new(TextFormatter) + } } log := &suplogger{ @@ -70,10 +76,18 @@ func (l *suplogger) initOnce() { l.writer = os.Stderr } + var formatter Formatter + switch os.Getenv("LOG_FORMATTER") { + case "json": + formatter = new(JSONFormatter) + default: + formatter = new(TextFormatter) + } + // otherwise init output with conservative defaults l.logger = &logrus.Logger{ Out: l.writer, - Formatter: new(TextFormatter), + Formatter: formatter, Hooks: make(LevelHooks), Level: DebugLevel, ExitFunc: closer.Exit,