GithubHelp home page GithubHelp logo

mark-ignacio / zerolog-gcp Goto Github PK

View Code? Open in Web Editor NEW
19.0 4.0 2.0 80 KB

a straightforward GCP Cloud Logging (née Stackdriver) writer for github.com/rs/zerolog.

License: Apache License 2.0

Go 100.00%
zerolog stackdriver-logs stackdriver gcp

zerolog-gcp's Introduction

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.

zerolog-gcp's People

Contributors

dependabot[bot] avatar mark-ignacio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

zerolog-gcp's Issues

sync.Once

Just a small suggestion... use sync.Once. =)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.