Golang go-workers自定义日志记录中间件?
Golang go-workers custom logging middleware?
我正在为编组 JSON 日志构建 Golang app that implements a Sidekiq-compatible jrallison/go-workers work queue and a custom logging wrapper around Sirupsen/logrus。
现在,我的所有应用程序(目前 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
日志记录中间件?
引用 jrallison from github.com/jrallison/go-workers/issues/50:
It looks like you're replacing the worker logging middleware (which logs information about each and every message processed by your workers).
The only logging I see in your output is the processing/quitting log entries from the manager which are logged on startup and shutdown.
https://github.com/jrallison/go-workers/blob/571e6b3b7be959e99024ec12b83b2ad261b06ff7/manager.go#L47
If you'd like these to not be logged, you may want to replace workers.Logger with an object that meets the WorkersLogger interface, but doesn't actually log anything (... or perhaps for your use-case, logs it in JSON format):
问题已由我的最终代码解决:
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...)
}
我正在为编组 JSON 日志构建 Golang app that implements a Sidekiq-compatible jrallison/go-workers work queue and a custom logging wrapper around Sirupsen/logrus。
现在,我的所有应用程序(目前 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
日志记录中间件?
引用 jrallison from github.com/jrallison/go-workers/issues/50:
It looks like you're replacing the worker logging middleware (which logs information about each and every message processed by your workers).
The only logging I see in your output is the processing/quitting log entries from the manager which are logged on startup and shutdown.
https://github.com/jrallison/go-workers/blob/571e6b3b7be959e99024ec12b83b2ad261b06ff7/manager.go#L47
If you'd like these to not be logged, you may want to replace workers.Logger with an object that meets the WorkersLogger interface, but doesn't actually log anything (... or perhaps for your use-case, logs it in JSON format):
问题已由我的最终代码解决:
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...)
}