Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

promlog: add support for json logger #136

Merged
merged 1 commit into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}