Implement handlers to normalise how logging is handled

This commit is contained in:
Arturo Filastò
2018-02-21 17:06:30 +02:00
parent e7ee54436e
commit 3cdb927eb0
14 changed files with 176 additions and 30 deletions
-2
View File
@@ -5,12 +5,10 @@ import (
"github.com/openobservatory/gooni/internal/cli/root"
"github.com/openobservatory/gooni/internal/cli/version"
"github.com/openobservatory/gooni/internal/util"
)
// Run the app. This is the main app entry point
func Run() error {
util.Log("Running")
root.Cmd.Version(version.Version)
_, err := root.Cmd.Parse(os.Args[1:])
return err
+2 -2
View File
@@ -2,15 +2,15 @@ package info
import (
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/openobservatory/gooni/internal/cli/root"
"github.com/openobservatory/gooni/internal/util"
)
func init() {
cmd := root.Command("info", "Display information about OONI Probe")
cmd.Action(func(_ *kingpin.ParseContext) error {
util.Log("Info")
log.Info("Info")
return nil
})
}
+2 -2
View File
@@ -2,15 +2,15 @@ package list
import (
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/openobservatory/gooni/internal/cli/root"
"github.com/openobservatory/gooni/internal/util"
)
func init() {
cmd := root.Command("list", "List measurements")
cmd.Action(func(_ *kingpin.ParseContext) error {
util.Log("Listing")
log.Info("Listing")
return nil
})
}
+2 -2
View File
@@ -2,15 +2,15 @@ package nettest
import (
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/openobservatory/gooni/internal/cli/root"
"github.com/openobservatory/gooni/internal/util"
)
func init() {
cmd := root.Command("nettest", "Run a specific nettest")
cmd.Action(func(_ *kingpin.ParseContext) error {
util.Log("Nettest")
log.Info("Nettest")
return nil
})
}
+11 -4
View File
@@ -3,9 +3,10 @@ package root
import (
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
ooni "github.com/openobservatory/gooni"
"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"
)
@@ -20,11 +21,17 @@ var Init func() (*ooni.Config, *ooni.Context, error)
func init() {
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 {
log.SetHandler(cli.Default)
if *verbose {
if *isBatch {
log.SetHandler(batch.Default)
} else {
log.SetHandler(cli.Default)
}
if *isVerbose {
log.SetLevel(log.DebugLevel)
log.Debugf("ooni version %s", version.Version)
}
+2 -3
View File
@@ -7,7 +7,6 @@ import (
"github.com/apex/log"
"github.com/openobservatory/gooni/internal/cli/root"
"github.com/openobservatory/gooni/internal/database"
"github.com/openobservatory/gooni/internal/util"
"github.com/openobservatory/gooni/nettests"
"github.com/openobservatory/gooni/nettests/groups"
)
@@ -18,7 +17,7 @@ func init() {
nettestGroup := cmd.Arg("name", "the nettest group to run").Required().String()
cmd.Action(func(_ *kingpin.ParseContext) error {
util.Log("Starting %s", *nettestGroup)
log.Infof("Starting %s", *nettestGroup)
_, ctx, err := root.Init()
if err != nil {
log.Errorf("%s", err)
@@ -37,7 +36,7 @@ func init() {
}
for _, nt := range group.Nettests {
ctl := nettests.NewController(ctx)
ctl := nettests.NewController(ctx, result)
nt.Run(ctl)
// XXX
// 1. Generate the summary
+2 -2
View File
@@ -2,15 +2,15 @@ package nettest
import (
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/openobservatory/gooni/internal/cli/root"
"github.com/openobservatory/gooni/internal/util"
)
func init() {
cmd := root.Command("show", "Show a specific measurement")
cmd.Action(func(_ *kingpin.ParseContext) error {
util.Log("Show")
log.Info("Show")
return nil
})
}
+2 -2
View File
@@ -2,15 +2,15 @@ package upload
import (
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/openobservatory/gooni/internal/cli/root"
"github.com/openobservatory/gooni/internal/util"
)
func init() {
cmd := root.Command("upload", "Upload a specific measurement")
cmd.Action(func(_ *kingpin.ParseContext) error {
util.Log("Uploading")
log.Info("Uploading")
return nil
})
}
+2 -2
View File
@@ -37,9 +37,9 @@ func CreateMeasurement(db *sqlx.DB, m Measurement) (*Measurement, error) {
report_id, input, measurement_id,
result_id)
VALUES (:name,:start_time,
:summary,:asn,:ip,:country,
:asn,:ip,:country,
:state,:failure,:report_file,
:report_id,:input,:measurement_id,
:report_id,:input,
:result_id)`,
m)
if err != nil {
+33
View 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
View 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
}