Implement handlers to normalise how logging is handled
This commit is contained in:
parent
e7ee54436e
commit
3cdb927eb0
11
Gopkg.lock
generated
11
Gopkg.lock
generated
|
@ -26,7 +26,8 @@
|
||||||
name = "github.com/apex/log"
|
name = "github.com/apex/log"
|
||||||
packages = [
|
packages = [
|
||||||
".",
|
".",
|
||||||
"handlers/cli"
|
"handlers/cli",
|
||||||
|
"handlers/json"
|
||||||
]
|
]
|
||||||
revision = "0296d6eb16bb28f8a0c55668affcf4876dc269be"
|
revision = "0296d6eb16bb28f8a0c55668affcf4876dc269be"
|
||||||
version = "v1.0.0"
|
version = "v1.0.0"
|
||||||
|
@ -43,6 +44,12 @@
|
||||||
packages = ["quantile"]
|
packages = ["quantile"]
|
||||||
revision = "4c0e84591b9aa9e6dcfdf3e020114cd81f89d5f9"
|
revision = "4c0e84591b9aa9e6dcfdf3e020114cd81f89d5f9"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
name = "github.com/fatih/color"
|
||||||
|
packages = ["."]
|
||||||
|
revision = "507f6050b8568533fb3f5504de8e5205fa62a114"
|
||||||
|
version = "v1.6.0"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
name = "github.com/golang/protobuf"
|
name = "github.com/golang/protobuf"
|
||||||
packages = ["proto"]
|
packages = ["proto"]
|
||||||
|
@ -174,6 +181,6 @@
|
||||||
[solve-meta]
|
[solve-meta]
|
||||||
analyzer-name = "dep"
|
analyzer-name = "dep"
|
||||||
analyzer-version = 1
|
analyzer-version = 1
|
||||||
inputs-digest = "5405ce55dbd69df4847de599c4e30d7540f208db389b67f2c9789ef6c17351d0"
|
inputs-digest = "6b04c53f65567785f5fc4ea3563d11c7744c450d1a0c45c0b8047ad10bf766ea"
|
||||||
solver-name = "gps-cdcl"
|
solver-name = "gps-cdcl"
|
||||||
solver-version = 1
|
solver-version = 1
|
||||||
|
|
|
@ -2,6 +2,8 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// commands
|
// commands
|
||||||
|
"github.com/apex/log"
|
||||||
|
|
||||||
_ "github.com/openobservatory/gooni/internal/cli/info"
|
_ "github.com/openobservatory/gooni/internal/cli/info"
|
||||||
_ "github.com/openobservatory/gooni/internal/cli/list"
|
_ "github.com/openobservatory/gooni/internal/cli/list"
|
||||||
_ "github.com/openobservatory/gooni/internal/cli/nettest"
|
_ "github.com/openobservatory/gooni/internal/cli/nettest"
|
||||||
|
@ -9,7 +11,6 @@ import (
|
||||||
_ "github.com/openobservatory/gooni/internal/cli/show"
|
_ "github.com/openobservatory/gooni/internal/cli/show"
|
||||||
_ "github.com/openobservatory/gooni/internal/cli/upload"
|
_ "github.com/openobservatory/gooni/internal/cli/upload"
|
||||||
_ "github.com/openobservatory/gooni/internal/cli/version"
|
_ "github.com/openobservatory/gooni/internal/cli/version"
|
||||||
"github.com/openobservatory/gooni/internal/util"
|
|
||||||
|
|
||||||
"github.com/openobservatory/gooni/internal/cli/app"
|
"github.com/openobservatory/gooni/internal/cli/app"
|
||||||
)
|
)
|
||||||
|
@ -19,5 +20,5 @@ func main() {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
util.Fatal(err)
|
log.WithError(err).Fatal("main exit")
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,12 +5,10 @@ import (
|
||||||
|
|
||||||
"github.com/openobservatory/gooni/internal/cli/root"
|
"github.com/openobservatory/gooni/internal/cli/root"
|
||||||
"github.com/openobservatory/gooni/internal/cli/version"
|
"github.com/openobservatory/gooni/internal/cli/version"
|
||||||
"github.com/openobservatory/gooni/internal/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Run the app. This is the main app entry point
|
// Run the app. This is the main app entry point
|
||||||
func Run() error {
|
func Run() error {
|
||||||
util.Log("Running")
|
|
||||||
root.Cmd.Version(version.Version)
|
root.Cmd.Version(version.Version)
|
||||||
_, err := root.Cmd.Parse(os.Args[1:])
|
_, err := root.Cmd.Parse(os.Args[1:])
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -2,15 +2,15 @@ package info
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/alecthomas/kingpin"
|
"github.com/alecthomas/kingpin"
|
||||||
|
"github.com/apex/log"
|
||||||
"github.com/openobservatory/gooni/internal/cli/root"
|
"github.com/openobservatory/gooni/internal/cli/root"
|
||||||
"github.com/openobservatory/gooni/internal/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmd := root.Command("info", "Display information about OONI Probe")
|
cmd := root.Command("info", "Display information about OONI Probe")
|
||||||
|
|
||||||
cmd.Action(func(_ *kingpin.ParseContext) error {
|
cmd.Action(func(_ *kingpin.ParseContext) error {
|
||||||
util.Log("Info")
|
log.Info("Info")
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,15 +2,15 @@ package list
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/alecthomas/kingpin"
|
"github.com/alecthomas/kingpin"
|
||||||
|
"github.com/apex/log"
|
||||||
"github.com/openobservatory/gooni/internal/cli/root"
|
"github.com/openobservatory/gooni/internal/cli/root"
|
||||||
"github.com/openobservatory/gooni/internal/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmd := root.Command("list", "List measurements")
|
cmd := root.Command("list", "List measurements")
|
||||||
|
|
||||||
cmd.Action(func(_ *kingpin.ParseContext) error {
|
cmd.Action(func(_ *kingpin.ParseContext) error {
|
||||||
util.Log("Listing")
|
log.Info("Listing")
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,15 +2,15 @@ package nettest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/alecthomas/kingpin"
|
"github.com/alecthomas/kingpin"
|
||||||
|
"github.com/apex/log"
|
||||||
"github.com/openobservatory/gooni/internal/cli/root"
|
"github.com/openobservatory/gooni/internal/cli/root"
|
||||||
"github.com/openobservatory/gooni/internal/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmd := root.Command("nettest", "Run a specific nettest")
|
cmd := root.Command("nettest", "Run a specific nettest")
|
||||||
|
|
||||||
cmd.Action(func(_ *kingpin.ParseContext) error {
|
cmd.Action(func(_ *kingpin.ParseContext) error {
|
||||||
util.Log("Nettest")
|
log.Info("Nettest")
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,10 @@ package root
|
||||||
import (
|
import (
|
||||||
"github.com/alecthomas/kingpin"
|
"github.com/alecthomas/kingpin"
|
||||||
"github.com/apex/log"
|
"github.com/apex/log"
|
||||||
"github.com/apex/log/handlers/cli"
|
|
||||||
ooni "github.com/openobservatory/gooni"
|
ooni "github.com/openobservatory/gooni"
|
||||||
"github.com/openobservatory/gooni/internal/database"
|
"github.com/openobservatory/gooni/internal/database"
|
||||||
|
"github.com/openobservatory/gooni/internal/log/handlers/batch"
|
||||||
|
"github.com/openobservatory/gooni/internal/log/handlers/cli"
|
||||||
"github.com/prometheus/common/version"
|
"github.com/prometheus/common/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,11 +21,17 @@ var Init func() (*ooni.Config, *ooni.Context, error)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
configPath := Cmd.Flag("config", "Set a custom config file path").Short('c').String()
|
configPath := Cmd.Flag("config", "Set a custom config file path").Short('c').String()
|
||||||
verbose := Cmd.Flag("verbose", "Enable verbose log output.").Short('v').Bool()
|
|
||||||
|
isVerbose := Cmd.Flag("verbose", "Enable verbose log output.").Short('v').Bool()
|
||||||
|
isBatch := Cmd.Flag("batch", "Enable batch command line usage.").Bool()
|
||||||
|
|
||||||
Cmd.PreAction(func(ctx *kingpin.ParseContext) error {
|
Cmd.PreAction(func(ctx *kingpin.ParseContext) error {
|
||||||
|
if *isBatch {
|
||||||
|
log.SetHandler(batch.Default)
|
||||||
|
} else {
|
||||||
log.SetHandler(cli.Default)
|
log.SetHandler(cli.Default)
|
||||||
if *verbose {
|
}
|
||||||
|
if *isVerbose {
|
||||||
log.SetLevel(log.DebugLevel)
|
log.SetLevel(log.DebugLevel)
|
||||||
log.Debugf("ooni version %s", version.Version)
|
log.Debugf("ooni version %s", version.Version)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"github.com/apex/log"
|
"github.com/apex/log"
|
||||||
"github.com/openobservatory/gooni/internal/cli/root"
|
"github.com/openobservatory/gooni/internal/cli/root"
|
||||||
"github.com/openobservatory/gooni/internal/database"
|
"github.com/openobservatory/gooni/internal/database"
|
||||||
"github.com/openobservatory/gooni/internal/util"
|
|
||||||
"github.com/openobservatory/gooni/nettests"
|
"github.com/openobservatory/gooni/nettests"
|
||||||
"github.com/openobservatory/gooni/nettests/groups"
|
"github.com/openobservatory/gooni/nettests/groups"
|
||||||
)
|
)
|
||||||
|
@ -18,7 +17,7 @@ func init() {
|
||||||
nettestGroup := cmd.Arg("name", "the nettest group to run").Required().String()
|
nettestGroup := cmd.Arg("name", "the nettest group to run").Required().String()
|
||||||
|
|
||||||
cmd.Action(func(_ *kingpin.ParseContext) error {
|
cmd.Action(func(_ *kingpin.ParseContext) error {
|
||||||
util.Log("Starting %s", *nettestGroup)
|
log.Infof("Starting %s", *nettestGroup)
|
||||||
_, ctx, err := root.Init()
|
_, ctx, err := root.Init()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("%s", err)
|
log.Errorf("%s", err)
|
||||||
|
@ -37,7 +36,7 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, nt := range group.Nettests {
|
for _, nt := range group.Nettests {
|
||||||
ctl := nettests.NewController(ctx)
|
ctl := nettests.NewController(ctx, result)
|
||||||
nt.Run(ctl)
|
nt.Run(ctl)
|
||||||
// XXX
|
// XXX
|
||||||
// 1. Generate the summary
|
// 1. Generate the summary
|
||||||
|
|
|
@ -2,15 +2,15 @@ package nettest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/alecthomas/kingpin"
|
"github.com/alecthomas/kingpin"
|
||||||
|
"github.com/apex/log"
|
||||||
"github.com/openobservatory/gooni/internal/cli/root"
|
"github.com/openobservatory/gooni/internal/cli/root"
|
||||||
"github.com/openobservatory/gooni/internal/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmd := root.Command("show", "Show a specific measurement")
|
cmd := root.Command("show", "Show a specific measurement")
|
||||||
|
|
||||||
cmd.Action(func(_ *kingpin.ParseContext) error {
|
cmd.Action(func(_ *kingpin.ParseContext) error {
|
||||||
util.Log("Show")
|
log.Info("Show")
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,15 +2,15 @@ package upload
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/alecthomas/kingpin"
|
"github.com/alecthomas/kingpin"
|
||||||
|
"github.com/apex/log"
|
||||||
"github.com/openobservatory/gooni/internal/cli/root"
|
"github.com/openobservatory/gooni/internal/cli/root"
|
||||||
"github.com/openobservatory/gooni/internal/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmd := root.Command("upload", "Upload a specific measurement")
|
cmd := root.Command("upload", "Upload a specific measurement")
|
||||||
|
|
||||||
cmd.Action(func(_ *kingpin.ParseContext) error {
|
cmd.Action(func(_ *kingpin.ParseContext) error {
|
||||||
util.Log("Uploading")
|
log.Info("Uploading")
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,9 +37,9 @@ func CreateMeasurement(db *sqlx.DB, m Measurement) (*Measurement, error) {
|
||||||
report_id, input, measurement_id,
|
report_id, input, measurement_id,
|
||||||
result_id)
|
result_id)
|
||||||
VALUES (:name,:start_time,
|
VALUES (:name,:start_time,
|
||||||
:summary,:asn,:ip,:country,
|
:asn,:ip,:country,
|
||||||
:state,:failure,:report_file,
|
:state,:failure,:report_file,
|
||||||
:report_id,:input,:measurement_id,
|
:report_id,:input,
|
||||||
:result_id)`,
|
:result_id)`,
|
||||||
m)
|
m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
33
internal/log/handlers/batch/batch.go
Normal file
33
internal/log/handlers/batch/batch.go
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
package batch
|
||||||
|
|
||||||
|
import (
|
||||||
|
j "encoding/json"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/apex/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Default handler outputting to stderr.
|
||||||
|
var Default = New(os.Stderr)
|
||||||
|
|
||||||
|
// Handler implementation.
|
||||||
|
type Handler struct {
|
||||||
|
*j.Encoder
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// New handler.
|
||||||
|
func New(w io.Writer) *Handler {
|
||||||
|
return &Handler{
|
||||||
|
Encoder: j.NewEncoder(w),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleLog implements log.Handler.
|
||||||
|
func (h *Handler) HandleLog(e *log.Entry) error {
|
||||||
|
h.mu.Lock()
|
||||||
|
defer h.mu.Unlock()
|
||||||
|
return h.Encoder.Encode(e)
|
||||||
|
}
|
84
internal/log/handlers/cli/cli.go
Normal file
84
internal/log/handlers/cli/cli.go
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/apex/log"
|
||||||
|
"github.com/fatih/color"
|
||||||
|
colorable "github.com/mattn/go-colorable"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Default handler outputting to stderr.
|
||||||
|
var Default = New(os.Stderr)
|
||||||
|
|
||||||
|
// start time.
|
||||||
|
var start = time.Now()
|
||||||
|
|
||||||
|
var bold = color.New(color.Bold)
|
||||||
|
|
||||||
|
// Colors mapping.
|
||||||
|
var Colors = [...]*color.Color{
|
||||||
|
log.DebugLevel: color.New(color.FgWhite),
|
||||||
|
log.InfoLevel: color.New(color.FgBlue),
|
||||||
|
log.WarnLevel: color.New(color.FgYellow),
|
||||||
|
log.ErrorLevel: color.New(color.FgRed),
|
||||||
|
log.FatalLevel: color.New(color.FgRed),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strings mapping.
|
||||||
|
var Strings = [...]string{
|
||||||
|
log.DebugLevel: "•",
|
||||||
|
log.InfoLevel: "•",
|
||||||
|
log.WarnLevel: "•",
|
||||||
|
log.ErrorLevel: "⨯",
|
||||||
|
log.FatalLevel: "⨯",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler implementation.
|
||||||
|
type Handler struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
Writer io.Writer
|
||||||
|
Padding int
|
||||||
|
}
|
||||||
|
|
||||||
|
// New handler.
|
||||||
|
func New(w io.Writer) *Handler {
|
||||||
|
if f, ok := w.(*os.File); ok {
|
||||||
|
return &Handler{
|
||||||
|
Writer: colorable.NewColorable(f),
|
||||||
|
Padding: 3,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Handler{
|
||||||
|
Writer: w,
|
||||||
|
Padding: 3,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleLog implements log.Handler.
|
||||||
|
func (h *Handler) HandleLog(e *log.Entry) error {
|
||||||
|
color := Colors[e.Level]
|
||||||
|
level := Strings[e.Level]
|
||||||
|
names := e.Fields.Names()
|
||||||
|
|
||||||
|
h.mu.Lock()
|
||||||
|
defer h.mu.Unlock()
|
||||||
|
|
||||||
|
color.Fprintf(h.Writer, "%s %-25s", bold.Sprintf("%*s", h.Padding+1, level), e.Message)
|
||||||
|
|
||||||
|
for _, name := range names {
|
||||||
|
if name == "source" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fmt.Fprintf(h.Writer, " %s=%s", color.Sprint(name), e.Fields.Get(name))
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintln(h.Writer)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -1,12 +1,11 @@
|
||||||
package nettests
|
package nettests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/apex/log"
|
"github.com/apex/log"
|
||||||
"github.com/measurement-kit/go-measurement-kit"
|
"github.com/measurement-kit/go-measurement-kit"
|
||||||
ooni "github.com/openobservatory/gooni"
|
ooni "github.com/openobservatory/gooni"
|
||||||
"github.com/openobservatory/gooni/internal/cli/version"
|
"github.com/openobservatory/gooni/internal/cli/version"
|
||||||
|
"github.com/openobservatory/gooni/internal/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Nettest interface. Every Nettest should implement this.
|
// Nettest interface. Every Nettest should implement this.
|
||||||
|
@ -24,15 +23,17 @@ type NettestGroup struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewController creates a nettest controller
|
// NewController creates a nettest controller
|
||||||
func NewController(ctx *ooni.Context) *Controller {
|
func NewController(ctx *ooni.Context, res *database.Result) *Controller {
|
||||||
return &Controller{
|
return &Controller{
|
||||||
ctx,
|
ctx,
|
||||||
|
res,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Controller is passed to the run method of every Nettest
|
// Controller is passed to the run method of every Nettest
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
ctx *ooni.Context
|
ctx *ooni.Context
|
||||||
|
res *database.Result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init should be called once to initialise the nettest
|
// Init should be called once to initialise the nettest
|
||||||
|
@ -53,7 +54,23 @@ func (c *Controller) Init(nt *mk.Nettest) {
|
||||||
CaBundlePath: "",
|
CaBundlePath: "",
|
||||||
}
|
}
|
||||||
nt.RegisterEventHandler(func(event interface{}) {
|
nt.RegisterEventHandler(func(event interface{}) {
|
||||||
fmt.Println("Got event", event)
|
e := event.(map[string]interface{})
|
||||||
|
if e["type"].(string) == "LOG" {
|
||||||
|
msg := e["message"].(string)
|
||||||
|
switch level := e["verbosity"].(string); level {
|
||||||
|
case "ERROR":
|
||||||
|
log.Error(msg)
|
||||||
|
case "INFO":
|
||||||
|
log.Info(msg)
|
||||||
|
default:
|
||||||
|
log.Debug(msg)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"key": "event",
|
||||||
|
"value": e,
|
||||||
|
}).Info("got event")
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,7 +87,7 @@ func (c *Controller) OnEntry(entry string) {
|
||||||
// MKStart is the interface for the mk.Nettest Start() function
|
// MKStart is the interface for the mk.Nettest Start() function
|
||||||
type MKStart func(name string) (chan bool, error)
|
type MKStart func(name string) (chan bool, error)
|
||||||
|
|
||||||
// Start should be called every time there is a new entry
|
// Start should be called to start the test
|
||||||
func (c *Controller) Start(f MKStart) {
|
func (c *Controller) Start(f MKStart) {
|
||||||
log.Debugf("MKStart: %s", f)
|
log.Debugf("MKStart: %s", f)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user