Skip to content

Commit ef50d40

Browse files
committed
Refactor structure
1 parent dd1f483 commit ef50d40

File tree

11 files changed

+33
-28
lines changed

11 files changed

+33
-28
lines changed

internal/slogfmt/humanfmt.go renamed to internal/entryhuman/entry.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// Package slogfmt contains the code to format slog.SinkEntry.
2-
package slogfmt
1+
// Package entryhuman contains the code to format slog.SinkEntry
2+
// for humans.
3+
package entryhuman
34

45
import (
56
"bytes"
@@ -8,7 +9,6 @@ import (
89
"io"
910
"os"
1011
"path/filepath"
11-
"regexp"
1212
"strconv"
1313
"strings"
1414
"time"
@@ -21,11 +21,6 @@ import (
2121
"cdr.dev/slog"
2222
)
2323

24-
// FilterJSONField filters the json field f from j.
25-
func FilterJSONField(j, f string) string {
26-
return regexp.MustCompile(`"`+f+`":[^,]+,`).ReplaceAllString(j, "")
27-
}
28-
2924
// StripTimestamp strips the timestamp from entry and returns
3025
// it and the rest of the entry.
3126
func StripTimestamp(ent string) (time.Time, string, error) {
@@ -47,14 +42,14 @@ func c(w io.Writer, attrs ...color.Attribute) *color.Color {
4742
return c
4843
}
4944

50-
// HumanEntry returns a human readable format for ent.
45+
// Fmt returns a human readable format for ent.
5146
//
5247
// We never return with a trailing newline because Go's testing framework adds one
5348
// automatically and if we include one, then we'll get two newlines.
5449
// We also do not indent the fields as go's test does that automatically
5550
// for extra lines in a log so if we did it here, the fields would be indented
5651
// twice in test logs. So the Stderr logger indents all the fields itself.
57-
func HumanEntry(w io.Writer, ent slog.SinkEntry) string {
52+
func Fmt(w io.Writer, ent slog.SinkEntry) string {
5853
var ents string
5954
ts := ent.Time.Format(TimeFormat)
6055
ents += ts + " "

internal/slogfmt/humanfmt_test.go renamed to internal/entryhuman/entry_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package slogfmt_test
1+
package entryhuman_test
22

33
import (
44
"io/ioutil"
@@ -9,7 +9,7 @@ import (
99

1010
"cdr.dev/slog"
1111
"cdr.dev/slog/internal/assert"
12-
"cdr.dev/slog/internal/slogfmt"
12+
"cdr.dev/slog/internal/entryhuman"
1313
)
1414

1515
var kt = time.Date(2000, time.February, 5, 4, 4, 4, 4, time.UTC)
@@ -18,7 +18,7 @@ func TestEntry(t *testing.T) {
1818
t.Parallel()
1919

2020
test := func(t *testing.T, in slog.SinkEntry, exp string) {
21-
act := slogfmt.HumanEntry(ioutil.Discard, in)
21+
act := entryhuman.Fmt(ioutil.Discard, in)
2222
assert.Equal(t, exp, act, "entry")
2323
}
2424

@@ -71,7 +71,7 @@ line2`)
7171
t.Run("color", func(t *testing.T) {
7272
t.Parallel()
7373

74-
act := slogfmt.HumanEntry(slogfmt.ForceColorWriter, slog.SinkEntry{
74+
act := entryhuman.Fmt(entryhuman.ForceColorWriter, slog.SinkEntry{
7575
Level: slog.LevelCritical,
7676
Fields: slog.M(
7777
slog.F("hey", "hi"),
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
package slogfmt
1+
package entryhuman
22

33
var ForceColorWriter = forceColorWriter

internal/slogfmt/json.go renamed to internal/entryhuman/json.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package slogfmt
1+
package entryhuman
22

33
import (
44
"bytes"

internal/entryjson/slogjson.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package entryjson
2+
3+
import (
4+
"regexp"
5+
)
6+
7+
// Filter filters the json field f from j.
8+
func Filter(j, f string) string {
9+
return regexp.MustCompile(`"`+f+`":[^,]+,`).ReplaceAllString(j, "")
10+
}

s_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
"cdr.dev/slog"
88
"cdr.dev/slog/internal/assert"
9-
"cdr.dev/slog/internal/slogfmt"
9+
"cdr.dev/slog/internal/entryhuman"
1010
"cdr.dev/slog/sloggers/sloghuman"
1111
)
1212

@@ -20,7 +20,7 @@ func TestStdlib(t *testing.T) {
2020
stdlibLog := slog.Stdlib(bg, l)
2121
stdlibLog.Println("stdlib")
2222

23-
et, rest, err := slogfmt.StripTimestamp(b.String())
23+
et, rest, err := entryhuman.StripTimestamp(b.String())
2424
assert.Success(t, err, "strip timestamp")
2525
assert.False(t, et.IsZero(), "timestamp")
2626
assert.Equal(t, " [INFO]\t(stdlib)\t<s_test.go:21>\tstdlib\t{\"hi\": \"we\"}\n", rest, "entry")

sloggers/sloghuman/sloghuman.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"golang.org/x/xerrors"
1111

1212
"cdr.dev/slog"
13-
"cdr.dev/slog/internal/slogfmt"
13+
"cdr.dev/slog/internal/entryhuman"
1414
"cdr.dev/slog/internal/syncwriter"
1515
)
1616

@@ -31,7 +31,7 @@ type humanSink struct {
3131
}
3232

3333
func (s humanSink) LogEntry(ctx context.Context, ent slog.SinkEntry) error {
34-
str := slogfmt.HumanEntry(s.w2, ent)
34+
str := entryhuman.Fmt(s.w2, ent)
3535
lines := strings.Split(str, "\n")
3636

3737
// We need to add 4 spaces before every field line for readability.

sloggers/sloghuman/sloghuman_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
"cdr.dev/slog"
99
"cdr.dev/slog/internal/assert"
10-
"cdr.dev/slog/internal/slogfmt"
10+
"cdr.dev/slog/internal/entryhuman"
1111
"cdr.dev/slog/sloggers/sloghuman"
1212
)
1313

@@ -21,7 +21,7 @@ func TestMake(t *testing.T) {
2121
l.Info(bg, "line1\n\nline2", slog.F("wowow", "me\nyou"))
2222
l.Sync()
2323

24-
et, rest, err := slogfmt.StripTimestamp(b.String())
24+
et, rest, err := entryhuman.StripTimestamp(b.String())
2525
assert.Success(t, err, "strip timestamp")
2626
assert.False(t, et.IsZero(), "timestamp")
2727
assert.Equal(t, " [INFO]\t<sloghuman_test.go:21>\t...\t{\"wowow\": \"me\\nyou\"}\n \"msg\": line1\n\n line2\n", rest, "entry")

sloggers/slogjson/slogjson_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
"cdr.dev/slog"
1313
"cdr.dev/slog/internal/assert"
14-
"cdr.dev/slog/internal/slogfmt"
14+
"cdr.dev/slog/internal/entryjson"
1515
"cdr.dev/slog/sloggers/slogjson"
1616
)
1717

@@ -27,7 +27,7 @@ func TestMake(t *testing.T) {
2727
l := slogjson.Make(b)
2828
l.Error(ctx, "line1\n\nline2", slog.F("wowow", "me\nyou"))
2929

30-
j := slogfmt.FilterJSONField(b.String(), "ts")
30+
j := entryjson.Filter(b.String(), "ts")
3131
exp := fmt.Sprintf(`{"level":"ERROR","component":"","msg":"line1\n\nline2","caller":"%v:28","func":"cdr.dev/slog/sloggers/slogjson_test.TestMake","trace":"%v","span":"%v","fields":{"wowow":"me\nyou"}}
3232
`, slogjsonTestFile, s.SpanContext().TraceID, s.SpanContext().SpanID)
3333
assert.Equal(t, exp, j, "entry")

sloggers/slogstackdriver/slogstackdriver_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
"cdr.dev/slog"
1414
"cdr.dev/slog/internal/assert"
15-
"cdr.dev/slog/internal/slogfmt"
15+
"cdr.dev/slog/internal/entryjson"
1616
"cdr.dev/slog/sloggers/slogstackdriver"
1717
)
1818

@@ -30,7 +30,7 @@ func TestStackdriver(t *testing.T) {
3030
l = l.Named("meow")
3131
l.Error(ctx, "line1\n\nline2", slog.F("wowow", "me\nyou"))
3232

33-
j := slogfmt.FilterJSONField(b.String(), "timestamp")
33+
j := entryjson.Filter(b.String(), "timestamp")
3434
exp := fmt.Sprintf(`{"severity":"ERROR","message":"line1\n\nline2","logging.googleapis.com/sourceLocation":"file:\"%v\" line:31 function:\"cdr.dev/slog/sloggers/slogstackdriver_test.TestStackdriver\" ","logging.googleapis.com/operation":"producer:\"meow\" ","logging.googleapis.com/trace":"projects//traces/%v","logging.googleapis.com/spanId":"%v","logging.googleapis.com/trace_sampled":false,"logging.googleapis.com/labels":{"label":2},"wowow":"me\nyou"}
3535
`, slogstackdriverTestFile, s.SpanContext().TraceID, s.SpanContext().SpanID)
3636
assert.Equal(t, exp, j, "entry")

0 commit comments

Comments
 (0)