Go SDK

Install the Logdash Go SDK, send your first log and your first metric.

Install the package, paste your API key and the first log lands in the dashboard within a second. Everything below is copied from the Go SDK README.

Install

go get github.com/logdash-io/go-sdk/logdash

Initialise

Create a project in Logdash, copy its API key and keep it in an environment variable. The key identifies the project the data lands in.

package main

import (
	"context"
	"os"
	"time"

	"github.com/logdash-io/go-sdk/logdash"
)

func main() {
	ld := logdash.New(
		logdash.WithApiKey(os.Getenv("LOGDASH_API_KEY")),
	)

	// Shutdown waits for every enqueued log and metric to flush.
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	defer ld.Shutdown(ctx)
}

Send a log

package main

import (
	"os"

	"github.com/logdash-io/go-sdk/logdash"
)

func main() {
	ld := logdash.New(logdash.WithApiKey(os.Getenv("LOGDASH_API_KEY")))
	logger := ld.Logger

	logger.Info("Application started successfully")
	logger.Error("An unexpected error occurred")

	// Every level has an ...F() counterpart, like fmt.Printf.
	logger.InfoF("Processing %v of %v item", 1, 10)
}

Send a metric

Metrics are named numbers. Set one to an absolute value or mutate it by a delta, and Logdash charts the history.

package main

import (
	"os"

	"github.com/logdash-io/go-sdk/logdash"
)

func main() {
	ld := logdash.New(logdash.WithApiKey(os.Getenv("LOGDASH_API_KEY")))
	metrics := ld.Metrics

	// to set absolute value
	metrics.Set("users", 0)

	// or increment / decrement by
	metrics.Mutate("users", 1)
}

Works with

  • net/http
  • log/slog
  • Gin
  • Echo
  • Fiber

FAQ

Can I keep using log/slog?

Yes. Wrap the logger with `logdash.NewSlogTextHandler(ld.Logger, slog.HandlerOptions{})` and pass it to `slog.New`. Error maps to Error, Warn to Warn, Info to Info, Debug to Debug and anything below Debug to Silly.

How do I flush before the process exits?

Call `ld.Shutdown(ctx)` with a context deadline. It blocks until every queued log and metric has been sent or the context expires.

Is there a printf-style logging call?

Yes. Every level has an `...F()` counterpart, so `logger.InfoF("Processing %v of %v item", i+1, items)` works the way `fmt.Printf` does.