Better Stack Go logging

Start logging in 2 steps

Collect logs from your Go application.

1. Install

Add slog-betterstack to your project dependencies using go get.

Install the slog-betterstack package
go get github.com/BetterStackHQ/slog-betterstack

2. Set up handler and start logging

Configure slog-betterstack as a handler in a new slog logger.
Your log events will be sent to Better Stack automatically.

Logging using slog-betterstack
import (
    slogbetterstack "github.com/BetterStackHQ/slog-betterstack"
    "log/slog"
)

func main() {
    handler := slogbetterstack.Option{
        Token:    "$SOURCE_TOKEN",
        Endpoint: "https://$INGESTING_HOST/",
        Level:    slog.LevelInfo, // Debug if omitted
    }.NewBetterstackHandler()
    defer handler.Close()

    logger := slog.New(handler)

    // Logger is ready to be used
    logger.Info("Hello from Better Stack!")
}


You should see your logs in Better Stack β†’ Live tail.

Running a short-lived process?

Logs are sent in batches in the background. handler.Close() sends the logs still queued and waits up to 15 seconds for them. os.Exit and log.Fatal skip deferred calls, so close the handler before calling them.

Not seeing any logs?

Delivery failures, such as a rejected source token, are printed to stderr. Set OnError to handle them yourself.

Huge thanks to Samuel Berthe who created the slog-betterstack library as an MIT-licensed open-source! ❀️

Context and structured data

You can use structured data in slog and send context along with your logs.
For more details check out the slog official documentation.

Logging with context in slog
import (
    "fmt"
    "time"

    slogbetterstack "github.com/BetterStackHQ/slog-betterstack"

    "log/slog"
)

func main() {
    handler := slogbetterstack.Option{
        Level:    slog.LevelDebug,
        Token:    "$SOURCE_TOKEN",
        Endpoint: "https://$INGESTING_HOST/",
    }.NewBetterstackHandler()
    defer handler.Close()

    logger := slog.New(handler)

    // You can add common context
    logger = logger.With("release", "v1.0.0")

    // Log a debug message
    logger.Debug("Debugging user service.", "service", "UserService")

    // You can add the context using With
    logger.With("userID", 123).Error("Unable to fetch user data.")

    // Or use structured data
    logger.
        With(
            slog.Group("user",
                slog.String("id", "user-123"),
                slog.Time("created_at", time.Now()),
            ),
        ).
        With("error", fmt.Errorf("an error")).
        Error("a message", slog.Int("count", 1))
}


Configure the handler

Fine-tune delivery with more fields of slogbetterstack.Option. See the package reference for all of them.

Handler options

Level
slog.LevelDebug slog.Leveler
BatchSize
1000 integer
BatchInterval
1s time.Duration
MaxRetries
5 integer
ShutdownTimeout
15s time.Duration
OnError
func(error)

Upgrading from samber/slog-betterstack

Better Stack maintains the library at github.com/BetterStackHQ/slog-betterstack since version 1.4.4. The API and the shape of the logs are unchanged.

  1. Replace the import path with github.com/BetterStackHQ/slog-betterstack and run go get.
  2. Add defer handler.Close() so the logs still queued are sent before your program exits.

Need help?

Please reach out to us at hello@betterstack.com.
We're happy to help! πŸ™