zapcloudwatch

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: BSD-3-Clause Imports: 11 Imported by: 0

README

zapcloudwatch

High performance (buffered) zap Core for CloudWatch Log.

GoDoc

Installation

go get -u github.com/uschen/zapstackdriver

Quick Start

logGroup := "YOUR-LOG-GROUP"
logStream := "YOUR-LOG-STREAM"

core, err := zapcloudwatch.NewCore(
  zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
  logGroup,
  logStream,
  zap.InfoLevel,
  zapcloudwatch.WithOnError(func(err error) {
    fmt.Printf("got error: %s", err)
  }),
)
if err != nil {
  panic(err)
}

logger := zap.New(core)
logger.Info("test message", zap.String("string_field", "string value"), zap.Int64("int64_field", 123))
logger.Sync() // flushes the logs to CloudWatch Log

l2 := logger.With(zap.String("sub_logger", "logger2"))
l2.Info("sub logger message", zap.Any("struct_value", struct {
  A string
  B string
}{
  A: "value a",
  B: "value b",
}))

// flush
if err := core.Close(); err != nil {
  panic(err)
}

Contributing


Released under the BSD 3-Clause License.

Documentation

Overview

Example (Custom_client)
package main

import (
	"context"
	"fmt"

	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
	"github.com/uschen/zapcloudwatch"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {
	logGroup := "YOUR-LOG-GROUP"
	logStream := "YOUR-LOG-STREAM"
	awsConfigProfile := "YOUR-PROFILE-NAME"

	cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigProfile(awsConfigProfile))
	if err != nil {
		panic("configuration error: " + err.Error())
	}
	cl := cloudwatchlogs.NewFromConfig(cfg)

	core, err := zapcloudwatch.NewCore(
		zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
		logGroup,
		logStream,
		zap.InfoLevel,
		zapcloudwatch.WithCloudWatchClient(cl),
		zapcloudwatch.WithOnError(func(err error) {
			fmt.Printf("got error: %s", err)
		}),
	)
	if err != nil {
		panic(err)
	}

	logger := zap.New(core)
	logger.Debug("this is a debug message which will not be logged due to INFO zapcore.LevelEnabler passed above")
	logger.Info("test message", zap.String("string_field", "string value"), zap.Int64("int64_field", 123))
	logger.Sync() // flush the logs to CloudWatch Log

	l2 := logger.With(zap.String("sub_logger", "logger2"))
	l2.Info("sub logger message", zap.Any("struct_value", struct {
		A string
		B string
	}{
		A: "value a",
		B: "value b",
	}))

	// flush
	if err := core.Close(); err != nil {
		panic(err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrOverflow signals that the number of buffered entries for a Logger
	// exceeds its BufferLimit.
	ErrOverflow = bundler.ErrOverflow

	// ErrOversizedEntry signals that an entry's size exceeds the maximum number of
	// bytes that will be sent in a single call to the logging service.
	ErrOversizedEntry = bundler.ErrOversizedItem
)

Functions

This section is empty.

Types

type Core

type Core struct {
	zapcore.LevelEnabler
	// contains filtered or unexported fields
}

Core zapcore.Core sinks to Amazon CloudWatch Log

func NewCore

func NewCore(enc zapcore.Encoder, logGroupName, logStreamName string, enab zapcore.LevelEnabler, options ...OptionFunc) (*Core, error)

func (*Core) Check

func (cc *Core) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry

func (*Core) Close

func (cc *Core) Close() error

Close flushes and return flush error.

func (*Core) Level

func (cc *Core) Level() zapcore.Level

func (*Core) Sync

func (cc *Core) Sync() error

func (*Core) With

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

func (*Core) Write

func (cc *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error

type OptionFunc

type OptionFunc func(*Core, *params) error

OptionFunc -

func WithBundlerBufferedByteLimit

func WithBundlerBufferedByteLimit(limit int) OptionFunc

WithBundlerBufferedByteLimit The maximum number of bytes that the Bundler will keep in memory before returning ErrOverflow. The default is bundler.DefaultBufferedByteLimit

func WithBundlerBundleByteLimit

func WithBundlerBundleByteLimit(limit int) OptionFunc

WithBundlerBundleByteLimit The maximum size of a bundle, in bytes. Zero means unlimited.

func WithBundlerBundleByteThreshold

func WithBundlerBundleByteThreshold(threshold int) OptionFunc

WithBundlerBundleByteThreshold Once the number of bytes in current bundle reaches this threshold, handle the bundle. The default is bundler.DefaultBundleByteThreshold. This triggers handling, but does not cap the total size of a bundle.

func WithBundlerBundleCountThreshold

func WithBundlerBundleCountThreshold(threshold int) OptionFunc

WithBundlerBundleCountThreshold Once a bundle has this many items, handle the bundle. Since only one item at a time is added to a bundle, no bundle will exceed this threshold, so it also serves as a limit. The default is bundler.DefaultBundleCountThreshold.

func WithBundlerDelayThreshold

func WithBundlerDelayThreshold(threshold time.Duration) OptionFunc

WithBundlerDelayThreshold Starting from the time that the first message is added to a bundle, once this delay has passed, handle the bundle. The default is bundler.DefaultDelayThreshold

func WithBundlerHandlerLimit

func WithBundlerHandlerLimit(limit int) OptionFunc

WithBundlerHandlerLimit The maximum number of handler invocations that can be running at once. The default is 1.

func WithCloudWatchClient

func WithCloudWatchClient(client *cloudwatchlogs.Client) OptionFunc

WithCloudWatchClient passes existing cloudwatchlogs.Client

func WithCreateLogGroup

func WithCreateLogGroup(create bool) OptionFunc

WithCreateLogGroup whether to create log group if it doesn't exist. Default is false.

func WithCreateLogStream

func WithCreateLogStream(create bool) OptionFunc

WithCreateLogStream whether to create log stream if it doesn't exist. Default is false.

func WithOnError

func WithOnError(onError func(error)) OptionFunc

WithOnError OnError is called when an error occurs in a call to Log or Flush.

Jump to

Keyboard shortcuts

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