Skip to content

stverhae/logrus-stack

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

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

About

Adds stack trace to github.com/Sirupsen/logrus

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%