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 ¶
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 ¶
Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.
func (*Handler) WithAttrs ¶
WithAttrs returns a new Handler whose attributes consists of h's attributes followed by attrs.
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.