Skip to content

A logrus hook for sending logs to AWS CloudWatch. Uses V2 of the Go AWS SDK.

License

Notifications You must be signed in to change notification settings

innix/logrus-cloudwatch

Repository files navigation

Logrus CloudWatch

Go Reference

A logrus hook for sending log events to AWS CloudWatch.

Install

go get github.com/innix/logrus-cloudwatch

Usage

This hook uses the AWS SDK for Go V2. If your project is using the legacy V1 SDK, you can still use this hook but it means your project will have a dependency on both versions of the SDK.

package main

import (
	"context"
	"time"

	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
	"github.com/innix/logrus-cloudwatch"
	log "github.com/sirupsen/logrus"
)

func main() {
	// Setup AWS SDK client.
	cfg, err := config.LoadDefaultConfig(context.Background())
	if err != nil {
		log.Fatalf("Could not load AWS config: %v", err)
	}
	client := cloudwatchlogs.NewFromConfig(cfg)

	// Create and register hook.
	hook, err := logruscloudwatch.New(client, nil)
	if err != nil {
		log.Fatalf("Could not create CloudWatch hook: %v", err)
	}
	log.AddHook(hook)

	// Do some logging.
	for i := 0; i < 10; i++ {
		log.WithField("counter", i).Info("Incremented counter.")
		time.Sleep(time.Second * 2)
	}

	// Ensure all logs are sent to CloudWatch before the program exits.
	<-hook.Stop()
}

Specify log group name

The Options struct contains fields for configuring how the hook works, including which CloudWatch log group to write to.

// Create and register hook.
hook, err := logruscloudwatch.New(client, &logruscloudwatch.Options{
    LogGroupName: "/my-project/development",
})
if err != nil {
    log.Fatalf("Could not create CloudWatch hook: %v", err)
}
log.AddHook(hook)

Test

To run the unit tests:

go test ./...

Lint

The project uses golangci-lint for linting. See the .golangci.yml file for configured rules. To run the linter:

golangci-lint run

About

A logrus hook for sending logs to AWS CloudWatch. Uses V2 of the Go AWS SDK.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages