Code Monkey home page Code Monkey logo

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

Watchers

 avatar

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.