https://github.com/miyamo2/altnrslog
alternative library for "New Relic Logs in Context" with log/slog.
https://github.com/miyamo2/altnrslog
go golang newrelic slog
Last synced: 4 months ago
JSON representation
alternative library for "New Relic Logs in Context" with log/slog.
- Host: GitHub
- URL: https://github.com/miyamo2/altnrslog
- Owner: miyamo2
- License: mit
- Created: 2024-02-26T19:00:22.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-28T20:13:48.000Z (over 1 year ago)
- Last Synced: 2026-02-01T14:46:41.426Z (5 months ago)
- Topics: go, golang, newrelic, slog
- Language: Go
- Homepage:
- Size: 47.9 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# altnrslog
[](https://pkg.go.dev/github.com/miyamo2/altnrslog)
[](https://img.shields.io/github/go-mod/go-version/miyamo2/altnrslog?logo=go&style=flat-square)
[](https://img.shields.io/github/v/release/miyamo2/altnrslog?style=flat-square)
[](https://codecov.io/gh/miyamo2/altnrslog)
[](https://goreportcard.com/report/github.com/miyamo2/altnrslog)
[](https://img.shields.io/github/license/miyamo2/altnrslog?style=flat-square&color=blue)
altnrslog is an alternative library for [_New Relic Logs in Context_](https://docs.newrelic.com/docs/logs/logs-context/logs-in-context/) with `log/slog`.
altnrslog can also forward `slog.Attr` even only APM Agent.
## Roadmap to release stable version
- [ ] Transaction Scope
- [x] Supports Logs in Context with APM Agent
- [ ] Supports Logs in Context without APM Agent
- [ ] ~Application Scope~ **CANCELED** [#32](https://github.com/miyamo2/altnrslog/issues/32)
- [ ] ~Supports Logs in Context with APM Agent~
- [ ] ~Supports Logs in Context without APM Agent~
## Getting started
### Installation
```sh
go get github.com/miyamo2/altnrslog
```
### Simple Usage
```go
package main
import (
"encoding/json"
"fmt"
"github.com/miyamo2/altnrslog"
"github.com/newrelic/go-agent/v3/newrelic"
"log"
"log/slog"
"net/http"
"os"
)
type IntroduceRequest struct {
Name string `json:"name"`
}
func main() {
nr, err := newrelic.NewApplication(
newrelic.ConfigAppName(os.Getenv("NEW_RELIC_CONFIG_APP_NAME")),
newrelic.ConfigLicense(os.Getenv("NEW_RELIC_CONFIG_LICENSE")),
newrelic.ConfigAppLogForwardingEnabled(true),
)
if err != nil {
panic(err)
}
http.HandleFunc(newrelic.WrapHandleFunc(nr, "/introduce", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
tx := newrelic.FromContext(ctx)
logHandler := altnrslog.NewTransactionalHandler(nr, tx)
logger := slog.New(logHandler)
var req IntroduceRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
logger.InfoContext(ctx, "START", slog.Group("request", slog.String("name", req.Name)))
response := fmt.Sprintf("Hello, %s!", req.Name)
defer logger.InfoContext(ctx, "END", slog.String("response", response))
w.Write([]byte(response))
}))
log.Fatal(http.ListenAndServe(":8080", nil))
}
```
## Contributing
Feel free to open PR or an Issue.