I am using github.com/sirupsen/logrus for logging in my golang scripts, however I want to get the filename and the line number which is logging the message. I am able to get that using the below code:
package main
import (
"fmt"
"os"
"runtime"
"strings"
"github.com/sirupsen/logrus"
)
func GetLogger() (*logrus.Logger, *os.File) {
log := logrus.New()
log.SetReportCaller(true)
file, err := os.OpenFile("info.log", os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
log.Out = file
log.Formatter = &logrus.TextFormatter{
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
repopath := fmt.Sprintf("%s/src/github.com/bob", os.Getenv("GOPATH"))
filename := strings.Replace(f.File, repopath, "", -1)
return fmt.Sprintf("%s()", f.Function), fmt.Sprintf("%s:%d", filename, f.Line)
},
}
return log, file
}
However this gives log in the below format:
time="2020-04-02T11:43:19+05:30" level=info msg=Hello func="main.main()" file="D:/.../main.go:13"
But I want the log in format as below:
Apr 02 00:00:00 INFO main.go:20 : Hello this is a log line
How can a custom formatter be written to get this?