Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

go - 如何在库中将logrus项目生成为函数调用(How to generate a logrus item as a function call in a library)

I am trying to create a logrus log (mylog) within the main package, and add log items to mylog by function calls within object methods in libraries.

(我试图在主程序包中创建日志日志(mylog),并通过库中对象方法内的函数调用将日志项添加到mylog中。)

In the main package, I created a logrus log called mylog I have a structure called "LogItemList" in a package called logutil and I want to write a method for the LogItemList which will automatically populate mylog with attributes within the object LogItemList.

(在主程序包中,我创建了一个名为mylog的日志日志,在名为logutil的程序包中有一个名为“ LogItemList”的结构,我想为LogItemList编写一个方法,该方法将使用对象LogItemList中的属性自动填充mylog。)

Here is the code for the LogItemList method:

(这是LogItemList方法的代码:)

func (self LogItemList) WriteToLog(inputlog *logrus.Logger) {
    inputlog.WithFields(log.Fields{
        "Event": "Event Description",
    }).Info("Computing")
}

How should I call the method from the main package?

(我应该如何从主程序包中调用该方法?)

This code is not accepted by the compiler:

(编译器不接受以下代码:)

loglist := logutil.NewLogItemList("Info", "main", "test")
loglist.WriteToLog(log.Logger)

The second line of the code (loglist.WriteToLog(log.Logger)) is not accepted by the compiler.

(编译器不接受第二行代码(loglist.WriteToLog(log.Logger))。)

I should pass a parameter to WriteToLog but I do not know which one.

(我应该将一个参数传递给WriteToLog,但我不知道哪个。)

  ask by Emanolo78 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...