Logging From Go
This documentation outlines how to configure logging with the AppSignal for Go integration.
Configure Logging
You do not need to create a log source to send logs from the AppSignal for Go integration. An "application" log source will be created automatically.
The Go integration uses OpenTelemetry's logging capabilities to send logs to AppSignal.
Stand-alone usage
To use logging with OpenTelemetry in Go, you need to set up the logger using OpenTelemetry's logger provider.
package main import ( "context" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/log" ) func main() { // Get the logger from the global logger provider loggerProvider := otel.GetLoggerProvider() logger := loggerProvider.Logger("my-app") // Log a message logger.Emit(context.Background(), log.Record{ Severity: log.SeverityInfo, Body: log.StringValue("Log message line"), }) }
Usage with slog
You can configure the standard slog
package to send structured logs to AppSignal using the OpenTelemetry handler.
package main import ( "context" "log/slog" "go.opentelemetry.io/contrib/bridges/otelslog" ) func main() { // Create an OpenTelemetry `slog` handler otelHandler := otelslog.NewHandler("my-app") // Create a `slog` logger with the OpenTelemetry handler logger := slog.New(otelHandler) // Use the logger logger.Info("Log message line") logger.With("user_id", 123).Info("User logged in") logger.Error("An error occurred", "error", "database connection failed") }
Usage
Sending Logs
Using the OpenTelemetry logger directly, you can define the severity level of your logs:
package main import ( "context" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/log" ) func main() { loggerProvider := otel.GetLoggerProvider() logger := loggerProvider.Logger("my-app") // Different severity levels logger.Emit(context.Background(), log.Record{ Severity: log.SeverityWarn, Body: log.StringValue("Something went wrong"), }) logger.Emit(context.Background(), log.Record{ Severity: log.SeverityInfo, Body: log.StringValue("Action completed"), }) logger.Emit(context.Background(), log.Record{ Severity: log.SeverityError, Body: log.StringValue("Database connection failed"), }) }
You can define custom attributes to send log information that can be used when filtering and querying logs:
package main import ( "context" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/log" ) func processInvoice(customerID string) { loggerProvider := otel.GetLoggerProvider() logger := loggerProvider.Logger("invoice_helper") logger.Emit(context.Background(), log.Record{ Severity: log.SeverityInfo, Body: log.StringValue("Generating invoice for customer"), Attributes: []log.KeyValue{ log.String("customer_id", customerID), }, }) invoice := generateInvoice(customerID) logger.Emit(context.Background(), log.Record{ Severity: log.SeverityInfo, Body: log.StringValue("Generated invoice for customer"), Attributes: []log.KeyValue{ log.String("customer_id", customerID), log.String("invoice_id", invoice.ID), }, }) }
You can query and filter on message contents and attribute values from within the Log Management tool.
Once configured, the desired attributes will be sent to AppSignal as log tags, and be queryable in the AppSignal logging interface.
Filtering Logs
You can use the ignore_logs
configuration option to ignore log lines. See our Ignore Logs guide to learn more.
Need Help?
After configuring your Go application to send logs, logs should appear in AppSignal. If you are unsure about this step or AppSignal is not receiving any logs, you can always reach out for assistance. We'll help get you back on track!