Coder Social home page Coder Social logo

logz's Introduction

Logz - yet another logger library for go

CI for Pull Request Godoc Go Report Card GitHub license

The logz is logger library for grouping application logs related a access log. The logz uses OpenTelemetry(https://opentelemetry.io) to generate the trace id.
This is for Google Cloud Logging (formerly known as Stackdriver Logging).

screenshot

Features

  • Writes access log each http requests
  • Writes application log
  • Grouping application logs related a access log.
    • The parent entry will inherit the severity of its children
  • Supports to App Engine 2nd and Cloud Run and GKE.

Use go111 package if your project is App Engine 1st generation.

Contribution Packages

The logz contribution packages that provides middlewares for 3rd-party Go packages.

For more details: https://github.com/glassonion1/logz/tree/main/contrib

Install

$ go get github.com/glassonion1/logz

Usage

Google App Engine Standard

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        ctx := r.Context()

        // Writes info log
        logz.Infof(ctx, "writes %s log", "info")
    })

    logz.SetConfig(logz.Config{
        NeedsAccessLog: true, // Writes the access log
    })
    logz.InitTracer()
    // Sets the middleware
    h := middleware.NetHTTP("tracer name")(mux)

    log.Fatal(http.ListenAndServe(":8080", h))
}

Cloud Run

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        ctx := r.Context()

        // Writes info log
        logz.Infof(ctx, "writes %s log", "info")
    })

    logz.SetConfig(logz.Config{
        ProjectID:       "your gcp project id",
        NeedsAccessLog: false, // Writes no access log
    })
    logz.InitTracer()
    // Sets the middleware
    h := middleware.NetHTTP("tracer name")(mux)

    log.Fatal(http.ListenAndServe(":8080", h))
}

GKE

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        ctx := r.Context()

        // Writes info log
        logz.Infof(ctx, "writes %s log", "info")
    })

    logz.SetConfig(logz.Config{
        ProjectID:       "your gcp project id",
        NeedsAccessLog: true, // Writes the access log
    })
    logz.InitTracer()
    // Sets the middleware
    h := middleware.NetHTTP("tracer name")(mux)

    log.Fatal(http.ListenAndServe(":8080", h))
}

Examples

See this sample projects for logz detailed usage
https://github.com/glassonion1/logz/tree/main/example

How logs are grouped

The logz leverages the grouping feature of GCP Cloud Logging. See following references for more details.

Log format

The log format is based on LogEntry's structured payload

Application log format

{
  "severity":"INFO",
  "message":"writes info log",
  "time":"2020-12-31T23:59:59.999999999Z",
  "logging.googleapis.com/sourceLocation":{
    "file":"logger_test.go",
    "line":"57",
    "function":"github.com/glassonion1/logz/internal/logger_test.TestLoggerWriteApplicationLog.func3"
  },
  "logging.googleapis.com/trace":"projects/test/traces/00000000000000000000000000000000",
  "logging.googleapis.com/spanId":"0000000000000000",
  "logging.googleapis.com/trace_sampled":false
}

Access log format

{
  "severity":"DEFAULT",
  "time":"2020-12-31T23:59:59.999999999Z",
  "logging.googleapis.com/trace":"projects/test/traces/a0d3eee13de6a4bbcf291eb444b94f28",
  "httpRequest":{
    "requestMethod":"GET",
    "requestUrl":"/test1",
    "requestSize":"0",
    "status":200,
    "responseSize":"333",
    "remoteIp":"192.0.2.1",
    "serverIp":"192.168.100.115",
    "latencyy":{
      "nanos":100,
      "seconds":0
    },
    "protocol":"HTTP/1.1"
  }
}

logz's People

Contributors

glassonion1 avatar karupanerura avatar

Watchers

James Cloos avatar  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.