Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created March 26, 2019 06:00
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 miguelmota/98e15aee9da90723edad609ec26db17e to your computer and use it in GitHub Desktop.
Save miguelmota/98e15aee9da90723edad609ec26db17e to your computer and use it in GitHub Desktop.
Golang logrus cut longest file path example
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