zapcloudwatchcore

package module
v0.0.0-...-135478c Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2020 License: MIT Imports: 7 Imported by: 0

README

Cloudwatch core for zap

Example

package main

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

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"github.com/vmlellis/zapcloudwatchcore"
)

func getConsoleCore() zapcore.Core {
	level := zap.NewAtomicLevelAt(zapcore.DebugLevel)
	encoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
	consoleDebugging := zapcore.Lock(os.Stdout)
	return zapcore.NewCore(encoder, consoleDebugging, level)
}

func getCloudwatchCore() (*zapcore.Core, error) {
	cred := credentials.NewStaticCredentials(
		os.Getenv("AWS_ACCESS_KEY"),
		os.Getenv("AWS_SECRET_KEY"),
		os.Getenv("AWS_TOKEN"),
	)
	awsCfg := aws.NewConfig().WithRegion(os.Getenv("AWS_REGION")).WithCredentials(cred)

	cloudWatchParams := zapcloudwatchcore.NewCloudwatchCoreParams{
		GroupName:    os.Getenv("CLOUDWATCH_GROUP_NAME"),
		StreamName:   os.Getenv("CLOUDWATCH_STREAM_NAME"),
		IsAsync:      false,
		Config:       awsCfg,
		Level:        zapcore.InfoLevel,
		LevelEnabler: zap.NewAtomicLevelAt(zapcore.InfoLevel),
		Enc:          zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
		Out:          zapcore.AddSync(ioutil.Discard),
	}

	core, err := zapcloudwatchcore.NewCloudwatchCore(&cloudWatchParams)
	if err != nil {
		log.Printf("can't initialize cloudwatch logger: %v", err)
		return nil, err
	}

	return &core, nil
}

func getLogger(name string) *zap.Logger {
	consoleCore := getConsoleCore()

	cloudwatchCore, err := getCloudwatchCore()

	if err != nil {
		return zap.New(consoleCore).Named(name)
	}

	core := zapcore.NewTee(consoleCore, *cloudwatchCore)

	return zap.New(core).Named(name)
}

func main() {
	logger := getLogger("test")

	logger.Debug("don't need to send a message")
	logger.Error("an error happened!")
}

Install

$ go get -u github.com/vmlellis/zapcloudwatch

Author

Victor Lellis

Documentation

Index

Constants

This section is empty.

Variables

AllLevels Supported log levels

Functions

func LevelThreshold

func LevelThreshold(l zapcore.Level) []zapcore.Level

LevelThreshold - Returns every logging level above and including the given parameter.

func NewCloudwatchCore

func NewCloudwatchCore(params *NewCloudwatchCoreParams) (zapcore.Core, error)

Types

type CloudwatchCore

type CloudwatchCore struct {
	// Messages with a log level not contained in this array
	// will not be dispatched. If nil, all messages will be dispatched.
	AcceptedLevels []zapcore.Level
	GroupName      string
	StreamName     string
	AWSConfig      *aws.Config

	Async bool // if async is true, send a message asynchronously.

	zapcore.LevelEnabler
	// contains filtered or unexported fields
}

CloudwatchCore is a zap Core for dispatching messages to the specified

func (*CloudwatchCore) Check

func (*CloudwatchCore) Levels

func (c *CloudwatchCore) Levels() []zapcore.Level

Levels sets which levels to sent to cloudwatch

func (*CloudwatchCore) Sync

func (c *CloudwatchCore) Sync() error

func (*CloudwatchCore) With

func (c *CloudwatchCore) With(fields []zapcore.Field) zapcore.Core

func (*CloudwatchCore) Write

func (c *CloudwatchCore) Write(ent zapcore.Entry, fields []zapcore.Field) error

type NewCloudwatchCoreParams

type NewCloudwatchCoreParams struct {
	GroupName    string
	StreamName   string
	IsAsync      bool
	Config       *aws.Config
	Level        zapcore.Level
	Enc          zapcore.Encoder
	Out          zapcore.WriteSyncer
	LevelEnabler zapcore.LevelEnabler
}

Jump to

Keyboard shortcuts

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