Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Golang Logrus show filename and line number
package logger
import (
// 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

This comment has been minimized.

Copy link

@giter giter commented Nov 7, 2018


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment