logrus_cloudwatchlogs

package module
v0.0.0-...-22c5437 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2023 License: MIT Imports: 17 Imported by: 0

README

Cloud Watch Logs hook for Logrus godoc reference

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 NewBatchingHook

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

func NewHook

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

func NewHookWithDuration

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

func (*Hook) Fire

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

func (*Hook) Levels

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

func (*Hook) Write

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

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.

type WriterHook

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

WriterHook is a hook that just outputs to an io.Writer. This is useful because our formatter outputs the file and line where it was called, and the callstack for a hook is different from the callstack for just writing to logrus.Logger.Out.

func NewWriterHook

func NewWriterHook(w io.Writer) *WriterHook

func (*WriterHook) Fire

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

func (*WriterHook) Levels

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

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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