Skip to content

Commit

Permalink
promlog: add support for json logger (weaveworks#136)
Browse files Browse the repository at this point in the history
Fixes issue: prometheus/prometheus#3219

Signed-off-by: Alex Yu <yu.alex96@gmail.com>
  • Loading branch information
alexander-yu authored and brian-brazil committed Nov 13, 2018
1 parent a04bd04 commit 41aa239
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
14 changes: 12 additions & 2 deletions promlog/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,19 @@ const LevelFlagName = "log.level"
// LevelFlagHelp is the help description for the log.level flag.
const LevelFlagHelp = "Only log messages with the given severity or above. One of: [debug, info, warn, error]"

// FormatFlagName is the canonical flag name to configure the log format
// within Prometheus projects.
const FormatFlagName = "log.format"

// FormatFlagHelp is the help description for the log.format flag.
const FormatFlagHelp = "Output format of log messages. One of: [logfmt, json]"

// AddFlags adds the flags used by this package to the Kingpin application.
// To use the default Kingpin application, call AddFlags(kingpin.CommandLine)
func AddFlags(a *kingpin.Application, logLevel *promlog.AllowedLevel) {
func AddFlags(a *kingpin.Application, config *promlog.Config) {
a.Flag(LevelFlagName, LevelFlagHelp).
Default("info").SetValue(logLevel)
Default("info").SetValue(config.Level)

a.Flag(FormatFlagName, FormatFlagHelp).
Default("logfmt").SetValue(config.Format)
}
39 changes: 35 additions & 4 deletions promlog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,42 @@ func (l *AllowedLevel) Set(s string) error {
return nil
}

// New returns a new leveled oklog logger in the logfmt format. Each logged line will be annotated
// AllowedFormat is a settable identifier for the output format that the logger can have.
type AllowedFormat struct {
s string
}

func (f *AllowedFormat) String() string {
return f.s
}

// Set updates the value of the allowed format.
func (f *AllowedFormat) Set(s string) error {
switch s {
case "logfmt", "json":
f.s = s
default:
return errors.Errorf("unrecognized log format %q", s)
}
return nil
}

type Config struct {
Level *AllowedLevel
Format *AllowedFormat
}

// New returns a new leveled oklog logger. Each logged line will be annotated
// with a timestamp. The output always goes to stderr.
func New(al AllowedLevel) log.Logger {
l := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
l = level.NewFilter(l, al.o)
func New(config *Config) log.Logger {
var l log.Logger
if config.Format.s == "logfmt" {
l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
} else {
l = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
}

l = level.NewFilter(l, config.Level.o)
l = log.With(l, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
return l
}

0 comments on commit 41aa239

Please sign in to comment.