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 ¶
- Variables
- type Core
- func (cc *Core) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry
- func (cc *Core) Close() error
- func (cc *Core) Level() zapcore.Level
- func (cc *Core) Sync() error
- func (cc *Core) With(fields []zapcore.Field) zapcore.Core
- func (cc *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error
- type OptionFunc
- func WithBundlerBufferedByteLimit(limit int) OptionFunc
- func WithBundlerBundleByteLimit(limit int) OptionFunc
- func WithBundlerBundleByteThreshold(threshold int) OptionFunc
- func WithBundlerBundleCountThreshold(threshold int) OptionFunc
- func WithBundlerDelayThreshold(threshold time.Duration) OptionFunc
- func WithBundlerHandlerLimit(limit int) OptionFunc
- func WithCloudWatchClient(client *cloudwatchlogs.Client) OptionFunc
- func WithCreateLogGroup(create bool) OptionFunc
- func WithCreateLogStream(create bool) OptionFunc
- func WithOnError(onError func(error)) OptionFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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
type OptionFunc ¶
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.