Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update readme #7

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.21' ]
go-version: [ '1.21', '1.22' ]

steps:
- uses: actions/checkout@v4
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ Golang structured logging (slog) handler middleware for Bugsnag.
Automatically send all Error level logs to Bugsnag, along with all attributes and context.
Never forget to snag another bug again.

### Other Great SLOG Utilities
- [slogctx](https://github.com/veqryn/slog-context): Add attributes to context and have them automatically added to all log lines. Work with a logger stored in context.
- [slogotel](https://github.com/veqryn/slog-context/tree/main/otel): Automatically extract and add [OpenTelemetry](https://opentelemetry.io/) TraceID's to all log lines.
- [slogdedup](https://github.com/veqryn/slog-dedup): Middleware that deduplicates and sorts attributes. Particularly useful for JSON logging.
- [slogbugsnag](https://github.com/veqryn/slog-bugsnag): Middleware that pipes Errors to [Bugsnag](https://www.bugsnag.com/).

## Install
`go get github.com/veqryn/slog-bugsnag`
Expand Down
73 changes: 73 additions & 0 deletions slogtest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package slogbugsnag_test

import (
"bytes"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"net/http/httptest"
"testing"
"testing/slogtest"

"github.com/bugsnag/bugsnag-go/v2"
slogbugsnag "github.com/veqryn/slog-bugsnag"
)

func TestSlogtest(t *testing.T) {

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer svr.Close()

// Set the bugsnag config to send all communication to the test server
notifiers := slogbugsnag.NewNotifierWorkers(&slogbugsnag.NotifierOptions{
Notifier: bugsnag.New(bugsnag.Configuration{
Endpoints: bugsnag.Endpoints{
Notify: svr.URL,
Sessions: svr.URL,
},
}),
})

opts := &slogbugsnag.HandlerOptions{Notifiers: notifiers}

var buf bytes.Buffer
h := slogbugsnag.NewHandler(slog.NewJSONHandler(&buf, nil), opts)

results := func() []map[string]any {
ms, err := parseLines(buf.Bytes(), parseJSON)
if err != nil {
t.Fatal(err)
}
return ms
}
if err := slogtest.TestHandler(h, results); err != nil {
t.Fatal(err)
}
}

func parseLines(src []byte, parse func([]byte) (map[string]any, error)) ([]map[string]any, error) {
fmt.Println(string(src))
var records []map[string]any
for _, line := range bytes.Split(src, []byte{'\n'}) {
if len(line) == 0 {
continue
}
m, err := parse(line)
if err != nil {
return nil, fmt.Errorf("%s: %w", string(line), err)
}
records = append(records, m)
}
return records, nil
}

func parseJSON(bs []byte) (map[string]any, error) {
var m map[string]any
if err := json.Unmarshal(bs, &m); err != nil {
return nil, err
}
return m, nil
}
Loading