logformatter

package module
v0.0.0-...-60fe426 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2020 License: MIT Imports: 13 Imported by: 2

README

go-logformatter

Formatter for go-based packages (gin, gorm, application logs) to stackdriver

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatStack

func FormatStack(err error) (buffer []byte)

Adapt from https://github.com/googleapis/google-cloud-go/issues/1084#issuecomment-474565019 FormatStack formats the error object to adhere to runtime.Stack required by the stackdriver errorreporting (ref: https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events/report#ReportedErrorEvent) FormatStack should accept the error implements stackTracer interface or the stackframe cannot be retrieved (i.e, return nil). Suggest using with pkg/errors to create the error object.

func GinLogSeverity

func GinLogSeverity(s ltype.LogSeverity) func(*GinLog)

GinLogSeverity sets the severity for the GinLog

func NewGinLogFormatter

func NewGinLogFormatter(Options ...func(*GinLog)) gin.LogFormatter

NewGinLogFormatter takes zero or one GinOption function and applis to GinLog.

Example
// disable default logger(stdout/stderr)
r := gin.New()

r.Use(gin.Recovery())

// customize log severity here
// default to Info
formatter := f.NewGinLogFormatter(f.GinLogSeverity(ltype.LogSeverity_DEBUG))

// config gin with the customize logger
r.Use(gin.LoggerWithFormatter(formatter))

r.GET("/ping", func(c *gin.Context) {
	c.JSON(200, "{message: \"pong\"}")
})

r.Run(":8080")
Output:

Types

type GinLog

type GinLog struct {
	Severity ltype.LogSeverity
}

GinLog represents the configuration to setup gin logger

type Stackdriver

type Stackdriver struct {
	logging.LogEntry
	clouderrorreporting.ErrorEvent
	Payload map[string]interface{}
}

func NewStackdriverFormatter

func NewStackdriverFormatter(service, version string) *Stackdriver
Example
package main

import (
	"os"

	"github.com/pkg/errors"

	log "github.com/sirupsen/logrus"

	f "github.com/twreporter/logformatter"
)

func main() {
	log.SetOutput(os.Stdout)

	// Config stackdriver formatter for example service
	log.SetFormatter(f.NewStackdriverFormatter("example", "test"))

	// Log message with info severity
	log.Info("message")

	// Format the error objecgt in runtime.Stack shape
	log.Error(f.FormatStack(errors.Errorf("error")))
}
Output:

func (*Stackdriver) Format

func (s *Stackdriver) Format(entry *logrus.Entry) ([]byte, error)

func (*Stackdriver) MarshalJSON

func (s *Stackdriver) MarshalJSON() ([]byte, error)

Jump to

Keyboard shortcuts

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