Add req-ID to logging

with this log format we can easily identify
the logs per request

Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
This commit is contained in:
Madhu Rajanna
2019-09-11 11:34:07 +05:30
committed by mergify[bot]
parent ed9330d2f6
commit e395080cdc
3 changed files with 117 additions and 0 deletions

View File

@ -23,6 +23,9 @@ type contextKey string
// CtxKey for context based logging
var CtxKey = contextKey("ID")
// ReqID for logging request ID
var ReqID = contextKey("Req-ID")
// Log helps in context based logging
func Log(ctx context.Context, format string) string {
id := ctx.Value(CtxKey)
@ -30,5 +33,10 @@ func Log(ctx context.Context, format string) string {
return format
}
a := fmt.Sprintf("ID: %v ", id)
reqID := ctx.Value(ReqID)
if reqID == nil {
return a + format
}
a += fmt.Sprintf("Req-ID: %v ", reqID)
return a + format
}