Golang go-worker自定义日志记录中间件?

时间:2015-05-27 08:25:40

标签: logging go sidekiq middleware

我正构建一个Golang应用,该应用实现Sidekiq - 兼容的jrallison/go-workers工作队列和围绕Sirupsen/logrus的自定义日志记录包装,用于封送的JSON日志。< / p>

现在,我的所有应用程序(到目前为止除go-workers之外)都在中央位置使用我的记录器包装器,以确保其100%的输出与JSON兼容。

请注意,第1行和第2行是我们中央记录器的正确JSON,但是当go-workers初始化时,我们看到第3行来自错误的记录器,以纯文本形式显示。

{"db":{"Mapper":{}},"instance_id":"1","level":"info","msg":"Db: Connected to MySQL","time":"2015-05-27T04:15:15-04:00"}
{"concurrency":10,"instance_id":"1","level":"info","msg":"Worker: Commencing work","time":"2015-05-27T04:15:15-04:00"}
workers: 2015/05/27 04:15:15.211217 processing queue contact with 10 workers.

当我们发送信号以关闭程序时,我们首先在第1行看到错误的记录器,然后是第2行的中央记录器中的正确JSON。

^C
workers: 2015/05/27 04:15:17.197504 quitting queue contact (waiting for 0 / 10 workers).
{"instance_id":"1","level":"info","msg":"Closed correctly","time":"2015-05-27T04:15:17-04:00"}

我似乎无法使用此自定义MiddlewareLogging来替换go-workers默认日志记录中间件。

func (a *App) ConfigureWorkers() {
  workers.Middleware = workers.NewMiddleware(
    &WorkMiddleware{ App: a },
    )
}

type WorkMiddleware struct{
  App *App
}

func (m *WorkMiddleware) Call(queue string, message *workers.Msg, next func() bool) (acknowledge bool) {
  // before each message is processed:
  job_json := message.Args().GetIndex(0).MustString()
  job := ContactJob{}
  err := json.Unmarshal([]byte(job_json), &job)
  if err != nil {
  m.App.Log.WithFields( log.Fields{
    "job_json": job_json,
    }).Fatal("Worker: Could not Unmarshal job JSON")
    return
  }
  SetMessageJob(message, job)
  start := time.Now()
  m.App.Log.WithFields( log.Fields{
    "job_id": message.Jid(),
    "queue": queue,
    "args": message.Args(),
    }).Print("Work: Job Starting")
  defer func() {
    if e := recover(); e != nil {
      buf := make([]byte, 4096)
      buf = buf[:runtime.Stack(buf, false)]
      m.App.Log.WithFields( log.Fields{
          "job_id": message.Jid(),
          "queue": queue,
          "duration": time.Since(start),
          "error": e,
          "stack": buf,
        }).Fatal("Work: Job Failed")
    }
  }()
  acknowledge = next()
  result := GetMessageResult(message)
  m.App.Log.WithFields( log.Fields{
    "job_id": message.Jid(),
    "result": result,
    "queue": queue,
    "duration": time.Since(start),
    }).Print("Work: Job Done")
  return
}

实际上是否可以为这些行替换默认的go-workers日志记录中间件?

1 个答案:

答案 0 :(得分:1)

jrallison引用github.com/jrallison/go-workers/issues/50

  

看起来您正在替换工作日志记录中间件(它记录有关您的工作人员处理的每条消息的信息)。

     

我在输出中看到的唯一日志记录是来自管理器的处理/退出日志条目,这些条目在启动和关闭时记录。

     

https://github.com/jrallison/go-workers/blob/571e6b3b7be959e99024ec12b83b2ad261b06ff7/manager.go#L47

     

如果您希望不记录这些内容,您可能希望将workers.Logger替换为符合WorkersLogger接口的对象,但实际上并未记录任何内容(...或者可能是您的用例,以JSON格式记录):

问题由我的最终代码解决:

func (a *App) ConfigureWorkers() {
  workers.Middleware = workers.NewMiddleware( &WorkMiddleware{ App: a } )
  workers.Logger = &WorkersLogger{ App: a }
}

type WorkersLogger struct {
  App *App
}

func (l *WorkersLogger) Println(args ...interface{}) {
  l.App.Log.WithFields( log.Fields{
    "instanceId": l.App.Id,
    }).Println(args...)
}

func (l *WorkersLogger) Printf(fmt string, args ...interface{}) {
  l.App.Log.WithFields( log.Fields{
    "instanceId": l.App.Id,
    }).Printf(fmt, args...)
}