zlg

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2020 License: Apache-2.0 Imports: 5 Imported by: 0

README

zlg (zerolog-gcp)

API reference GitHub

zlg is a (hopefully) straightforward LevelWriter for using zerolog with Google Cloud Operations Logging, which used to be named Stackdriver.

Some notable features:

  • The first log written to Cloud Logging is a slow, blocking write to confirm connectivity + permissions, but all subsequent writes are non-blocking.
  • Handles converting zerolog.WarnLevel to logging.Warning.
  • Zerolog's trace level maps to Cloud Logging's Default level.
  • Cloud Logging's Alert and Emergency levels are not used.
  • Ensure that all zlg-created loggers are flushed before program exit with defer zlg.Flush()

Getting Started

The usual cases

Logging only to Stackdriver:

gcpWriter, err := zlg.NewCloudLoggingWriter(ctx, projectID, logID, zlg.CloudLoggingOptions{})
if err != nil {
    log.Panic().Err(err).Msg("could not create a CloudLoggingWriter")
}
log.Logger = log.Output(gcpWriter)

For non-GCP-hosted situations, you can log to both the console and GCP without much additional fuss.

gcpWriter, err := zlg.NewCloudLoggingWriter(ctx, projectID, logID, zlg.CloudLoggingOptions{})
if err != nil {
    log.Panic().Err(err).Msg("could not create a CloudLoggingWriter")
}
log.Logger = log.Output(zerolog.MultiLevelWriter(
    zerolog.NewConsoleWriter(), 
    gcpWriter,
))

To ensure that the last asynchronous logs are delievered to Cloud Logging, zlg keeps a reference to all logging.Logger structs that zlg itself creates. If memory leaks of loggers are a concern, consider specifying providing a logger client via zlg.CloudLoggingOptions instead.

gcpWriter, err := zlg.NewCloudLoggingWriter(ctx, projectID, logID, zlg.CloudLoggingOptions{})
if err != nil {
    log.Panic().Err(err).Msg("could not create a CloudLoggingWriter")
}
defer zlg.Flush()
doEverythingElse()

More advanced usage involves a non-empty zlg.CloudLoggingOptions.

Documentation

Index

Constants

This section is empty.

Variables

DefaultSeverityMap contains the default zerolog.Level -> logging.Severity mappings.

Functions

func Flush

func Flush()

Flush blocks while flushing all loggers this module created.

Types

type CloudLoggingOptions

type CloudLoggingOptions struct {
	// SeverityMap can be optionally specified to use instead of DefaultSeverityMap.
	SeverityMap map[zerolog.Level]logging.Severity

	// Logger can be optionally provided in lieu of constructing a logger on the caller's behalf.
	Logger *logging.Logger

	// LoggerOptions is optionally used to construct a Logger.
	LoggerOptions []logging.LoggerOption
}

CloudLoggingOptions specifies some optional configuration.

type CloudLoggingWriter

type CloudLoggingWriter struct {
	OnError func(err error)

	zerolog.LevelWriter
	io.Closer
	// contains filtered or unexported fields
}

func NewCloudLoggingWriter

func NewCloudLoggingWriter(ctx context.Context, projectID, logID string, opts CloudLoggingOptions) (writer *CloudLoggingWriter, err error)

NewCloudLoggingWriter creates a LevelWriter that logs only to GCP Cloud Logging using non-blocking calls.

func (*CloudLoggingWriter) Close added in v0.2.2

func (c *CloudLoggingWriter) Close() error

func (*CloudLoggingWriter) Write

func (c *CloudLoggingWriter) Write(p []byte) (int, error)

func (*CloudLoggingWriter) WriteLevel

func (c *CloudLoggingWriter) WriteLevel(level zerolog.Level, payload []byte) (int, error)

Jump to

Keyboard shortcuts

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