Skip to content

Latest commit

 

History

History
68 lines (46 loc) · 2.3 KB

File metadata and controls

68 lines (46 loc) · 2.3 KB

slogan

Documentation Release

slogan is a Go module that provides a slog Handler implementation for submitting log records to Azure Application Insights.

Prerequisites

This module requires Go version 1.23 or higher.

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 appinsights 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)
}

CLI

A command line program appinsights is available for sending the lines you type to Azure Application Insights. The following command will install the program.

go install github.com/openclosed-dev/slogan/cmd/appinsights

Before invoking the command, you must define your Application Insights connection string in the environment variable APPLICATIONINSIGHTS_CONNECTION_STRING

Known Issue

This module depends on the module github.com/microsoft/ApplicationInsights-Go developed by Microsoft, that is not actively maintained for several years. While there seems to be no functional problem in the module, we need to constantly check the status of the project.