Coder Social home page Coder Social logo

isabella232 / logrus-stackdriver-formatter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jenkins-x/logrus-stackdriver-formatter

0.0 0.0 0.0 52 KB

Stackdriver formatter for logrus

License: MIT License

Go 34.99% Makefile 25.93% Shell 39.08%

logrus-stackdriver-formatter's Introduction

logrus-stackdriver-formatter

logrus formatter for Stackdriver.

In addition to supporting level-based logging to Stackdriver, for Error, Fatal and Panic levels it will append error context for Error Reporting.

Installation

go get -u github.com/jenkins-x/logrus-stackdriver-formatter

Usage

package main

import (
    "github.com/sirupsen/logrus"
    stackdriver "github.com/jenkins-x/logrus-stackdriver-formatter"
)

var log = logrus.New()

func init() {
    log.Formatter = stackdriver.NewFormatter(
        stackdriver.WithService("your-service"), 
        stackdriver.WithVersion("v0.1.0"),
    )
    log.Level = logrus.DebugLevel

    log.Info("ready to log!")
}

Here's a sample entry (prettified) from the example:

{
  "serviceContext": {
    "service": "test-service",
    "version": "v0.1.0"
  },
  "message": "unable to parse integer: strconv.ParseInt: parsing \"text\": invalid syntax",
  "severity": "ERROR",
  "context": {
    "reportLocation": {
      "filePath": "github.com/jenkins-x/logrus-stackdriver-formatter/example_test.go",
      "lineNumber": 21,
      "functionName": "ExampleLogError"
    }
  }
}

Here's an example test:

package stackdriver

import (
        "os"
        "strconv"

        stackdriver "github.com/jenkins-x/logrus-stackdriver-formatter"
        "github.com/sirupsen/logrus"
)

func ExampleLogError() {
        logger := logrus.New()
        logger.Out = os.Stdout
        logger.Formatter = stackdriver.NewFormatter(
                stackdriver.WithService("test-service"),
                stackdriver.WithVersion("v0.1.0"),
        )

        logger.Info("application up and running")

        _, err := strconv.ParseInt("text", 10, 64)
        if err != nil {
                logger.WithError(err).Errorln("unable to parse integer")
        }

        // Output:
        // {"message":"application up and running","severity":"INFO","context":{}}
        // {"serviceContext":{"service":"test-service","version":"v0.1.0"},"message":"unable to parse integer: strconv.ParseInt: parsing \"text\": invalid syntax","severity":"ERROR","context":{"reportLocation":{"filePath":"github.com/jenkins-x/logrus-stackdriver-formatter/example_test.go","lineNumber":23,"functionName":"ExampleLogError"}}}
}

HTTP request context

If you'd like to add additional context like the httpRequest, here's a convenience function for creating a HTTP logger:

func httpLogger(logger *logrus.Logger, r *http.Request) *logrus.Entry {
    return logger.WithFields(logrus.Fields{
        "httpRequest": map[string]interface{}{
            "method":    r.Method,
            "url":       r.URL.String(),
            "userAgent": r.Header.Get("User-Agent"),
            "referrer":  r.Header.Get("Referer"),
        },
    })
}

Then, in your HTTP handler, create a new context logger and all your log entries will have the HTTP request context appended to them:

func handler(w http.ResponseWriter, r *http.Request) {
    httplog := httpLogger(log, r)
    // ...
    httplog.Infof("Logging with HTTP request context")
}

logrus-stackdriver-formatter's People

Contributors

cagiti avatar marcusolsson avatar ankitm123 avatar garethjevans avatar jenkins-x-bot avatar aluzzardi avatar datamob avatar jsageryd avatar tglander 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.