logrus_stack

package module
v0.0.0-...-89c00d8 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2017 License: MIT Imports: 3 Imported by: 26

README

logrus-stack 🎯

GoDoc

logrus-stack provides facebookgo/stack integration hook for sirupsen/logrus.

Instead of setting file, line, and func name values individually, this hook sets "caller" and/or "stack" objects containing file, line and func name.

The values play well with logrus.TextFormatter{} as well as logrus.JSONFormatter{}. See example outputs below.

Doc

There's not much to it. See usage and GoDoc.

Usage

$ go get github.com/Gurpartap/logrus-stack
logrus.AddHook(logrus_stack.StandardHook())

Same as:

callerLevels := logrus.AllLevels
stackLevels := []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel}

logrus.AddHook(logrus_stack.NewHook(callerLevels, stackLevels))

Example

package main

import (
	"errors"
	"os"

	"github.com/Gurpartap/logrus-stack"
	"github.com/sirupsen/logrus"
)

type Worker struct {
	JobID string
}

func (w Worker) Perform() {
	logrus.WithField("jod_id", w.JobID).Infoln("Now working")

	err := errors.New("I don't know what to do yet")
	if err != nil {
		logrus.Errorln(err)
		return
	}

	// ...
}

func main() {
	// Setup logrus.
	logrus.SetFormatter(&logrus.JSONFormatter{})
	logrus.SetOutput(os.Stderr)

	// Add the stack hook.
	logrus.AddHook(logrus_stack.StandardHook())

	// Let's try it.
	Worker{"123"}.Perform()
}
$ go run example/main.go
{"caller":{"File":"github.com/Gurpartap/logrus-stack/example/main.go","Line":16,"Name":"Worker.Perform"},"jod_id":"123","level":"info","msg":"Now working","time":"2016-10-10T01:17:40+05:30"}
{"caller":{"File":"github.com/Gurpartap/logrus-stack/example/main.go","Line":20,"Name":"Worker.Perform"},"level":"error","msg":"I don't know what to do yet","stack":[{"File":"github.com/Gurpartap/logrus-stack/example/main.go","Line":20,"Name":"Worker.Perform"},{"File":"github.com/Gurpartap/logrus-stack/example/main.go","Line":36,"Name":"main"},{"File":"/usr/local/Cellar/go/1.7.1/libexec/src/runtime/proc.go","Line":183,"Name":"main"},{"File":"/usr/local/Cellar/go/1.7.1/libexec/src/runtime/asm_amd64.s","Line":2086,"Name":"goexit"}],"time":"2016-10-10T01:17:40+05:30"}
Same as above but indented:
{
	"caller": {
		"File": "github.com/Gurpartap/logrus-stack/example/main.go",
		"Line": 16,
		"Name": "Worker.Perform"
	},
	"jod_id": "123",
	"level": "info",
	"msg": "Now working",
	"time": "2016-10-10T09:41:00+05:30"
}
{
	"caller": {
		"File": "github.com/Gurpartap/logrus-stack/example/main.go",
		"Line": 20,
		"Name": "Worker.Perform"
	},
	"level": "error",
	"msg": "I don't know what to do yet",
	"stack": [
		{
			"File": "github.com/Gurpartap/logrus-stack/example/main.go",
			"Line": 20,
			"Name": "Worker.Perform"
		},
		{
			"File": "github.com/Gurpartap/logrus-stack/example/main.go",
			"Line": 36,
			"Name": "main"
		},
		{
			"File": "/usr/local/Cellar/go/1.7.1/libexec/src/runtime/proc.go",
			"Line": 183,
			"Name": "main"
		},
		{
			"File": "/usr/local/Cellar/go/1.7.1/libexec/src/runtime/asm_amd64.s",
			"Line": 2086,
			"Name": "goexit"
		}
	],
	"time": "2016-10-10T09:41:00+05:30"
}

If the same example was used with logrus.SetFormatter(&logrus.TextFormatter{}) instead, the output would be:

INFO[0000] Now working                                   caller=github.com/Gurpartap/logrus-stack/example/main.go:16 Worker.Perform jod_id=123
ERRO[0000] I don't know what to do yet                   caller=github.com/Gurpartap/logrus-stack/example/main.go:20 Worker.Perform stack=github.com/Gurpartap/logrus-stack/example/main.go:20            Worker.Perform
github.com/Gurpartap/logrus-stack/example/main.go:36            main
/usr/local/Cellar/go/1.7.1/libexec/src/runtime/proc.go:183      main
/usr/local/Cellar/go/1.7.1/libexec/src/runtime/asm_amd64.s:2086 goexit

Hello 👋

Follow me on https://twitter.com/Gurpartap

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LogrusStackHook

type LogrusStackHook struct {
	// Set levels to CallerLevels for which "caller" value may be set,
	// providing a single frame of stack.
	CallerLevels []logrus.Level

	// Set levels to StackLevels for which "stack" value may be set,
	// providing the full stack (minus logrus).
	StackLevels []logrus.Level
}

LogrusStackHook is an implementation of logrus.Hook interface.

func NewHook

func NewHook(callerLevels []logrus.Level, stackLevels []logrus.Level) LogrusStackHook

NewHook is the initializer for LogrusStackHook{} (implementing logrus.Hook). Set levels to callerLevels for which "caller" value may be set, providing a single frame of stack. Set levels to stackLevels for which "stack" value may be set, providing the full stack (minus logrus).

func StandardHook

func StandardHook() LogrusStackHook

StandardHook is a convenience initializer for LogrusStackHook{} with default args.

func (LogrusStackHook) Fire

func (hook LogrusStackHook) Fire(entry *logrus.Entry) error

Fire is called by logrus when something is logged.

func (LogrusStackHook) Levels

func (hook LogrusStackHook) Levels() []logrus.Level

Levels provides the levels to filter.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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