Skip to content

Commit ca4adce

Browse files
authored
Merge pull request #15 from cdr/refactor
Refactor Sink and move all loggers to subpkgs
2 parents 456f8bf + 2c36812 commit ca4adce

File tree

24 files changed

+636
-635
lines changed

24 files changed

+636
-635
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ go get go.coder.com/slog
2020
- First class [context.Context](https://blog.golang.org/context) support
2121
- Beautiful logging output by default
2222
- Multiple adapters
23-
- First class [\*testing.T](https://godoc.org/go.coder.com/slog/testlog) support
23+
- First class [\*testing.T](https://godoc.org/go.coder.com/slog/slogtest) support
2424

2525
## Example
2626

ci/.codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
comment: off
1+
.comment: off
22
coverage:
33
status:
44
# Prevent small changes in coverage from failing CI.

ci/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ argv=(
1111
"--format=short-verbose"
1212
--
1313
"-vet=off"
14+
"./..."
1415
)
1516
# Interactive usage does not want to turn off vet or use gotestsum.
1617
if [[ $# -gt 0 ]]; then

examples_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"testing"
55

66
"go.coder.com/slog"
7-
"go.coder.com/slog/slogtest"
7+
"go.coder.com/slog/sloggers/slogtest"
88
)
99

1010
func Example_test() {

fields.go

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

internal/humanfmt/humanfmt.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ package humanfmt
22

33
import (
44
"fmt"
5+
"io"
56
"path/filepath"
67

78
"github.com/fatih/color"
89
"go.opencensus.io/trace"
10+
"golang.org/x/crypto/ssh/terminal"
911

10-
"go.coder.com/slog/slogcore"
12+
"go.coder.com/slog"
13+
"go.coder.com/slog/slogval"
1114
)
1215

13-
func Entry(ent slogcore.Entry, enableColor bool) string {
16+
func Entry(ent slog.Entry, enableColor bool) string {
1417
var ents string
1518
if ent.File != "" {
1619
ents += fmt.Sprintf("%v:%v: ", filepath.Base(ent.File), ent.Line)
@@ -21,7 +24,7 @@ func Entry(ent slogcore.Entry, enableColor bool) string {
2124
cl := levelColor(ent.Level)
2225
ents += color.New(cl).Sprint(ent.Level)
2326
} else {
24-
ents += string(ent.Level)
27+
ents += ent.Level.String()
2528
}
2629

2730
ents += "]"
@@ -45,20 +48,20 @@ func Entry(ent slogcore.Entry, enableColor bool) string {
4548
return ents
4649
}
4750

48-
func pinnedFields(ent slogcore.Entry) string {
49-
pinned := slogcore.Map{}
51+
func pinnedFields(ent slog.Entry) string {
52+
pinned := slogval.Map{}
5053

5154
if ent.SpanContext != (trace.SpanContext{}) {
52-
pinned = pinned.Append("trace", slogcore.String(ent.SpanContext.TraceID.String()))
53-
pinned = pinned.Append("span", slogcore.String(ent.SpanContext.SpanID.String()))
55+
pinned = pinned.Append("trace", slogval.String(ent.SpanContext.TraceID.String()))
56+
pinned = pinned.Append("span", slogval.String(ent.SpanContext.SpanID.String()))
5457
}
5558

56-
return Fields(pinned)
59+
return humanFields(pinned)
5760
}
5861

59-
func stringFields(ent slogcore.Entry) string {
62+
func stringFields(ent slog.Entry) string {
6063
pinned := pinnedFields(ent)
61-
fields := Fields(ent.Fields)
64+
fields := humanFields(slogval.Reflect(ent.Fields))
6265

6366
if pinned == "" {
6467
return fields
@@ -74,20 +77,21 @@ func stringFields(ent slogcore.Entry) string {
7477
// Same as time.StampMilli but the days in the month padded by zeros.
7578
const timestampMilli = "Jan 02 15:04:05.000"
7679

77-
func panicf(f string, v ...interface{}) {
78-
f = "humanfmt: " + f
79-
s := fmt.Sprintf(f, v...)
80-
panic(s)
81-
}
82-
83-
func levelColor(level slogcore.Level) color.Attribute {
80+
func levelColor(level slog.Level) color.Attribute {
8481
switch level {
85-
case slogcore.Debug, slogcore.Info:
82+
case slog.Debug, slog.Info:
8683
return color.FgBlue
87-
case slogcore.Warn:
84+
case slog.Warn:
8885
return color.FgYellow
89-
case slogcore.Error, slogcore.Critical, slogcore.Fatal:
86+
case slog.LevelError, slog.Critical, slog.Fatal:
9087
return color.FgRed
9188
}
9289
panic("humanfmt: unexpected level: " + string(level))
9390
}
91+
92+
func IsTTY(w io.Writer) bool {
93+
f, ok := w.(interface {
94+
Fd() uintptr
95+
})
96+
return ok && terminal.IsTerminal(int(f.Fd()))
97+
}

internal/humanfmt/marshal.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package humanfmt
22

33
import (
4+
"fmt"
45
"strconv"
56
"strings"
67

7-
"go.coder.com/slog/slogcore"
8+
"go.coder.com/slog/slogval"
89
)
910

1011
// consoleMarshaller marshals a fieldValue into a human readable format.
@@ -13,7 +14,7 @@ type consoleMarshaller struct {
1314
indentstr string
1415
}
1516

16-
func Fields(m slogcore.Map) string {
17+
func humanFields(m slogval.Map) string {
1718
var y consoleMarshaller
1819
y.marshal(m)
1920
return y.out.String()
@@ -43,23 +44,23 @@ func (y *consoleMarshaller) unindent() {
4344
y.indentstr = y.indentstr[:len(y.indentstr)-2]
4445
}
4546

46-
func (y *consoleMarshaller) marshal(v slogcore.Value) {
47+
func (y *consoleMarshaller) marshal(v slogval.Value) {
4748
switch v := v.(type) {
48-
case slogcore.String:
49+
case slogval.String:
4950
// Ensures indentation.
5051
y.indent()
5152
// Replaces every newline with a newline plus the correct indentation.
5253
y.s(strings.ReplaceAll(string(v), "\n", "\n"+y.indentstr))
5354
y.unindent()
54-
case slogcore.Bool:
55+
case slogval.Bool:
5556
y.s(strconv.FormatBool(bool(v)))
56-
case slogcore.Float:
57+
case slogval.Float:
5758
y.s(strconv.FormatFloat(float64(v), 'f', -1, 64))
58-
case slogcore.Int:
59+
case slogval.Int:
5960
y.s(strconv.FormatInt(int64(v), 10))
60-
case slogcore.Uint:
61+
case slogval.Uint:
6162
y.s(strconv.FormatUint(uint64(v), 10))
62-
case slogcore.Map:
63+
case slogval.Map:
6364
for i, f := range v {
6465
if i > 0 {
6566
// Add newline before every field except first.
@@ -70,13 +71,13 @@ func (y *consoleMarshaller) marshal(v slogcore.Value) {
7071

7172
y.marshalSub(f.Value, true)
7273
}
73-
case slogcore.List:
74+
case slogval.List:
7475
y.indent()
7576
for _, v := range v {
7677
y.line()
7778
y.s("-")
7879

79-
if _, ok := v.(slogcore.List); !ok {
80+
if _, ok := v.(slogval.List); !ok {
8081
// Non list values begin with the -.
8182
y.s(" ")
8283
}
@@ -90,9 +91,9 @@ func (y *consoleMarshaller) marshal(v slogcore.Value) {
9091
}
9192
}
9293

93-
func (y *consoleMarshaller) marshalSub(v slogcore.Value, isParentMap bool) {
94+
func (y *consoleMarshaller) marshalSub(v slogval.Value, isParentMap bool) {
9495
switch v := v.(type) {
95-
case slogcore.Map:
96+
case slogval.Map:
9697
if isParentMap && len(v) == 0 {
9798
// Nothing to output for this field. Without this line, we get additional newlines due to below code as
9899
// the map field is expected to start on the next line given it is in a parentMap.
@@ -108,7 +109,7 @@ func (y *consoleMarshaller) marshalSub(v slogcore.Value, isParentMap bool) {
108109
// it with the `-` of the list.
109110
y.line()
110111
}
111-
case slogcore.List:
112+
case slogval.List:
112113
default:
113114
if isParentMap {
114115
// Non map and non list values in structs begin on the same line with a space between the key and value.
@@ -118,7 +119,7 @@ func (y *consoleMarshaller) marshalSub(v slogcore.Value, isParentMap bool) {
118119

119120
y.marshal(v)
120121

121-
if _, ok := v.(slogcore.Map); ok {
122+
if _, ok := v.(slogval.Map); ok {
122123
y.unindent()
123124
}
124125
}
@@ -144,3 +145,9 @@ func quote(key string) string {
144145
}
145146
return quoted
146147
}
148+
149+
func panicf(f string, v ...interface{}) {
150+
f = "humanfmt: " + f
151+
s := fmt.Sprintf(f, v...)
152+
panic(s)
153+
}

internal/humanfmt/marshal_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"testing"
55

66
"go.coder.com/slog/internal/diff"
7-
"go.coder.com/slog/slogcore"
7+
"go.coder.com/slog/slogval"
88
)
99

1010
func Test_marshalFields(t *testing.T) {
@@ -116,8 +116,8 @@ dsamkld`,
116116
t.Run(tc.name, func(t *testing.T) {
117117
t.Parallel()
118118

119-
v := slogcore.Reflect(tc.in)
120-
actOut := Fields(v.(slogcore.Map))
119+
v := slogval.ReflectUnsafe(tc.in)
120+
actOut := humanFields(v.(slogval.Map))
121121
t.Logf("yaml:\n%v", actOut)
122122
if diff := diff.Diff(tc.out, actOut); diff != "" {
123123
t.Fatalf("unexpected output: %v", diff)

internal/stdlibctx/stdlibctx.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package stdlibctx
2+
3+
import (
4+
"context"
5+
)
6+
7+
type stdlibKey struct{}
8+
9+
// With appends to the skip offset in the context and returns
10+
// a new context with the new skip offset.
11+
func With(ctx context.Context) context.Context {
12+
return context.WithValue(ctx, stdlibKey{}, true)
13+
}
14+
15+
// From returns the skip offset.
16+
func From(ctx context.Context) bool {
17+
b, _ := ctx.Value(stdlibKey{}).(bool)
18+
return b
19+
}

0 commit comments

Comments
 (0)