Skip to content

Commit a439c42

Browse files
committed
Import all necessary files
1 parent 92de874 commit a439c42

File tree

11 files changed

+1861
-0
lines changed

11 files changed

+1861
-0
lines changed

context.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package core
2+
3+
import (
4+
"context"
5+
6+
"go.coder.com/m/lib/log"
7+
"go.coder.com/m/lib/log/internal/logctx"
8+
)
9+
10+
type (
11+
loggerKey struct{}
12+
skipKey struct{}
13+
stdlibKey struct{}
14+
)
15+
16+
func withContext(ctx context.Context, l Logger) context.Context {
17+
return context.WithValue(ctx, loggerKey{}, l)
18+
}
19+
20+
func fromContext(ctx context.Context) Logger {
21+
l, _ := ctx.Value(loggerKey{}).(Logger)
22+
return l
23+
}
24+
25+
func With(ctx context.Context, fields log.F) context.Context {
26+
l := fromContext(ctx)
27+
l = l.With(fields)
28+
return withContext(ctx, l)
29+
}
30+
31+
func init() {
32+
logctx.With = func(ctx context.Context, fields map[string]interface{}) context.Context {
33+
return With(ctx, fields)
34+
}
35+
logctx.WithSkip = WithSkip
36+
logctx.WithStdlib = WithStdlib
37+
}
38+
39+
func WithSkip(ctx context.Context, skip int) context.Context {
40+
skip += skipFrom(ctx)
41+
return context.WithValue(ctx, skipKey{}, skip)
42+
}
43+
44+
func skipFrom(ctx context.Context) int {
45+
l, _ := ctx.Value(skipKey{}).(int)
46+
return l
47+
}
48+
49+
func WithStdlib(ctx context.Context) context.Context {
50+
return context.WithValue(ctx, stdlibKey{}, true)
51+
}
52+
53+
func IsStdlib(ctx context.Context) bool {
54+
ok, _ := ctx.Value(stdlibKey{}).(bool)
55+
return ok
56+
}

examples_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package log_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"testing"
8+
9+
"go.coder.com/slog"
10+
"go.coder.com/slog/cloudlog"
11+
"go.coder.com/slog/stderrlog"
12+
"go.coder.com/slog/testlog"
13+
)
14+
15+
func Example_stderr() {
16+
ctx := context.Background()
17+
stderrlog.Info(ctx, "my message here", log.F{
18+
"field_name": "something or the other",
19+
"some_map": log.F{
20+
"nested_fields": "wowow",
21+
},
22+
"some slice": []interface{}{
23+
1,
24+
"foof",
25+
"bar",
26+
true,
27+
},
28+
log.Component: "test",
29+
})
30+
}
31+
32+
func Example_stackdriver() {
33+
ctx := context.Background()
34+
l, closefn, err := cloudlog.Make(ctx, cloudlog.Config{
35+
Service: "my_service",
36+
Version: 1,
37+
})
38+
if err != nil {
39+
fmt.Fprintf(os.Stderr, "failed to initialize logger: %v", err)
40+
os.Exit(1)
41+
}
42+
defer func() {
43+
err := closefn()
44+
if err != nil {
45+
fmt.Fprintf(os.Stderr, "failed to close logger: %v", err)
46+
}
47+
}()
48+
49+
l.Info(ctx, "my message here", log.F{
50+
"field_name": "something or the other",
51+
"some_map": map[string]interface{}{
52+
"nested_fields": "wowow",
53+
},
54+
"some slice": []interface{}{
55+
1,
56+
"foof",
57+
"bar",
58+
true,
59+
},
60+
log.Component: "test",
61+
})
62+
}
63+
64+
func Example_test() {
65+
// Nil here but would be provided by the testing framework.
66+
var t *testing.T
67+
68+
testlog.Info(t, "my message here", log.F{
69+
"field_name": "something or the other",
70+
"some_map": map[string]interface{}{
71+
"nested_fields": "wowow",
72+
},
73+
"some slice": []interface{}{
74+
1,
75+
"foof",
76+
"bar",
77+
true,
78+
},
79+
log.Component: "test",
80+
})
81+
}

0 commit comments

Comments
 (0)