+
Skip to content

[log] Support source line number #7

@at15

Description

@at15

Found it when using prometheus

// sourced adds a source field to the logger that contains
// the file name and line where the logging happened.
func (l logger) sourced() *logrus.Entry {
	_, file, line, ok := runtime.Caller(2)
	if !ok {
		file = "<???>"
		line = 1
	} else {
		slash := strings.LastIndex(file, "/")
		file = file[slash+1:]
	}
	return l.entry.WithField("source", fmt.Sprintf("%s:%d", file, line))
}

FIXME:

  • using source as tag would overwrite user defined value for source if there is any, maybe we should rename ours to _source? but this would also add burden for log processing

TODO:

  • the line number should be highlighted by IDE, so when you run test in IDE, you can click the log and jump to the line

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载