Skip to content
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
73 changes: 25 additions & 48 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package log

import (
"flag"
"fmt"
"io"
"io/ioutil"
Expand All @@ -26,25 +25,9 @@ import (
"strings"

"github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"
)

type levelFlag string

// String implements flag.Value.
func (f levelFlag) String() string {
return fmt.Sprintf("%q", origLogger.Level.String())
}

// Set implements flag.Value.
func (f levelFlag) Set(level string) error {
l, err := logrus.ParseLevel(level)
if err != nil {
return err
}
origLogger.Level = l
return nil
}

// setSyslogFormatter is nil if the target architecture does not support syslog.
var setSyslogFormatter func(logger, string, string) error

Expand All @@ -55,38 +38,32 @@ func setJSONFormatter() {
origLogger.Formatter = &logrus.JSONFormatter{}
}

type logFormatFlag url.URL

// String implements flag.Value.
func (f logFormatFlag) String() string {
u := url.URL(f)
return fmt.Sprintf("%q", u.String())
}

// Set implements flag.Value.
func (f logFormatFlag) Set(format string) error {
return baseLogger.SetFormat(format)
}

func init() {
AddFlags(flag.CommandLine)
type loggerSettings struct {
level string
format string
}

// AddFlags adds the flags used by this package to the given FlagSet. That's
// useful if working with a custom FlagSet. The init function of this package
// adds the flags to flag.CommandLine anyway. Thus, it's usually enough to call
// flag.Parse() to make the logging flags take effect.
func AddFlags(fs *flag.FlagSet) {
fs.Var(
levelFlag(origLogger.Level.String()),
"log.level",
"Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal]",
)
fs.Var(
logFormatFlag(url.URL{Scheme: "logger", Opaque: "stderr"}),
"log.format",
`Set the log target and format. Example: "logger:syslog?appname=bob&local=7" or "logger:stdout?json=true"`,
)
func (s *loggerSettings) apply(ctx *kingpin.ParseContext) error {
err := baseLogger.SetLevel(s.level)
if err != nil {
return err
}
err = baseLogger.SetFormat(s.format)
return err
}

// 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) {
s := loggerSettings{}
kingpin.Flag("log.level", "Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal]").
Default(origLogger.Level.String()).
StringVar(&s.level)
defaultFormat := url.URL{Scheme: "logger", Opaque: "stderr"}
kingpin.Flag("log.format", `Set the log target and format. Example: "logger:syslog?appname=bob&local=7" or "logger:stdout?json=true"`).
Default(defaultFormat.String()).
StringVar(&s.format)
a.Action(s.apply)
}

// Logger is the interface for loggers used in the Prometheus components.
Expand Down
2 changes: 1 addition & 1 deletion log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestFileLineLogging(t *testing.T) {
Debug("This debug-level line should not show up in the output.")
Infof("This %s-level line should show up in the output.", "info")

re := `^time=".*" level=info msg="This info-level line should show up in the output." source="log_test.go:33" \n$`
re := `^time=".*" level=info msg="This info-level line should show up in the output." source="log_test.go:33"\n$`
if !regexp.MustCompile(re).Match(buf.Bytes()) {
t.Fatalf("%q did not match expected regex %q", buf.String(), re)
}
Expand Down