We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Instance Context

Different runtime context matters depending on how your app runs. For desktop or mobile apps, useful fields often include:

  • Host operating system
  • Time zone
  • Shared library versions

For backend services this is usually simpler, but scale still matters. Thousands of pods across regions need more context than one app on one EC2 instance.

What to Log

All of this is probably overkill for most backends, but it's worth considering:

  • Runtime environment name (e.g. production, staging, development)
  • The server's hostname
  • The server's IP address
  • Cloud region
  • Node name or IP (for containerized services)
  • Docker container name or Kubernetes pod name
  • Host operating system and kernel version
  • Host/server time zone (particularly if you deploy across geographic regions)

Gathering Runtime Information

For our purposes, we'll log two fields that are almost always useful:

  • Runtime environment name (e.g. production, staging, development)
  • Server hostname/domain (e.g. jons-macbook (local), c7a9d8b32c4a (container), or ip-172-31-22-45 (cloud server))
env := os.Getenv("ENV")
hostname, _ := os.Hostname()

logger = logger.With(
	slog.String("env", env),
	slog.String("hostname", hostname),
)

Assignment

Include runtime instance metadata in every log.

Build your app with -ldflags and run it with ENV and LINKO_LOG_FILE set:

go build \
  -ldflags "-X boot.dev/linko/internal/build.GitSHA=$(git rev-parse HEAD) -X boot.dev/linko/internal/build.BuildTime=$(date -u '+%Y-%m-%dT%H:%M:%SZ')" \
  -o linko &&
LINKO_LOG_FILE=linko.access.log ENV=development ./linko

Run and submit the CLI tests from the root of the Linko repo.