Skip to content

Instantly share code, notes, and snippets.

@Jimmy-Xu
Forked from miguelmota/logrus_hooks.go
Created May 22, 2019 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jimmy-Xu/a3c87c96b557c5e5e0206c470955b151 to your computer and use it in GitHub Desktop.
Save Jimmy-Xu/a3c87c96b557c5e5e0206c470955b151 to your computer and use it in GitHub Desktop.
Golang Logrus show filename and line number
package logger
import (
"fmt"
"path"
"runtime"
"github.com/sirupsen/logrus"
)
// ContextHook ...
type ContextHook struct{}
// Levels ...
func (hook ContextHook) Levels() []logrus.Level {
return logrus.AllLevels
}
// Fire ...
func (hook ContextHook) Fire(entry *logrus.Entry) error {
if pc, file, line, ok := runtime.Caller(10); ok {
funcName := runtime.FuncForPC(pc).Name()
entry.Data["source"] = fmt.Sprintf("%s:%v:%s", path.Base(file), line, path.Base(funcName))
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment