GithubHelp home page GithubHelp logo

hirosassa / zerodriver Goto Github PK

View Code? Open in Web Editor NEW
29.0 2.0 9.0 35 KB

Zerolog based logging library optimized for Cloud Logging (formerly Stackdriver Logging)

License: MIT License

Go 100.00%
zerolog google-cloud cloud-logging cloud-trace structured-logging golang

zerodriver's Introduction

zerodriver

Actions Status: test Actions Status: golangci-lint Go Reference Go Report Card Coverage Status Apache-2.0

Zerolog based logging libary optimized for Cloud Logging (formerly Stackdriver Logging). This package is inspired by Zapdriver.

What is this package?

This package provides simple structured logger optimized for Cloud Logging based on zerolog.

Key features of zerodriver are:

Usage

First of all, initialize a logger.

logger := zerodriver.NewProductionLogger() // production mode (global log level set to `info`)
logger := zerodriver.NewDevelopmentLogger() // development mode (global log level set to `debug`)

Then, write logs by using zerolog based fluent API!

logger.Info().Str("key", "value").Msg("Hello World!")
// output: {"severity":"INFO","key":"value","time":"2009-11-10T23:00:00Z","message":"hello world"}

Here's complete example:

package main

import (
    "github.com/hirosassa/zerodriver"
)

func main() {
    logger := zerodriver.NewProductionLogger()
    logger.Info().Str("key", "value").Msg("hello world")
}

// output: {"severity":"INFO","key":"value","time":"2009-11-10T23:00:00Z","message":"hello world"}

GCP specific fields

If your log follows LogEntry format, you can query logs or create metrics alert easier and efficiently on GCP Cloud Logging console.

HTTP request

To log HTTP related metrics and information, you can use following function

func (e *Event) HTTP(req *HTTPPayload) *zerolog.Event

This feature is forked from zapdriver. You can generate zerodriver.HTTPPayload from http.Request and http.Response using NewHTTP function. Same as zapdriver.NewHTTP, following fields needs to be set manually:

  • ServerIP
  • Latency
  • CacheLookup
  • CacheHit
  • CacheValidatedWithOriginServer
  • CacheFillBytes

Using these feature, you can log HTTP related information as follows,

p := NewHTTP(req, res)
p.Latency = time.Since(start) // add some fields manually
logger.Info().HTTP(p).Msg("request received")

Trace context

To add trace information to your log, you can use TraceContext. The signature of the function is as follows:

func (e *Event) TraceContext(trace string, spanId string, sampled bool, projectID string) *zerolog.Event

You can use this feature as follows:

import	"go.opencensus.io/trace"

span := trace.FromContext(r.Context()).SpanContext()
logger.Info().TraceContext(span.TraceID.String(), span.SpanID.String(), true, "my-project").Msg("trace contexts")

// {"severity":"INFO","logging.googleapis.com/trace":"projects/my-project/traces/00000000000000000000000000000000","logging.googleapis.com/spanId":"0000000000000000","logging.googleapis.com/trace_sampled":true,"message":"trace contexts"}

Labels

You can add any "labels" to your log by following:

logger.Info().Labels(zerodriver.Label("foo", "var")).Msg("labeled log")

// {"severity":"INFO","logging.googleapis.com/labels":{"foo":"var"},"message":"labeled log"}

Operations

You can add additional information about a potentially long-running operation with which a log entry is associated by following function:

func (e *Event) Operation(id, producer string, first, last bool) *zerolog.Event

Log entries with the same id are assumed to be part of the same operation. The producer is an arbitrary identifier that should be globally unique amongst all the logs of all your applications (meaning it should probably be the unique name of the current application). You should set first to true for the first log in the operation, and last to true for the final log of the operation.

Also see, https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogEntryOperation

For readable implementation of operation log, you can use following functions:

func (e *Event) OperationStart(id, producer string) *zerolog.Event
func (e *Event) OperationContinue(id, producer string) *zerolog.Event
func (e *Event) OperationEnd(id, producer string) *zerolog.Event

A concrete example of operation log is as follows:

logger.Info().OperationStart("foo", "bar").Msg("started")
logger.Debug().OperationContinue("foo", "bar").Msg("processing")
logger.Info().OperationEnd("foo", "bar").Msg("done")

zerodriver's People

Contributors

hirosassa avatar k-yomo avatar kitagry avatar mgh87 avatar po3rin avatar

Stargazers

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

Watchers

 avatar  avatar

zerodriver's Issues

Proposal: use Labels command more than once

Currently, Labels returns *zerolog.Event. So, we can't call Labels command more than once.
I want to do it as follows.
Do you think about this?

// prepare common label
commonLogger := logger.Info().Labels(zerodriver.Label("label", "common"))

commonLogger.Labels("phase", "1").Msg("hoge")
commonLogger.Labels("phase", "2").Msg("fuga")

Logging API behavior for error differs

The *.err(err Error) API from zerodriver is not behaving like the one from zerolog.

The execution of the following command

package main

import (
	"github.com/hirosassa/zerodriver"
	"github.com/rs/zerolog/log"
)

func main() {
	driverLog := *zerodriver.NewProductionLogger()
	log.Logger = *driverLog.Logger
	driverLog.Err(nil).Msg("error")
	log.Err(nil).Msg("error")
}

is

{"severity":"ERROR","time":"2022-12-02T16:16:39.642678263+01:00","message":"error"}
{"severity":"INFO","time":"2022-12-02T16:16:39.642704011+01:00","message":"error"}

This is the result of the usage of the wrong API in the abstraction layer at:

e := l.Logger.Error().Err(err)

Which should be replaced with

e := l.Logger.Err(err)

feature request: sentry integration

I think it's common pattern that logging error and sending events to monitoring service like Sentry, when an error happens.

so it's nice if this logger support sentry, like https://github.com/archdx/zerolog-sentry

some requirements image:

  • can enable/disable sentry sending per each .Msg() call
  • automatically set sentry event's error level according to logger's error level
  • works with sentry's additional package like https://docs.sentry.io/platforms/go/guides/http/ (automatically set http request info to sentry events)
  • automatically set logger's label data to sentry events

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.