Go Logs

Learn how to setup logs in your Go application.

Setup

Open the Vigilant dashboard and generate an API key.

Install

Install the Vigilant SDK in your Go application.

go get github.com/vigilant-run/vigilant-golang/v2

Initialize

Initialize the Vigilant SDK in your Go application. Make sure to shutdown the SDK before your application exits.

package main

import (
  "github.com/vigilant-run/vigilant-golang/v2"
)

func main() {
  config := vigilant.NewConfigBuilder().
    WithName("backend").
    WithToken("tk_1234567890"). // Use your API key from the dashboard
    Build()

  vigilant.Init(config)
  defer vigilant.Shutdown()
}

Logging Functions

The Vigilant SDK provides several functions to log messages. You can log messages with different log levels and add custom attributes to the logs.

import (
    "github.com/vigilant-run/vigilant-golang/v2"
)

func function() {
  // Log a message
  vigilant.LogInfo("Hello, World!")
  vigilant.LogError("An error occurred")
  vigilant.LogWarn("A warning occurred")
  vigilant.LogDebug("A debug message")
  vigilant.LogTrace("A trace message")

  // Log a formatted message
  vigilant.LogInfof("Hello, %s!", "World")
  vigilant.LogErrorf("An error occurred: %s", "error")
  vigilant.LogWarnf("A warning occurred: %s", "warning")
  vigilant.LogDebugf("A debug message: %s", "debug")
  vigilant.LogTracef("A trace message: %s", "trace")

  // Log with typed attributes
  vigilant.LogErrort("An error occurred", vigilant.String("error", "some error"))
  vigilant.LogWarnt("A warning occurred", vigilant.Int("warning", 123))
  vigilant.LogInfot("A info message", vigilant.Bool("info", true))
  vigilant.LogDebugt("A debug message", vigilant.Float64("debug", 123.456))
  vigilant.LogTracet("A trace message", vigilant.Time("trace", time.Now()))

  // Log with key-value attributes (automatically converted to attribute pairs)
  vigilant.LogErrorw("An error occurred", "error", "some error")
  vigilant.LogWarnw("A warning occurred", "warning", "some warning")
  vigilant.LogInfow("A info message", "info", "some info")
  vigilant.LogDebugw("A debug message", "debug", "some debug")
  vigilant.LogTracew("A trace message", "trace", "some trace")
}

Optional Setup

The Vigilant SDK exposes a few setup options when you initialize it.

package main

import (
  "github.com/vigilant-run/vigilant-golang/v2"
)

func main() {
  config := vigilant.NewConfigBuilder().
    // Disable all functionality
    WithNoop(true).
    // Disable logging to the console
    WithPassthrough(false).
    // Add global attributes to all logs
    WithAttributes(vigilant.String("env", "prod")).
    // Build the config
    Build()

  vigilant.Init(config)
  defer vigilant.Shutdown()
}