zlg

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2023 License: Apache-2.0 Imports: 6 Imported by: 5

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.
  • Flushes all logs before a .Fatal() or .Panic()
  • Ensure that all zlg-created loggers are flushed before program exit with defer zlg.Flush()
  • Overridable Cloud Monitoring levels (zerolog -> Cloud Logging):
    • Trace -> Default
    • Warn -> Warning
    • Cloud Logging's Alert and Emergency levels are not used.

Getting Started

The usual cases

Logging only to Stackdriver:

import zlg "github.com/mark-ignacio/zerolog-gcp"

// [...]

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 stdout/stderr 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 delivered 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, which allows for log level and GCP client customization.

Documentation

Index

Constants

This section is empty.

Variables

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

Functions

func Flush added in v0.2.0

func Flush() []error

Flush blocks while flushing all loggers this module created.

func NewCloudLoggingWriter

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

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

Types

type CloudLoggingOptions

type CloudLoggingOptions struct {
	// Specify this to override DefaultSeverityMap.
	SeverityMap map[zerolog.Level]logging.Severity

	// Used during *logging.Client construction.
	ClientOptions []option.ClientOption

	// Used during *logging.Client construction.
	ClientOnError func(error)

	// Specify this to override the default of constructing a *logging.Logger on the caller's behalf.
	Logger *logging.Logger

	// Used during GCP Logger construction.
	LoggerOptions []logging.LoggerOption
}

CloudLoggingOptions specifies some optional configuration.

Jump to

Keyboard shortcuts

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