stackdriver

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2019 License: MIT Imports: 10 Imported by: 0

README

logrus-stackdriver-formatter

Build Status Go Report Card GoDoc License MIT

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/TV4/logrus-stackdriver-formatter

Usage

package main

import (
    "github.com/sirupsen/logrus"
    stackdriver "github.com/TV4/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/TV4/logrus-stackdriver-formatter/example_test.go",
      "lineNumber": 21,
      "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")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Formatter

type Formatter struct {
	Service       string
	Version       string
	StackSkip     []string
	SkipTimestamp bool
	RegexSkip     string
}

Formatter implements Stackdriver formatting for logrus.

func NewFormatter

func NewFormatter(options ...Option) *Formatter

NewFormatter returns a new Formatter.

func (*Formatter) Format

func (f *Formatter) Format(e *logrus.Entry) ([]byte, error)

Format formats a logrus entry according to the Stackdriver specifications.

type Logger added in v0.1.1

type Logger struct {
	Logger *logrus.Logger
}

Logger is a gokit-compatible wrapper for logrus.Logger

func NewStackdriverLogger added in v0.1.1

func NewStackdriverLogger(w io.Writer, opts ...Option) *Logger

NewStackdriverLogger creates a gokit-compatible logger

func (Logger) Log added in v0.1.1

func (l Logger) Log(kvs ...interface{}) error

Log creates a log event from keyvals, a variadic sequence of alternating keys and values.

func (Logger) NewEntry added in v0.1.1

func (l Logger) NewEntry(kvs ...interface{}) *logrus.Entry

NewEntry creates a new logrus entry

type Option

type Option func(*Formatter)

Option lets you configure the Formatter.

func WithRegexSkip added in v0.1.1

func WithRegexSkip(v string) Option

WithRegexSkip lets you configure which functions or packages should be skipped for locating the error.

func WithService

func WithService(n string) Option

WithService lets you configure the service name used for error reporting.

func WithSkipTimestamp added in v0.1.1

func WithSkipTimestamp() Option

WithSkipTimestamp lets you avoid setting the timestamp

func WithStackSkip

func WithStackSkip(v string) Option

WithStackSkip lets you configure which packages should be skipped for locating the error.

func WithVersion

func WithVersion(v string) Option

WithVersion lets you configure the service version used for error reporting.

type ReportLocation added in v0.1.1

type ReportLocation struct {
	FilePath     string `json:"filePath,omitempty"`
	LineNumber   int    `json:"lineNumber,omitempty"`
	FunctionName string `json:"functionName,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL