Skip to content

Commit 14b3125

Browse files
committed
Add Sinks
Closes #8
1 parent ca4ed15 commit 14b3125

22 files changed

+507
-560
lines changed

ci/lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ cd "$(git rev-parse --show-toplevel)"
77
# shellcheck disable=SC2046
88
shellcheck -x $(git ls-files "*.sh")
99
go vet ./...
10-
go run golang.org/x/lint/golint -set_exit_status ./...
10+
#go run golang.org/x/lint/golint -set_exit_status ./...

doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
// Package slog implements minimal structured logging.
12
package slog // import "go.coder.com/slog"

entry.go

Lines changed: 0 additions & 123 deletions
This file was deleted.

examples_test.go

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,12 @@
11
package slog_test
22

33
import (
4-
"context"
54
"testing"
65

76
"go.coder.com/slog"
8-
"go.coder.com/slog/stderrlog"
97
"go.coder.com/slog/testlog"
108
)
119

12-
func Example_stderr() {
13-
ctx := context.Background()
14-
stderrlog.Info(ctx, "my message here",
15-
slog.F("field_name", "something or the other"),
16-
slog.F("some_map", map[string]interface{}{
17-
"nested_fields": "wowow",
18-
}),
19-
slog.F("some slice", []interface{}{
20-
1,
21-
"foof",
22-
"bar",
23-
true,
24-
}),
25-
slog.Component("test"),
26-
27-
slog.F("name", slog.ValueFunc(func() interface{} {
28-
return "wow"
29-
})),
30-
)
31-
32-
// test_test.go:17: Sep 06 14:33:34.677 [INFO] (test): my_message_here
33-
// field_name: something or the other
34-
// some_map:
35-
// nested_fields: wowow
36-
// error:
37-
// - msg: wrap2
38-
// loc: /Users/nhooyr/src/cdr/slog/test_test.go:22
39-
// fun: go.coder.com/slog_test.TestExampleStderr
40-
// - msg: wrap1
41-
// loc: /Users/nhooyr/src/cdr/slog/test_test.go:23
42-
// fun: go.coder.com/slog_test.TestExampleStderr
43-
// - EOF
44-
// name: wow
45-
}
46-
4710
func Example_test() {
4811
// Nil here but would be provided by the testing framework.
4912
var t *testing.T
@@ -61,9 +24,7 @@ func Example_test() {
6124
}),
6225
slog.Component("test"),
6326

64-
slog.F("name", slog.ValueFunc(func() interface{} {
65-
return "wow"
66-
})),
27+
slog.F("name", "hi"),
6728
)
6829

6930
// --- PASS: TestExampleTest (0.00s)

fields.go

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,25 @@ package slog
22

33
import (
44
"context"
5-
"reflect"
5+
"runtime"
6+
"time"
67

78
"go.opencensus.io/trace"
8-
)
9-
10-
type level string
119

12-
const (
13-
levelDebug level = "DEBUG"
14-
levelInfo level = "INFO"
15-
levelWarn level = "WARN"
16-
levelError level = "ERROR"
17-
levelCritical level = "CRITICAL"
18-
levelFatal level = "FATAL"
10+
"go.coder.com/slog/internal/skipctx"
11+
"go.coder.com/slog/slogcore"
1912
)
2013

2114
type parsedFields struct {
2215
component string
2316
spanCtx trace.SpanContext
2417

25-
fields fieldMap
18+
fields slogcore.Map
2619
}
2720

2821
func parseFields(fields []Field) parsedFields {
2922
var l parsedFields
30-
l.fields = make(fieldMap, 0, len(fields))
23+
l.fields = make(slogcore.Map, 0, len(fields))
3124

3225
for _, f := range fields {
3326
if s, ok := f.(componentField); ok {
@@ -41,7 +34,7 @@ func parseFields(fields []Field) parsedFields {
4134
}
4235

4336
func (l parsedFields) appendField(k string, v interface{}) parsedFields {
44-
l.fields = l.fields.append(k, reflectFieldValue(reflect.ValueOf(v)))
37+
l.fields = l.fields.Append(k, slogcore.Reflect(v))
4538
return l
4639
}
4740

@@ -55,7 +48,7 @@ func (l parsedFields) with(l2 parsedFields) parsedFields {
5548
l.spanCtx = l2.spanCtx
5649
}
5750

58-
l.fields = l.fields.appendFields(l2.fields)
51+
l.fields = l.fields.AppendFields(l2.fields)
5952
return l
6053
}
6154

@@ -76,3 +69,41 @@ func (l parsedFields) withContext(ctx context.Context) parsedFields {
7669

7770
return l.with(l2)
7871
}
72+
73+
type entryParams struct {
74+
level slogcore.Level
75+
msg string
76+
fields []Field
77+
skip int
78+
}
79+
80+
func (l parsedFields) entry(ctx context.Context, params entryParams) slogcore.Entry {
81+
l = l.withContext(ctx)
82+
l = l.withFields(params.fields)
83+
84+
ent := slogcore.Entry{
85+
Time: time.Now(),
86+
Level: params.level,
87+
Component: l.component,
88+
Message: params.msg,
89+
SpanContext: trace.FromContext(ctx).SpanContext(),
90+
Fields: l.fields,
91+
}
92+
93+
file, line, fn, ok := location(params.skip + 1 + skipctx.From(ctx))
94+
if ok {
95+
ent.File = file
96+
ent.Line = line
97+
ent.Func = fn
98+
}
99+
return ent
100+
}
101+
102+
func location(skip int) (file string, line int, fn string, ok bool) {
103+
pc, file, line, ok := runtime.Caller(skip + 1)
104+
if !ok {
105+
return "", 0, "", false
106+
}
107+
f := runtime.FuncForPC(pc)
108+
return file, line, f.Name(), true
109+
}

console.go renamed to internal/console/marshal.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
package slog
1+
package console
22

33
import (
44
"strconv"
55
"strings"
6+
7+
"go.coder.com/slog/slogcore"
68
)
79

810
// consoleMarshaller marshals a fieldValue into a human readable format.
@@ -11,9 +13,9 @@ type consoleMarshaller struct {
1113
indentstr string
1214
}
1315

14-
func marshalFields(v fieldMap) string {
16+
func Fields(m slogcore.Map) string {
1517
var y consoleMarshaller
16-
y.marshal(v)
18+
y.marshal(m)
1719
return y.out.String()
1820
}
1921

@@ -41,40 +43,40 @@ func (y *consoleMarshaller) unindent() {
4143
y.indentstr = y.indentstr[:len(y.indentstr)-2]
4244
}
4345

44-
func (y *consoleMarshaller) marshal(v fieldValue) {
46+
func (y *consoleMarshaller) marshal(v slogcore.Value) {
4547
switch v := v.(type) {
46-
case fieldString:
48+
case slogcore.String:
4749
// Ensures indentation.
4850
y.indent()
4951
// Replaces every newline with a newline plus the correct indentation.
5052
y.s(strings.ReplaceAll(string(v), "\n", "\n"+y.indentstr))
5153
y.unindent()
52-
case fieldBool:
54+
case slogcore.Bool:
5355
y.s(strconv.FormatBool(bool(v)))
54-
case fieldFloat:
56+
case slogcore.Float:
5557
y.s(strconv.FormatFloat(float64(v), 'f', -1, 64))
56-
case fieldInt:
58+
case slogcore.Int:
5759
y.s(strconv.FormatInt(int64(v), 10))
58-
case fieldUint:
60+
case slogcore.Uint:
5961
y.s(strconv.FormatUint(uint64(v), 10))
60-
case fieldMap:
62+
case slogcore.Map:
6163
for i, f := range v {
6264
if i > 0 {
6365
// Add newline before every field except first.
6466
y.line()
6567
}
6668

67-
y.s(quote(f.name) + ":")
69+
y.s(quote(f.Name) + ":")
6870

69-
y.marshalSub(f.value, true)
71+
y.marshalSub(f.Value, true)
7072
}
71-
case fieldList:
73+
case slogcore.List:
7274
y.indent()
7375
for _, v := range v {
7476
y.line()
7577
y.s("-")
7678

77-
if _, ok := v.(fieldList); !ok {
79+
if _, ok := v.(slogcore.List); !ok {
7880
// Non list values begin with the -.
7981
y.s(" ")
8082
}
@@ -88,9 +90,9 @@ func (y *consoleMarshaller) marshal(v fieldValue) {
8890
}
8991
}
9092

91-
func (y *consoleMarshaller) marshalSub(v fieldValue, isParentMap bool) {
93+
func (y *consoleMarshaller) marshalSub(v slogcore.Value, isParentMap bool) {
9294
switch v := v.(type) {
93-
case fieldMap:
95+
case slogcore.Map:
9496
if isParentMap && len(v) == 0 {
9597
// Nothing to output for this field. Without this line, we get additional newlines due to below code as
9698
// the map field is expected to start on the next line given it is in a parentMap.
@@ -106,7 +108,7 @@ func (y *consoleMarshaller) marshalSub(v fieldValue, isParentMap bool) {
106108
// it with the `-` of the list.
107109
y.line()
108110
}
109-
case fieldList:
111+
case slogcore.List:
110112
default:
111113
if isParentMap {
112114
// Non map and non list values in structs begin on the same line with a space between the key and value.
@@ -116,7 +118,7 @@ func (y *consoleMarshaller) marshalSub(v fieldValue, isParentMap bool) {
116118

117119
y.marshal(v)
118120

119-
if _, ok := v.(fieldMap); ok {
121+
if _, ok := v.(slogcore.Map); ok {
120122
y.unindent()
121123
}
122124
}

0 commit comments

Comments
 (0)