Fix logging flags (#201)
authorCalle Pettersson <carlpett@users.noreply.github.com>
Mon, 31 Jul 2017 19:49:52 +0000 (21:49 +0200)
committerBrian Brazil <brian.brazil@robustperception.io>
Mon, 31 Jul 2017 19:49:52 +0000 (20:49 +0100)
Update with new common/log

main.go
vendor/github.com/prometheus/common/log/log.go
vendor/vendor.json

diff --git a/main.go b/main.go
index e3cce95fb4ae0c145d8fd799ffd92f2b75a24ba9..56682cfca5b51f37fb13be95be9abd1157c4303e 100644 (file)
--- a/main.go
+++ b/main.go
@@ -143,6 +143,7 @@ func init() {
 }
 
 func main() {
+       log.AddFlags(kingpin.CommandLine)
        kingpin.Version(version.Print("blackbox_exporter"))
        kingpin.HelpFlag.Short('h')
        kingpin.Parse()
index 1321741ad3e0fa462780637a6884d4c5acd27dfe..04e906c077bb1bdfd7d183b222bd017a951b63e0 100644 (file)
@@ -14,7 +14,6 @@
 package log
 
 import (
-       "flag"
        "fmt"
        "io"
        "io/ioutil"
@@ -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
 
@@ -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.
index 01784b9597aa5946199e51056095b7f29c077ec3..1265fbe3340a7ab485b74c10c8e07dce5998d627 100644 (file)
                        "revisionTime": "2017-06-21T12:10:01Z"
                },
                {
-                       "checksumSHA1": "esljoY35OOHttvpjBctA6WGZc70=",
+                       "checksumSHA1": "jYpLEs+wZ5LZubvOJEDSQ8I14MI=",
                        "path": "github.com/prometheus/common/log",
-                       "revision": "0866df4b85a18d652b6965be022d007cdf076822",
-                       "revisionTime": "2017-06-21T12:10:01Z"
+                       "revision": "8ba51016a21456f1649877d7079f416d69eb3948",
+                       "revisionTime": "2017-07-31T09:30:31Z"
                },
                {
                        "checksumSHA1": "3VoqH7TFfzA6Ds0zFzIbKCUvBmw=",