---
title: "Laravel for Go"
description: "Learn how to send errors, logs, and traces from your Laravel for Go application to Sentry."
url: https://docs.sentry.io/platforms/go/guides/laravel/
last_updated: 2026-12-04T15:16:13.000Z
---

# Sentry for Laravel for Go

[Laravel for Go](https://laravel.com/go/docs) is a full-stack web framework for Go. It reports errors through an error hook, logs with `log/slog`, and traces with OpenTelemetry, so you should instrument it with:

* The SDK's [capture methods](https://docs.sentry.io/platforms/go/guides/laravel/usage.md) to capture errors as you normally would. Calling them from Laravel's error hook captures every server error and recovered panic for you.
* The [`sentryslog`](https://docs.sentry.io/platforms/go/guides/laravel/logs/slog.md) handler to send logs as you normally would with `slog`. Adding it to Laravel's log handler sends every record your application and Laravel log.
* Sentry's [OTLP endpoint](https://docs.sentry.io/concepts/otlp.md) to send traces as you normally would with OpenTelemetry. Pointing Laravel's own export at it sends every span Laravel creates, and the [`sentryotel`](https://docs.sentry.io/platforms/go/guides/laravel/tracing/instrumentation/opentelemetry.md) integration links your errors and logs to them.

##### Provisional Setup

Sentry support for Laravel for Go is in progress. Follow it in [getsentry/sentry-go#1572](https://github.com/getsentry/sentry-go/issues/1572). Until we have full support, use the setup on this page.

Laravel for Go is in beta, so its API can change between releases.

## [Install](https://docs.sentry.io/platforms/go/guides/laravel.md#install)

```bash
go get github.com/getsentry/sentry-go
go get github.com/getsentry/sentry-go/slog
go get github.com/getsentry/sentry-go/otel
```

## [Configure](https://docs.sentry.io/platforms/go/guides/laravel.md#configure)

### [Initialize the Sentry SDK](https://docs.sentry.io/platforms/go/guides/laravel.md#initialize-the-sentry-sdk)

Add your DSN to your `.env` file:

```bash
SENTRY_DSN=https://<key>@o<orgId>.ingest.sentry.io/<projectId>
```

Laravel builds its logger before the rest of your application and passes your `.env` values to the `LogHandler` function, so that's where you initialize the SDK. Create `bootstrap/sentry.go`:

```go
package bootstrap

import (
	"context"
	"log/slog"
	"time"

	"github.com/getsentry/sentry-go"
	sentryotel "github.com/getsentry/sentry-go/otel"
	sentryslog "github.com/getsentry/sentry-go/slog"
	"go.laravel.com/config"
	"go.laravel.com/web"
)

// sentryLogHandler initializes the Sentry SDK and sends the application's logs to Sentry.
func sentryLogHandler(next slog.Handler, env *config.Env) (slog.Handler, error) {
	err := sentry.Init(sentry.ClientOptions{
		Dsn:         env.String("SENTRY_DSN", ""),
		Environment: env.String("APP_ENV", "production"),
		// Links errors and logs to Laravel's OpenTelemetry traces.
		Integrations: func(integrations []sentry.Integration) []sentry.Integration {
			return append(integrations, sentryotel.NewOtelIntegration())
		},
	})
	if err != nil {
		return nil, err
	}

	handler := sentryslog.Option{
		LogLevel: []slog.Level{slog.LevelInfo, slog.LevelWarn, slog.LevelError},
	}.NewSentryHandler(context.Background())

	return slog.NewMultiHandler(next, web.LogHandler(handler)), nil
}

// sentryMiddleware gives each request its own Sentry hub.
type sentryMiddleware struct{}

func (sentryMiddleware) Handle(r *web.Request, next web.Next) *web.Response {
	hub := sentry.CurrentHub().Clone()
	hub.Scope().SetRequest(r.Request)

	return next(r.WithContext(sentry.SetHubOnContext(r.Context(), hub)))
}

// reportError sends server errors and recovered panics to Sentry.
func reportError(ctx context.Context, err error) {
	hub := sentry.GetHubFromContext(ctx)
	if hub == nil {
		// Jobs, commands, and scheduled tasks don't pass through the middleware.
		hub = sentry.CurrentHub().Clone()
	}

	hub.Client().CaptureException(err, &sentry.EventHint{Context: ctx}, hub.Scope())

	if _, ok := ctx.(*web.Request); !ok {
		// A failed command exits the process before the deferred flush in main.go runs.
		hub.Flush(2 * time.Second)
	}
}
```

Laravel calls `reportError` with every server error and recovered panic. You can capture errors or logs inside it.

### [Register the Hooks](https://docs.sentry.io/platforms/go/guides/laravel.md#register-the-hooks)

Add the three functions to your options in `bootstrap/app.go`:

```go
func Options() laravel.Options {
	return laravel.Options{
		// ...your existing options
		LogHandler: sentryLogHandler,
		Errors:     web.Errors{Report: reportError},
		Middleware: func(m *laravel.Middleware, cfg *config.Config) {
			// ...your existing middleware
			m.Global.Prepend(sentryMiddleware{})
		},
	}
}
```

Then flush buffered events when your application stops, in `main.go`:

```go
func main() {
	// Flush buffered events before the program terminates.
	// Set the timeout to the maximum duration the program can afford to wait.
	defer sentry.Flush(2 * time.Second)

	laravel.Run(bootstrap.Options())
}
```

When a console command fails, `laravel.Run` exits the process without running deferred calls, so the flush in `main.go` doesn't run. That's why `reportError` flushes itself for errors reported outside a request, such as those from commands, jobs, and scheduled tasks.

### [Send Traces](https://docs.sentry.io/platforms/go/guides/laravel.md#send-traces)

Laravel traces requests, console commands, queued jobs, scheduled tasks, database queries, cache operations, and outgoing HTTP requests with OpenTelemetry. To send those traces to Sentry, set your project's OTLP traces endpoint and authentication header in your `.env` file:

```bash
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://o<orgId>.ingest.sentry.io/api/<projectId>/integration/otlp/v1/traces
OTEL_EXPORTER_OTLP_TRACES_HEADERS="x-sentry-auth=sentry sentry_key=<your-public-key>"
```

The `sentryotel.NewOtelIntegration()` integration links the errors and logs the SDK captures to the trace they happened in.

Laravel continues incoming traces from the W3C `traceparent` header. If a Sentry SDK in another service calls your application, see [Linking Sentry and OTLP Traces](https://docs.sentry.io/concepts/otlp/sentry-with-otel.md#linking-sentry-and-otlp-traces).

## [Verify](https://docs.sentry.io/platforms/go/guides/laravel.md#verify)

Add a route that logs a message and fails:

```go
func Web(route *routing.Router, cfg *config.Config) {
	// ...your existing routes
	route.Get("/debug-sentry", func(r *web.Request) *web.Response {
		slog.InfoContext(r, "Testing Sentry")

		return web.Fail(errors.New("Sentry test error"))
	})
}
```

Start your application with `laravel dev` and open `http://127.0.0.1:8000/debug-sentry` in your browser. You should then see the issue created in Sentry.

## [Usage](https://docs.sentry.io/platforms/go/guides/laravel.md#usage)

`sentryMiddleware` attaches an instance of `*sentry.Hub` to the request's context, which makes it available throughout the rest of the request's lifetime. A `*web.Request` is a `context.Context`, so you can access the hub by passing the request to `sentry.GetHubFromContext()` in any of your proceeding middleware and handlers. And it should be used instead of the global `sentry.CaptureMessage`, `sentry.CaptureException`, or any other calls, as it keeps the separation of data between the requests.

**Keep in mind that `*sentry.Hub` won't be available in middleware that runs before `sentryMiddleware`!**

```go
route.Get("/orders/{order}", func(r *web.Request) *web.Response {
	if hub := sentry.GetHubFromContext(r); hub != nil {
		// Tags set on the hub's scope are added to every error this request reports.
		hub.Scope().SetTag("order", r.PathValue("order"))
	}

	// reportError will catch it just fine.
	panic("y tho")
})
```

## [Next Steps](https://docs.sentry.io/platforms/go/guides/laravel.md#next-steps)

* Explore [practical guides](https://docs.sentry.io/get-started/guides.md) on what to monitor, log, track, and investigate after setup
* Learn more about [using OpenTelemetry with the Go SDK](https://docs.sentry.io/platforms/go/guides/laravel/tracing/instrumentation/opentelemetry.md)

## Other Go Frameworks

- [Echo](https://docs.sentry.io/platforms/go/guides/echo.md)
- [FastHTTP](https://docs.sentry.io/platforms/go/guides/fasthttp.md)
- [Fiber](https://docs.sentry.io/platforms/go/guides/fiber.md)
- [Fiber v3](https://docs.sentry.io/platforms/go/guides/fiberv3.md)
- [Gin](https://docs.sentry.io/platforms/go/guides/gin.md)
- [gRPC](https://docs.sentry.io/platforms/go/guides/grpc.md)
- [Iris](https://docs.sentry.io/platforms/go/guides/iris.md)
- [Negroni](https://docs.sentry.io/platforms/go/guides/negroni.md)
- [net/http](https://docs.sentry.io/platforms/go/guides/http.md)

## Topics

- [Extended Configuration](https://docs.sentry.io/platforms/go/guides/laravel/configuration.md)
- [Capturing Errors](https://docs.sentry.io/platforms/go/guides/laravel/usage.md)
- [Source Context](https://docs.sentry.io/platforms/go/guides/laravel/source-context.md)
- [Integrations](https://docs.sentry.io/platforms/go/guides/laravel/integrations.md)
- [Enriching Events](https://docs.sentry.io/platforms/go/guides/laravel/enriching-events.md)
- [Data Management](https://docs.sentry.io/platforms/go/guides/laravel/data-management.md)
- [Tracing](https://docs.sentry.io/platforms/go/guides/laravel/tracing.md)
- [Logs](https://docs.sentry.io/platforms/go/guides/laravel/logs.md)
- [Application Metrics](https://docs.sentry.io/platforms/go/guides/laravel/metrics.md)
- [Crons](https://docs.sentry.io/platforms/go/guides/laravel/crons.md)
- [Security Policy Reporting](https://docs.sentry.io/platforms/go/guides/laravel/security-policy-reporting.md)
- [Migration Guide](https://docs.sentry.io/platforms/go/guides/laravel/migration.md)
- [Troubleshooting](https://docs.sentry.io/platforms/go/guides/laravel/troubleshooting.md)

---

Find other pages: [Sitemap](https://docs.sentry.io/sitemap.md) lists every page with a one-line summary, and [llms.txt](https://docs.sentry.io/llms.txt) gives an overview with guidance for agents.
