Giter Site home page Giter Site logo

logrus's Introduction

Hooks for logrus

Example

package main

import (
    "fmt"

    "github.com/onrik/logrus/filename"
    "github.com/onrik/logrus/sentry"
    log "github.com/sirupsen/logrus"
)

var (
    dsn = "http://[email protected]/1"
)

func main() {
    filenameHook := filename.NewHook()
    filenameHook.Field = "custom_source_field" // Customize source field name
    log.AddHook(filenameHook)

    sentryHook, err := sentry.NewHook(sentry.Options{
        Dsn: dsn,
    }, log.PanicLevel, log.FatalLevel, log.ErrorLevel)
    if err != nil {
        log.Error(err)
        return
    }
    defer sentryHook.Flush()
    
    log.AddHook(sentryHook)

    err = fmt.Errorf("test error")
    log.WithError(err).Error("Dead beef")
}

logrus's People

Contributors

angristan avatar davidklassen avatar geertjohan avatar itmeze avatar mikluke avatar neeraj-npen avatar onrik avatar pilinux avatar smurfpandey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

logrus's Issues

GORM add to support FieldLogger

please change to use log.FieldLogger interface :

type Logger struct {
	//logrus *logrus.Logger
        logrus log.FieldLogger
}

so that we can use either logrus.Logger or logrus.Entry
we can also use existing preconfigured logrus Logger with this change.

package main

import (
    "github.com/jinzhu/gorm"
    gormlog "github.com/onrik/logrus/gorm"
    "github.com/sirupsen/logrus"
)

func main() {
    db, err := gorm.Open("<driver>", "<dsn>")
    if err != nil {
        logrus.Fatal(err)
    }

    db.SetLogger(gormlog.New(logrus.WithFields(log.Fields{"app": "gorm"})))
    //db.SetLogger(gormlog.New(logrus.StandardLogger()))
    db.LogMode(true) // Will log only on logrus debug level
}

Release or Tag

Would it be possible for you to create a release/tag? I am using the fileline hook in a few projects and it would be nice to insulate them from any potential bugs/etc.

Thanks

support logs with with fields and `_source`

With _source="registry/container.go:93" we can pinpoint source of the SQL logs

current logs with fields only

DEBU[2019-08-13T10:51:32-07:00] CREATE TABLE "users" ("created_at" datetime NOT NULL,"deleted_at" datetime,"email" varchar(255) NOT NULL,"first_name" varchar(255) NOT NULL,"id" uuid,"last_name" varchar(255) NOT NULL,"updated_at" datetime NOT NULL,"username" varchar(100) NOT NULL , PRIMARY KEY ("id")) [190.156µs]  module=gorm
DEBU[2019-08-13T10:51:32-07:00] CREATE INDEX idx_users_deleted_at ON "users"(deleted_at)  [76.138µs]  module=gorm
DEBU[2019-08-13T10:51:32-07:00] CREATE TABLE "profiles" ("avatar" varchar(255),"birthday" datetime NOT NULL,"created_at" datetime NOT NULL,"deleted_at" datetime,"gender" varchar(255),"id" uuid,"tz" varchar(255),"updated_at" datetime NOT NULL,"user_id" varchar(255) , PRIMARY KEY ("id")) [80.66µs]  module=gorm
DEBU[2019-08-13T10:51:32-07:00] CREATE INDEX idx_profiles_deleted_at ON "profiles"(deleted_at)  [65.971µs]  module=gorm

logs with with fields and _source

DEBU[2019-08-13T10:53:23-07:00] CREATE TABLE "users" ("created_at" datetime NOT NULL,"deleted_at" datetime,"email" varchar(255) NOT NULL,"first_name" varchar(255) NOT NULL,"id" uuid,"last_name" varchar(255) NOT NULL,"updated_at" datetime NOT NULL,"username" varchar(100) NOT NULL , PRIMARY KEY ("id"))  _source="registry/container.go:93" module=gorm type=sql
DEBU[2019-08-13T10:53:23-07:00] CREATE INDEX idx_users_deleted_at ON "users"(deleted_at)   _source="registry/container.go:93" module=gorm type=sql
DEBU[2019-08-13T10:53:23-07:00] CREATE TABLE "profiles" ("avatar" varchar(255),"birthday" datetime NOT NULL,"created_at" datetime NOT NULL,"deleted_at" datetime,"gender" varchar(255),"id" uuid,"tz" varchar(255),"updated_at" datetime NOT NULL,"user_id" varchar(255) , PRIMARY KEY ("id"))  _source="registry/container.go:99" module=gorm type=sql
DEBU[2019-08-13T10:53:23-07:00] CREATE INDEX idx_profiles_deleted_at ON "profiles"(deleted_at)   _source="registry/container.go:99" module=gorm type=sql

filename.go doesn't include Func name in the log - is this intentional?

Formatter: func(file, function string, line int) string {
return fmt.Sprintf("%s:%d", file, line)

Hi,
Although the function name is now available, the formatter doesn't use it.
Would be nice if it did 👍
(if some users feel like it is too cumbersome and prefer only file+line, maybe should introduce a separate one that is identical to filename.go except for including the func name in the log)

any thoughts?

when I print logs, deadlock!

hi, i have some trouble.

i use like this:
filenameHook := filename.NewHook()
filenameHook.Field = "custom_source_field"
log.AddHook(filenameHook)
log.Infof("finish init logger")

BUT, it happened:
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [semacquire]:
sync.runtime_SemacquireMutex(0xc000090034, 0x10c3300, 0x1)
/usr/local/Cellar/go/1.13.3/libexec/src/runtime/sema.go:71 +0x47
sync.(*Mutex).lockSlow(0xc000090030)
/usr/local/Cellar/go/1.13.3/libexec/src/sync/mutex.go:138 +0xfc
sync.(*Mutex).Lock(...)
/usr/local/Cellar/go/1.13.3/libexec/src/sync/mutex.go:81
github.com/sirupsen/logrus.(*MutexWrap).Lock(...)
/Users/onepiece/go/src/github.com/sirupsen/logrus/logger.go:53
github.com/sirupsen/logrus.(*Entry).WithFields(0xc0000900e0, 0xc00007ca70, 0x0)
/Users/onepiece/go/src/github.com/sirupsen/logrus/entry.go:125 +0x68d
github.com/sirupsen/logrus.(*Entry).WithField(...)
/Users/onepiece/go/src/github.com/sirupsen/logrus/entry.go:120
github.com/onrik/logrus/filename.(*wrapper).Format(0xc00000c0a0, 0xc0000900e0, 0x30, 0x10d7420, 0xc00004d401, 0xc000074270, 0xc00007cc10)
/Users/onepiece/go/src/github.com/onrik/logrus/filename/filename.go:19 +0x184
github.com/sirupsen/logrus.(*Entry).write(0xc0000900e0)
/Users/onepiece/go/src/github.com/sirupsen/logrus/entry.go:270 +0xa1
github.com/sirupsen/logrus.Entry.log(0xc000090000, 0xc000074240, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
/Users/onepiece/go/src/github.com/sirupsen/logrus/entry.go:246 +0x19d
github.com/sirupsen/logrus.(*Entry).Log(0xc000090070, 0x4, 0xc00007ce30, 0x1, 0x1)
/Users/onepiece/go/src/github.com/sirupsen/logrus/entry.go:282 +0xeb
github.com/sirupsen/logrus.(*Entry).Logf(0xc000090070, 0xc000000004, 0x10eef3b, 0x11, 0xc00007cee0, 0x1, 0x1)
/Users/onepiece/go/src/github.com/sirupsen/logrus/entry.go:328 +0xe2
github.com/sirupsen/logrus.(*Logger).Logf(0xc000090000, 0x4, 0x10eef3b, 0x11, 0xc00007cee0, 0x1, 0x1)
/Users/onepiece/go/src/github.com/sirupsen/logrus/logger.go:146 +0x94
github.com/sirupsen/logrus.(*Logger).Infof(...)
/Users/onepiece/go/src/github.com/sirupsen/logrus/logger.go:160
github.com/sirupsen/logrus.Infof(...)
/Users/onepiece/go/src/github.com/sirupsen/logrus/exported.go:154
main.initLogger(0x10ed49d, 0x4)
/Users/onepiece/go/src/awesomeProject2/main.go:39 +0x1fb
main.main()
/Users/onepiece/go/src/awesomeProject2/main.go:12 +0x3a
exit status 2

could someone help me? Thanks!!

is that can work????

package main
import (
"github.com/onrik/logrus/filename"
"github.com/onrik/logrus/sentry"
log "github.com/sirupsen/logrus"
)

var (
dsn = "http://60a0257d7b5a429a8838e5f2ba873ec9:[email protected]/1"
)

func main() {
filenameHook := filename.NewHook()
filenameHook.Field = "filename" // Customize source field name
log.AddHook(filenameHook)

sentryHook := sentry.NewHook(dsn, log.PanicLevel, log.FatalLevel, log.ErrorLevel)
log.AddHook(sentryHook)

log.WithFields(log.Fields{
"a":"a",
}).Info("Request info")

}

that is the log ouput:

time="2017-11-03T00:01:49+08:00" level=info msg="Request info" a=a filename="runtime/proc.go:185"

as you can see ,th filename is error!

Would be nice if we have option to get function name

Would be nice if the hook has the option do display function name, just something like that could help:

        pc := make([]uintptr, 15)
	n := runtime.Callers(8, pc)
	frames := runtime.CallersFrames(pc[:n])
	frame, _ := frames.Next()
	return fmt.Sprintf("%s:%d - %s", frame.File, frame.Line, frame.Function)

filename hook is not thread safe

This is an example that reproduces the problem.

package main

import (
	"github.com/onrik/logrus/filename"
	"github.com/sirupsen/logrus"
)

func main() {
	logger := logrus.New()
	logger.AddHook(filename.NewHook())
	l := logger.WithFields(logrus.Fields{"foo": "bar"})
	go func() {
		for {
			l.WithFields(nil)
		}
	}()
	go func() {
		for {
			l.Println("test")
		}
	}()
	for {
	}
}

Run it with go run -race main.go > /dev/null.
Looks like the problem is in the hook.Fire function that modifies entry.Data.

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.