feat(miniooni): optionally log using emojis (#932)
As silly as it seems, emojis help _a lot_ when eyeballing logs to quickly identify unexpected lines. I'm doing this work as part of https://github.com/ooni/probe/issues/2257
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
package main
|
||||
|
||||
//
|
||||
// Logging functionality
|
||||
//
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/apex/log"
|
||||
)
|
||||
|
||||
// logStartTime is the time when we started logging
|
||||
var logStartTime = time.Now()
|
||||
|
||||
// logHandler implements the log handler required by github.com/apex/log
|
||||
type logHandler struct {
|
||||
// Writer is the underlying writer
|
||||
io.Writer
|
||||
}
|
||||
|
||||
var _ log.Handler = &logHandler{}
|
||||
|
||||
// HandleLog implements log.Handler
|
||||
func (h *logHandler) HandleLog(e *log.Entry) (err error) {
|
||||
s := fmt.Sprintf("[%14.6f] <%s> %s", time.Since(logStartTime).Seconds(), e.Level, e.Message)
|
||||
if len(e.Fields) > 0 {
|
||||
s += fmt.Sprintf(": %+v", e.Fields)
|
||||
}
|
||||
s += "\n"
|
||||
_, err = h.Writer.Write([]byte(s))
|
||||
return
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/ooni/probe-cli/v3/internal/engine"
|
||||
"github.com/ooni/probe-cli/v3/internal/humanize"
|
||||
"github.com/ooni/probe-cli/v3/internal/legacy/assetsdir"
|
||||
"github.com/ooni/probe-cli/v3/internal/logx"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/registry"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
@@ -25,6 +26,7 @@ import (
|
||||
// Options contains the options you can set from the CLI.
|
||||
type Options struct {
|
||||
Annotations []string
|
||||
Emoji bool
|
||||
ExtraOptions []string
|
||||
HomeDir string
|
||||
Inputs []string
|
||||
@@ -64,6 +66,13 @@ func main() {
|
||||
"add KEY=VALUE annotation to the report (can be repeated multiple times)",
|
||||
)
|
||||
|
||||
flags.BoolVar(
|
||||
&globalOptions.Emoji,
|
||||
"emoji",
|
||||
false,
|
||||
"whether to use emojis when logging",
|
||||
)
|
||||
|
||||
flags.StringVar(
|
||||
&globalOptions.HomeDir,
|
||||
"home",
|
||||
@@ -266,7 +275,9 @@ func MainWithConfiguration(experimentName string, currentOptions *Options) {
|
||||
currentOptions.Proxy = fmt.Sprintf("%s:///", currentOptions.Tunnel)
|
||||
}
|
||||
|
||||
logger := &log.Logger{Level: log.InfoLevel, Handler: &logHandler{Writer: os.Stderr}}
|
||||
logHandler := logx.NewHandlerWithDefaultSettings()
|
||||
logHandler.Emoji = currentOptions.Emoji
|
||||
logger := &log.Logger{Level: log.InfoLevel, Handler: logHandler}
|
||||
if currentOptions.Verbose {
|
||||
logger.Level = log.DebugLevel
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user