logrus icon indicating copy to clipboard operation
logrus copied to clipboard

SetReportCaller only returns logrus log file instead of where the error occured

Open mateusmlo opened this issue 2 years ago • 2 comments

This is my func that instantiates a new logrus:

func NewLogger() *StdLogger {
	baseLogger := logrus.New()
	stdLogger := &StdLogger{baseLogger}

	if beego.AppConfig.String("runmode") != "dev" {
		stdLogger.SetOutput(os.Stdout)
		stdLogger.SetLevel(logrus.DebugLevel)
	} else {
		stdLogger.SetLevel(logrus.InfoLevel)
	}

	stdLogger.SetReportCaller(true)
        // prettyfier based on an official example
	stdLogger.Formatter = &logrus.JSONFormatter{
		PrettyPrint: true,
		CallerPrettyfier: func(f *runtime.Frame) (string, string) {
			s := strings.Split(f.Function, ".")
			funcname := s[len(s)-1]
			_, filename := path.Split(f.File)
			return funcname, filename
		},
	}

	return stdLogger
}

From it, I defined a few other log functions such as this one for common errors:

func (log *StdLogger) LogError(arg string) {
	log.Errorf(commonError.message, arg)
}

However, the file and func properties on the logs will only refer to my log package itself, that is, the very LogError func above instead of where I captured it with an err != nil check. I'm currently on go 1.18, logrus@latest, saw a buch of other resolved issues where this problem seems to have been solved, but it's happening to me nonetheless.

mateusmlo avatar Jul 14 '22 15:07 mateusmlo

Sembra che questo livello di avvolgimento della libreria di classi sia la causa di questo problema, l'hai risolto per favore?

ysk229 avatar Jul 20 '22 01:07 ysk229

After debugging, the constant knownLogrusFrames=7 in yywrentry.go can be changed to

ysk229 avatar Jul 20 '22 02:07 ysk229

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Aug 20 '22 02:08 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Sep 03 '22 02:09 github-actions[bot]

Have you found a solution to this problem?

agclqq avatar Nov 25 '22 02:11 agclqq