我已经开始使用 Go 登录,我遇到了这篇关于登录 Go 的文章https://www.goinggo.net/2013/11/using-log-package-in-go.html
使用以下源代码(略有更改):
var (
Trace *log.Logger
Info *log.Logger
Warning *log.Logger
Error *log.Logger
)
func Init(
traceHandle io.Writer,
infoHandle io.Writer,
warningHandle io.Writer,
errorHandle io.Writer) {
Trace = log.New(traceHandle,
“TRACE: “,
log.Ldate|log.Ltime|log.Lshortfile)
Info = log.New(infoHandle,
“INFO: “,
log.Ldate|log.Ltime|log.Lshortfile)
Warning = log.New(warningHandle,
“WARNING: “,
log.Ldate|log.Ltime|log.Lshortfile)
Error = log.New(errorHandle,
“ERROR: “,
log.Ldate|log.Ltime|log.Lshortfile)
}
func main() {
Init(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr)
}
现在我想向此记录器生成的日志条目添加一些自定义信息,但它们不是常量字符串文字(如添加到那里INFO:或WARNING:)
例如,我想添加触发日志的函数的名称。假设我有以下功能,我想为此使用:(来自此处)
func _enter() {
// Skip this function, and fetch the PC and file for its parent
pc, _, _, _ := runtime.Caller(1)
// Retrieve a Function object this functions parent
functionObject := runtime.FuncForPC(pc)
// Regex to extract just the function name (and not the module path)
extractFnName := regexp.MustCompile(`^.*\.(.*)$`)
fnName := extractFnName.ReplaceAllString(functionObject.Name(), "$1")
fmt.Printf("Entering %s\n", fnName)
}
如何将其添加到记录器?
潇湘沐
相关分类