Skip to content

Commit 646b5aa

Browse files
authored
Merge pull request #65 from cdr/ammar
Fixes from Ammar's review
2 parents 61f13e5 + c93ed4b commit 646b5aa

File tree

8 files changed

+27
-23
lines changed

8 files changed

+27
-23
lines changed

internal/entryhuman/entry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ func Fmt(w io.Writer, ent slog.SinkEntry) string {
5858
level = c(w, levelColor(ent.Level)).Sprint(level)
5959
ents += fmt.Sprintf("%v\t", level)
6060

61-
if ent.LoggerName != "" {
62-
component := "(" + quoteKey(ent.LoggerName) + ")"
61+
if len(ent.Names) > 0 {
62+
component := "(" + quoteKey(strings.Join(ent.Names, ".")) + ")"
6363
component = c(w, color.FgMagenta).Sprint(component)
6464
ents += fmt.Sprintf("%v\t", component)
6565
}

internal/entryhuman/entry_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ line2`)
5151
t.Parallel()
5252

5353
test(t, slog.SinkEntry{
54-
Level: slog.LevelWarn,
55-
LoggerName: "named.meow",
54+
Level: slog.LevelWarn,
55+
Names: []string{"named", "meow"},
5656
}, `0001-01-01 00:00:00.000 [WARN] (named.meow) <.:0> ""`)
5757
})
5858

map_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestMap(t *testing.T) {
2121
t.Parallel()
2222

2323
test := func(t *testing.T, m slog.Map, exp string) {
24+
t.Helper()
2425
exp = indentJSON(t, exp)
2526
act := marshalJSON(t, m)
2627
assert.Equal(t, exp, act, "JSON")
@@ -61,12 +62,12 @@ func TestMap(t *testing.T) {
6162
{
6263
"msg": "wrap1",
6364
"fun": "cdr.dev/slog_test.TestMap.func2",
64-
"loc": "`+mapTestFile+`:40"
65+
"loc": "`+mapTestFile+`:41"
6566
},
6667
{
6768
"msg": "wrap2",
6869
"fun": "cdr.dev/slog_test.TestMap.func2",
69-
"loc": "`+mapTestFile+`:41"
70+
"loc": "`+mapTestFile+`:42"
7071
},
7172
"EOF"
7273
],
@@ -82,7 +83,7 @@ func TestMap(t *testing.T) {
8283
t.Run("badJSON", func(t *testing.T) {
8384
t.Parallel()
8485

85-
mapTestFile = strings.Replace(mapTestFile, "_test", "", 1)
86+
mapTestFile := strings.Replace(mapTestFile, "_test", "", 1)
8687

8788
test(t, slog.M(
8889
slog.F("meow", indentJSON),
@@ -92,7 +93,7 @@ func TestMap(t *testing.T) {
9293
{
9394
"msg": "failed to marshal to JSON",
9495
"fun": "cdr.dev/slog.encode",
95-
"loc": "`+mapTestFile+`:84"
96+
"loc": "`+mapTestFile+`:88"
9697
},
9798
"json: unsupported type: func(*testing.T, string) string"
9899
],

slog.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ type SinkEntry struct {
7777
Level Level
7878
Message string
7979

80-
LoggerName string
80+
// Names represents the chain of names on the
81+
// logger constructed with Named.
82+
Names []string
8183

8284
Func string
8385
File string
@@ -143,7 +145,7 @@ func Make(s Sink) Logger {
143145
}
144146

145147
type sink struct {
146-
name string
148+
name []string
147149
sink Sink
148150
level *int64
149151
fields Map
@@ -160,12 +162,9 @@ func (s sink) withFields(fields Map) sink {
160162
return s
161163
}
162164

163-
func (s sink) named(name string) sink {
164-
if s.name == "" {
165-
s.name = name
166-
} else if name != "" {
167-
s.name += "." + name
168-
}
165+
func (s sink) named(names ...string) sink {
166+
s.name = append([]string(nil), s.name...)
167+
s.name = append(s.name, names...)
169168
return s
170169
}
171170

@@ -343,9 +342,9 @@ func (ent SinkEntry) fillLoc(skip int) SinkEntry {
343342
func (s sink) entry(ctx context.Context, ent SinkEntry) SinkEntry {
344343
s = s.withContext(ctx)
345344
s = s.withFields(ent.Fields)
346-
s = s.named(ent.LoggerName)
345+
s = s.named(ent.Names...)
347346

348-
ent.LoggerName = s.name
347+
ent.Names = s.name
349348
ent.Fields = s.fields
350349

351350
return ent

slog_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestLogger(t *testing.T) {
108108
Level: slog.LevelInfo,
109109
Message: "meow",
110110

111-
LoggerName: "hello.hello2",
111+
Names: []string{"hello", "hello2"},
112112

113113
File: slogTestFile,
114114
Func: "cdr.dev/slog_test.TestLogger.func3",

sloggers/slogjson/slogjson.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,15 @@ func (s jsonSink) LogEntry(ctx context.Context, ent slog.SinkEntry) error {
4949
m := slog.M(
5050
slog.F("ts", ent.Time),
5151
slog.F("level", ent.Level),
52-
slog.F("component", ent.LoggerName),
5352
slog.F("msg", ent.Message),
5453
slog.F("caller", fmt.Sprintf("%v:%v", ent.File, ent.Line)),
5554
slog.F("func", ent.Func),
5655
)
5756

57+
if len(ent.Names) > 0 {
58+
m = append(m, slog.F("component", ent.Names))
59+
}
60+
5861
if ent.SpanContext != (trace.SpanContext{}) {
5962
m = append(m,
6063
slog.F("trace", ent.SpanContext.TraceID),

sloggers/slogjson/slogjson_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestMake(t *testing.T) {
2828
l.Error(ctx, "line1\n\nline2", slog.F("wowow", "me\nyou"))
2929

3030
j := entryjson.Filter(b.String(), "ts")
31-
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"}}
31+
exp := fmt.Sprintf(`{"level":"ERROR","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")
3434
}

sloggers/slogstackdriver/slogstackdriver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9+
"strings"
910

1011
"cloud.google.com/go/compute/metadata"
1112
"go.opencensus.io/trace"
@@ -58,9 +59,9 @@ func (s stackdriverSink) LogEntry(ctx context.Context, ent slog.SinkEntry) error
5859
}),
5960
)
6061

61-
if ent.LoggerName != "" {
62+
if len(ent.Names) > 0 {
6263
e = append(e, slog.F("logging.googleapis.com/operation", &logpb.LogEntryOperation{
63-
Producer: ent.LoggerName,
64+
Producer: strings.Join(ent.Names, "."),
6465
}))
6566
}
6667

0 commit comments

Comments
 (0)