-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathmain.go
45 lines (35 loc) · 1.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"context"
"log/slog"
"os"
"time"
"github.com/newrelic/go-agent/v3/integrations/logcontext-v2/nrslog"
"github.com/newrelic/go-agent/v3/newrelic"
)
func main() {
app, err := newrelic.NewApplication(
newrelic.ConfigAppName("slog example app"),
newrelic.ConfigFromEnvironment(),
newrelic.ConfigAppLogEnabled(true),
)
if err != nil {
panic(err)
}
app.WaitForConnection(time.Second * 5)
log := slog.New(nrslog.TextHandler(app, os.Stdout, &slog.HandlerOptions{}))
log.Info("I am a log message")
txn := app.StartTransaction("example transaction")
ctx := newrelic.NewContext(context.Background(), txn)
log.InfoContext(ctx, "I am a log inside a transaction with custom attributes!",
slog.String("foo", "bar"),
slog.Int("answer", 42),
slog.Any("some_map", map[string]interface{}{"a": 1.0, "b": 2}),
)
// pretend to do some work
time.Sleep(500 * time.Millisecond)
log.Warn("Uh oh, something important happened!")
txn.End()
log.Info("All Done!")
app.Shutdown(time.Second * 10)
}