appinsights

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 10, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package appinsights provides a slog Handler to submit log records to Application Insights. See more at https://azure.microsoft.com/en-us/services/application-insights/

Getting Started

Install this module using the following command.

go get github.com/openclosed-dev/slogan

Usage

The code below shows how to use the handler in this package.

import (
	"fmt"
	"log/slog"
	"os"

	"github.com/openclosed-dev/slogan/appinsights"
)

func main() {

	// The connection string provided by your Application Insights resource.
	connectionString := os.Getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")

	// Creates a handler for Application Insights using the default options.
	handler, err := appinsights.NewHandler(connectionString, nil)
	if err != nil {
		fmt.Print(err)
		return
	}
	// The handler should be closed to flush log records before the application exits.
	defer handler.Close()

	// Creates a slog.Logger with the handler.
	logger := slog.New(handler)
	// Makes the logger as the system-wide default if you prefer.
	slog.SetDefault(logger)

	slog.Info("hello", "count", 3)
}

Index

Examples

Constants

View Source
const (
	// LevelCritical is the log level corresponding to the Critical level
	// in Application Insights
	LevelCritical = slog.Level(12)
	// LevelFatal is an alias of [LevelCritical]
	LevelFatal = LevelCritical
)

Variables

This section is empty.

Functions

func EnableDiagnostics

func EnableDiagnostics()

EnableDiagnostics enables telemetry printing for debugging purpose.

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler is a slog.Handler that submits log records to Azure Application Insights.

Example
package main

import (
	"fmt"
	"log/slog"
	"os"

	"github.com/openclosed-dev/slogan/appinsights"
)

func main() {

	// The connection string provided by your Application Insights resource.
	connectionString := os.Getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")

	// Creates a handler for Application Insights using the default options.
	handler, err := appinsights.NewHandler(connectionString, nil)
	if err != nil {
		fmt.Print(err)
		return
	}
	// The handler should be closed to flush log records before the application exits.
	defer handler.Close()

	// Creates a slog.Logger with the handler.
	logger := slog.New(handler)
	// Makes the logger as the system-wide default if you prefer.
	slog.SetDefault(logger)

	slog.Info("hello", "count", 3)
}

func NewHandler

func NewHandler(connectionString string, opts *HandlerOptions) (*Handler, error)

NewHandler creates a Handler that submits log records to Azure Application Insights resource. The argument connectionString must be a valid connection string given by the target Application Insights resource. opts must be created by the NewHandlerOptions function, or may be nil if the default settings are sufficient.

func (*Handler) Close

func (h *Handler) Close()

Close flushes the buffered log records and waits until the transmission is complete.

func (*Handler) Enabled

func (h *Handler) Enabled(_ context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.

func (*Handler) Handle

func (h *Handler) Handle(_ context.Context, r slog.Record) error

Handle handles the log Record.

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new Handler whose attributes consists of h's attributes followed by attrs.

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

WithGroup returns a new Handler with the given group appended to the receiver's existing groups. The keys of all subsequent attributes, whether added by With or in a Record, will be qualified by the sequence of group names.

type HandlerOptions

type HandlerOptions struct {
	// Level reports the minimum record level that will be logged.
	// Default value is [slog.LevelInfo].
	Level slog.Leveler
	// MaxBatchSize is the maximum number of log records.
	// that can be submitted in a request.
	MaxBatchSize int
	// MaxBatchInterval is the maximum time to wait before sending a batch of log records.
	MaxBatchInterval time.Duration
	// Client is a customized HTTP client.
	Client *http.Client
}

HandlerOptions are options for a Handler.

func NewHandlerOptions

func NewHandlerOptions(level slog.Leveler) *HandlerOptions

NewHandlerOptions creates a HandlerOptions that contains reasonable default values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL