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:
parent
4b13e4e78b
commit
34dc029b33
6 changed files with 225 additions and 36 deletions
109
internal/logx/logx_test.go
Normal file
109
internal/logx/logx_test.go
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
package logx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
||||
"github.com/ooni/probe-cli/v3/internal/testingx"
|
||||
)
|
||||
|
||||
func TestNewHandlerWithDefaultSettings(t *testing.T) {
|
||||
lh := NewHandlerWithDefaultSettings()
|
||||
if lh.Emoji {
|
||||
t.Fatal("expected false")
|
||||
}
|
||||
// Note: Go does not allow us to check whether lh.Now == time.Now
|
||||
if lh.StartTime.IsZero() {
|
||||
t.Fatal("expected non-zero time")
|
||||
}
|
||||
if lh.Writer != os.Stderr {
|
||||
t.Fatal("expected stderr")
|
||||
}
|
||||
}
|
||||
|
||||
// creates a new handler with deterministic time to help with testing
|
||||
func newHandlerForTesting() *Handler {
|
||||
lh := NewHandlerWithDefaultSettings()
|
||||
dtime := testingx.NewTimeDeterministic(time.Now())
|
||||
lh.Now = dtime.Now
|
||||
lh.StartTime = dtime.Now()
|
||||
return lh
|
||||
}
|
||||
|
||||
func TestLogHandlerHandleLog(t *testing.T) {
|
||||
type config struct {
|
||||
// name of the test
|
||||
Name string
|
||||
|
||||
// whether to use emojis
|
||||
Emoji bool
|
||||
|
||||
// the verbosity level of the log entry
|
||||
Level log.Level
|
||||
|
||||
// the string we expect for severity
|
||||
ExpectSeverity string
|
||||
}
|
||||
|
||||
configs := []config{{
|
||||
Name: "debug level without emoji",
|
||||
Emoji: false,
|
||||
Level: log.DebugLevel,
|
||||
ExpectSeverity: "<debug>",
|
||||
}, {
|
||||
Name: "info level without emoji",
|
||||
Emoji: false,
|
||||
Level: log.InfoLevel,
|
||||
ExpectSeverity: "<info>",
|
||||
}, {
|
||||
Name: "warn level without emoji",
|
||||
Emoji: false,
|
||||
Level: log.WarnLevel,
|
||||
ExpectSeverity: "<warn>",
|
||||
}, {
|
||||
Name: "debug level with emoji",
|
||||
Emoji: true,
|
||||
Level: log.DebugLevel,
|
||||
ExpectSeverity: "🧐",
|
||||
}, {
|
||||
Name: "info level with emoji",
|
||||
Emoji: true,
|
||||
Level: log.InfoLevel,
|
||||
ExpectSeverity: "🗒️",
|
||||
}, {
|
||||
Name: "warn level with emoji",
|
||||
Emoji: true,
|
||||
Level: log.WarnLevel,
|
||||
ExpectSeverity: "🔥",
|
||||
}}
|
||||
|
||||
for _, cnf := range configs {
|
||||
t.Run(cnf.Name, func(t *testing.T) {
|
||||
expected := fmt.Sprintf("[ 1.000000] %s antani: map[error:EOF]\n", cnf.ExpectSeverity)
|
||||
var got string
|
||||
lh := newHandlerForTesting()
|
||||
lh.Emoji = cnf.Emoji
|
||||
lh.Writer = &mocks.Writer{
|
||||
MockWrite: func(b []byte) (int, error) {
|
||||
got = string(b)
|
||||
return len(b), nil
|
||||
},
|
||||
}
|
||||
lh.HandleLog(&log.Entry{
|
||||
Fields: map[string]any{
|
||||
"error": "EOF",
|
||||
},
|
||||
Level: cnf.Level,
|
||||
Message: "antani",
|
||||
})
|
||||
if diff := cmp.Diff(expected, got); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue