GoWorkers如何将日志存储到文件中?

huangapple go评论81阅读模式
英文:

GoWorkers how to store logs into a file

问题

我可以帮你翻译这段内容。以下是翻译的结果:

如何将来自GoWorkers的日志存储到文件中?我看到有一个WorkersLogger接口,但我不知道如何使用它。

目前我有以下代码:

func main() {
  workers.Configure(map[string]string)
  workers.Middleware.Append(&midRetry{})
  workers.Process("imp", worker.InitJob, 30)
  workers.Run()
}

type midRetry struct{}
func (r *midRetry) Call(queue string, message *workers.Msg, next func() bool) (acknowledge bool) {
  acknowledge = next()
  return
}

我在考虑在midRetry结构体中创建一个chan string,在Call方法中向通道发送消息,然后在main函数中接收消息并将其写入文件,但我感觉还有更好的方法。

英文:

How can I store the logs which are coming from GoWorkers (docs) into a file. I show that there is a WorkersLogger interface but I don't understand how to use it.

At the moment I have this:

func main() {
  workers.Configure(map[string]string)
  workers.Middleware.Append(&midRetry{})
  workers.Process("imp", worker.InitJob, 30)
  workers.Run()
}

type midRetry struct{}
func (r *midRetry) Call(queue string, message *workers.Msg, next func() bool) (acknowledge bool) {
  acknowledge = next()
  return
}

I was thinking in creating a chan string in the midRetry struct, and inside Call send messages on the channel, and receive them in main, and then write them to file, but I feel that there is a better way.

答案1

得分: 3

WorkerLogger接口只是一个实现PrintlnPrintf方法的结构体的接口。

你可以简单地定义一个新的log.Logger,给它一个os.File作为输出(参见构造函数),并将全局变量workers.Logger设置为这个新的记录器。

示例:

file, _ := os.Create("log.txt")
workers.Logger = log.New(file, "[worker] ", log.LstdFlags)

请注意,这是一个代码示例,用于说明如何使用log.Loggeros.File来实现日志记录功能。你需要根据自己的需求进行适当的修改和调整。

英文:

The WorkerLogger interface is just an interface for a struct that implements the Println and Printf methods.

You can simply define a new log.Logger, give it an os.File as output (see the constructor), and set the global var workers.Logger to this new logger.

Example:

file, _ := os.Create("log.txt")
workers.Logger = log.New(file, "[worker] ", log.LstdFlags)

huangapple
  • 本文由 发表于 2015年8月12日 22:45:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/31968528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定