Created
March 26, 2019 06:00
-
-
Save miguelmota/98e15aee9da90723edad609ec26db17e to your computer and use it in GitHub Desktop.
Golang logrus cut longest file path example
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 main | |
import ( | |
"fmt" | |
"os" | |
"runtime" | |
"strings" | |
"github.com/sirupsen/logrus" | |
) | |
func main() { | |
log := logrus.New() | |
log.SetReportCaller(true) | |
log.Formatter = &logrus.TextFormatter{ | |
CallerPrettyfier: func(f *runtime.Frame) (string, string) { | |
repopath := fmt.Sprintf("%s/src/github.com/miguelmota", os.Getenv("GOPATH")) | |
filename := strings.Replace(f.File, repopath, "", -1) | |
return fmt.Sprintf("%s()", f.Function), fmt.Sprintf("%s:%d", filename, f.Line) | |
}, | |
} | |
log.Println("hello world") | |
// OUTPUTS: | |
// | |
// INFO[0000]/test/main.go:23 main.main() hello world | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment