Skip to content
Prev Previous commit
Next Next commit
Quote more aggressively
  • Loading branch information
ammario committed May 8, 2023
commit e2270d803ab4f936c0e2914b117ae2214e1618ec
27 changes: 8 additions & 19 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"
"time"

"go.opencensus.io/trace"
"golang.org/x/xerrors"

"cdr.dev/slog"
Expand Down Expand Up @@ -72,23 +71,13 @@ func Example_testing() {
slog.F("field_name", "something or the other"),
)

// t.go:55: 2019-12-05 21:20:31.218 [INFO] <examples_test.go:42> my message here {"field_name": "something or the other"}
}

func Example_tracing() {
log := slog.Make(sloghuman.Sink(os.Stdout))

ctx, _ := trace.StartSpan(context.Background(), "spanName")

log.Info(ctx, "my msg", slog.F("hello", "hi"))

// 2019-12-09 21:59:48.110 [INFO] <example_test.go:62> my msg {"trace": "f143d018d00de835688453d8dc55c9fd", "span": "f214167bf550afc3", "hello": "hi"}
// t.go:55: 2019-12-05 21:20:31.218 [INFO] my message here field_name="something or the other"
}

func Example_multiple() {
l := slog.Make(sloghuman.Sink(os.Stdout))

f, err := os.OpenFile("stackdriver", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
f, err := os.OpenFile("stackdriver", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o644)
if err != nil {
l.Fatal(context.Background(), "failed to open stackdriver log file", slog.Error(err))
}
Expand All @@ -97,7 +86,7 @@ func Example_multiple() {

l.Info(context.Background(), "log to stdout and stackdriver")

// 2019-12-07 20:59:55.790 [INFO] <example_test.go:46> log to stdout and stackdriver
// 2019-12-07 20:59:55.790 [INFO] log to stdout and stackdriver
}

func ExampleWith() {
Expand All @@ -106,7 +95,7 @@ func ExampleWith() {
l := slog.Make(sloghuman.Sink(os.Stdout))
l.Info(ctx, "msg")

// 2019-12-07 20:54:23.986 [INFO] <example_test.go:20> msg {"field": 1}
// 2019-12-07 20:54:23.986 [INFO] msg field=1}
}

func ExampleStdlib() {
Expand All @@ -115,7 +104,7 @@ func ExampleStdlib() {

l.Print("msg")

// 2019-12-07 20:54:23.986 [INFO] (stdlib) <example_test.go:29> msg {"field": 1}
// 2019-12-07 20:54:23.986 [INFO] (stdlib) msg field=1
}

func ExampleLogger_Named() {
Expand All @@ -125,7 +114,7 @@ func ExampleLogger_Named() {
l = l.Named("http")
l.Info(ctx, "received request", slog.F("remote address", net.IPv4(127, 0, 0, 1)))

// 2019-12-07 21:20:56.974 [INFO] (http) <example_test.go:85> received request {"remote address": "127.0.0.1"}
// 2019-12-07 21:20:56.974 [INFO] (http) received request remote_address=127.0.0.1}
}

func ExampleLogger_Leveled() {
Expand All @@ -139,6 +128,6 @@ func ExampleLogger_Leveled() {

l.Debug(ctx, "testing2")

// 2019-12-07 21:26:20.945 [INFO] <example_test.go:95> received request
// 2019-12-07 21:26:20.945 [DEBUG] <example_test.go:99> testing2
// 2019-12-07 21:26:20.945 [INFO] received request
// 2019-12-07 21:26:20.945 [DEBU] testing2
}
14 changes: 11 additions & 3 deletions internal/entryhuman/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"time"
"unicode"

"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
Expand Down Expand Up @@ -120,7 +121,7 @@ func Fmt(buf io.StringWriter, termW io.Writer, ent slog.SinkEntry,
if i < len(ent.Fields) {
buf.WriteString("\t")
}
buf.WriteString(render(termW, keyStyle, f.Name+"="))
buf.WriteString(render(termW, keyStyle, quoteKey(f.Name)+"="))
valueStr := fmt.Sprintf("%+v", f.Value)
buf.WriteString(quote(valueStr))
}
Expand Down Expand Up @@ -195,17 +196,24 @@ func quote(key string) string {
return `""`
}

var hasSpace bool
for _, r := range key {
if unicode.IsSpace(r) {
hasSpace = true
break
}
}
quoted := strconv.Quote(key)
// If the key doesn't need to be quoted, don't quote it.
// We do not use strconv.CanBackquote because it doesn't
// account tabs.
if quoted[1:len(quoted)-1] == key {
if !hasSpace && quoted[1:len(quoted)-1] == key {
return key
}
return quoted
}

func quoteKey(key string) string {
// Replace spaces in the map keys with underscores.
return strings.ReplaceAll(key, " ", "_")
return quote(strings.ReplaceAll(key, " ", "_"))
}
9 changes: 9 additions & 0 deletions internal/entryhuman/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ func TestEntry(t *testing.T) {
),
},
},
{
"spacey",
slog.SinkEntry{
Level: slog.LevelWarn,
Fields: slog.M(
slog.F("space in my key", "value in my value"),
),
},
},
}
if *updateGoldenFiles {
ents, err := os.ReadDir("testdata")
Expand Down
1 change: 1 addition & 0 deletions internal/entryhuman/testdata/spacey.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0001-01-01 00:00:00.000 [WARN] space_in_my_key="value in my value"