zlg

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2022 License: Apache-2.0 Imports: 4 Imported by: 1

README

zlg

GitHub

zlg is a fork of Mark Ignacio's zerolog-gcp project which creates a LevelWriter for using zerolog with Google Cloud Operations Logging (the logging system formerly known as Stackdriver).

Some notable features and changes from zerolog-gcp:

  • Creation of the logging.Logger and logging.Client has been separated. Users can now call NewWriter or NewWriterWithClient. The first call will create a default client versus passing an instantiated client. If zlg creates the client, it will execute the logging.Client.Ping function to ensure ability to connect to GCP Cloud Logging.
  • All writes are non-blocking.
  • Handles converting zerolog.WarnLevel to logging.Warning.
  • Zerolog's TraceLevel and NoLevel maps to Cloud Logging's Default severity.
  • Zerolog's FatalLevel maps to Alert severity by default.
  • Ensure that all zlg-created clients are closed before program exit with defer zlg.Close(). A logging.Client close will flush all associated loggers.

Getting Started

The usual cases

Logging only to GCP Cloud Logging:

import "github.com/mshindle/zlg"

// [...]

gcpWriter, err := zlg.NewWriter(ctx, parent, logID, opts...)
if err != nil {
    log.Panic().Err(err).Msg("could not create a GCP Cloud Logging writer")
}
log.Logger = log.Output(gcpWriter)

As zlg.NewWriter creates a zerolog.LevelWriter, all of zerolog's features are available such as creating MultiLevel writers.

gcpWriter, err := zlg.NewWriter(ctx, parent, logID, opts...)
if err != nil {
    log.Panic().Err(err).Msg("could not create a GCP Cloud Logging writer")
}
log.Logger = log.Output(zerolog.MultiLevelWriter(
    gcpWriter,
	zerolog.New(os.Stderr)
))

To ensure that the last asynchronous logs are delivered to Cloud Logging, zlg keeps a reference to all logging.Client entities that zlg itself creates. Close() should be called on the package to ensure all created clients are flushed before termination.

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

Documentation

Index

Constants

This section is empty.

Variables

Functions

func Close

func Close()

Close waits for all opened loggers for all clients to be flushed. Once flushed, the client is then closed as well.

func NewWriter

func NewWriter(ctx context.Context, parent, logID string, opts ...logging.LoggerOption) (zerolog.LevelWriter, error)

NewWriter creates a LevelWriter that logs only to GCP Cloud Logging using non-blocking calls. Writer is created using default client options. Once logging.Client is created, NewWriter will execute logging.Client.Ping to ensure application can use GCP Cloud Logging. If a specific logging client is needed, use NewWriterWithClient instead.

func NewWriterWithClient

func NewWriterWithClient(client *logging.Client, logID string, opts ...logging.LoggerOption) (zerolog.LevelWriter, error)

NewWriterWithClient instantiates a logging.Logger that will write entries with the given log ID, such as "syslog". A log ID must be less than 512 characters long and can only include the following characters: upper and lower case alphanumeric characters: [A-Za-z0-9]; and punctuation characters: forward-slash, underscore, hyphen, and period.

Types

This section is empty.

Jump to

Keyboard shortcuts

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