Coder Social home page Coder Social logo

logrus-stack's Introduction

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

logrus-stack's People

Contributors

gurpartap avatar

Watchers

 avatar  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.