Last active
September 7, 2021 07:27
-
-
Save miguelmota/0508139b2df9142b4574dba2edb6fb9b to your computer and use it in GitHub Desktop.
Golang Logrus show filename and line number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
awesome