logrus_cloudwatchlogs

package module
v0.0.0-...-e626e3f Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2021 License: MIT Imports: 16 Imported by: 0

README

Cloud Watch Logs hook for Logrus godoc reference

This fork of logrus-cloudwatchlogs:

Use this hook to send your Logrus logs to Amazon's Cloud Watch Logs.

Options

The formatter has options available to it. Please check the godoc.

Example

Look in the examples directory for more examples.

package main

import (
	"io/ioutil"
	"log"
	"os"

	"github.com/sirupsen/logrus"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/kdar/logrus-cloudwatchlogs"
)

func main() {
	group := os.Getenv("AWS_CLOUDWATCHLOGS_GROUP_NAME")
	stream := os.Getenv("AWS_CLOUDWATCHLOGS_STREAM_NAME")

	// logs.us-east-1.amazonaws.com
	// Define the session - using SharedConfigState which forces file or env creds
    	sess, err := session.NewSessionWithOptions(session.Options{
    		SharedConfigState: session.SharedConfigEnable,
    		Config:            aws.Config{Region: aws.String("us-east-1")},
    	})
    	if err != nil {
    		panic("Not going to be able to write to cloud watch if you cant create a session")
    	}
    
    	// Determine if we are authorized to access AWS with the credentials provided. This does not mean you have access to the
    	// services required however.
    	_, err = sts.New(sess).GetCallerIdentity(&sts.GetCallerIdentityInput{})
    	if err != nil {
    		panic("Couldn't Validate our aws credentials")
    	}

	hook, err := logrus_cloudwatchlogs.NewHook(group, stream, sess)
	if err != nil {
		log.Fatal(err)
	}

	l := logrus.New()
	l.Hooks.Add(hook)
	l.Out = ioutil.Discard
	l.Formatter = logrus_cloudwatchlogs.NewProdFormatter()

	l.WithFields(logrus.Fields{
		"event": "testevent",
		"topic": "testtopic",
		"key":   "testkey",
	}).Fatal("Some fatal event")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DevFormatter

type DevFormatter struct {
	HTTPRequestKey string
	*logrus.TextFormatter
}

func (*DevFormatter) Format

func (f *DevFormatter) Format(entry *logrus.Entry) ([]byte, error)

type Hook

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

func NewHook

func NewHook(groupName, streamName string, sess *session.Session, batchFrequency time.Duration) (*Hook, error)

NewHook returns a new CloudWatch hook.

CloudWatch log events are sent in batches on an interval, batchFrequency. Pass 0 to send events synchronously.

func (*Hook) Fire

func (h *Hook) Fire(entry *logrus.Entry) error

Fire sends the given entry to CloudWatch.

func (*Hook) Levels

func (h *Hook) Levels() []logrus.Level

func (*Hook) WithFields

func (h *Hook) WithFields(fields logrus.Fields) *Hook

WithFields includes the given fields to log entries sent to CloudWatch.

func (*Hook) WithFormatter

func (h *Hook) WithFormatter(formatter logrus.Formatter) *Hook

WithFormatter uses the given formatter to format log entries sent to CloudWatch.

func (*Hook) Write

func (h *Hook) Write(p []byte) (n int, err error)

Write sends the given bytes to CloudWatch.

type Marshaler

type Marshaler interface {
	MarshalLog() map[string]interface{}
}

Marshaler is an interface any type can implement to change its output in our production logs.

type ProdFormatter

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

func NewProdFormatter

func NewProdFormatter(options ...ProdFormatterOption) *ProdFormatter

NewProdFormatter creates a new cloudwatchlogs production formatter. This is opinionated and you can feel free to create your own.

func (*ProdFormatter) Format

func (f *ProdFormatter) Format(entry *logrus.Entry) ([]byte, error)

Format formats logrus.Entry in the form of: [timestamp] [jsondata]

type ProdFormatterOption

type ProdFormatterOption func(*ProdFormatter) error

func AppName

func AppName(name string) ProdFormatterOption

AppName is a formatter option that specifies the name of the app. If this is not specified, the default is to use the executable name.

func HTTPRequest

func HTTPRequest(key string, headerFilter ...string) ProdFormatterOption

HTTPRequest is a formatter option that allows you to indicate that a certain key will contain an *http.Request. If it does, it will be formatted in the output. You can provide an optional list of keys to filter out of the serialized header. This is useful so you don't include sensitive information (like the authorization header). Note: if you do not provide this and you pass in an *http.Request, it will fail because encoding/json cannot serialize *http.Request.

func Hostname

func Hostname(name string) ProdFormatterOption

Hostname is a formatter option that specifies the hostname this program is running on. If this is not specified, the system's hostname will be used.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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