How to add context info in logs

Should we be using ContextPropagators to pass information like request-id? I want it to be logged in every log entry without having to explicitly every time i log something

1 Like

Yes context propagators were created for exactly this purpose.

Is there a way to print propagated values in logs automatically?

Add log statements to your custom propagators. Also they are recorded in history events as Header fields.

I meant, I want them printed with each log entry automatically. Is that possible?
Currently iā€™m using:

workflow.GetLogger(ctx).Info("worker started", zap.String("workflowId", workflowId))

Or would I have to use:

if val := ctx.Value(propagateKey); val != nil {
		vals := val.(Values)
		workflow.GetLogger(ctx).Info("worker started", zap.String("workflowId", workflowId), zap.String(vals.Key, vals.Value))
	}

I believe the logger that you are getting using workflow.GetLogger already contains workflowId and workflowType attributes. Here is the code that injects them.

To inject custom attributes you can use WorkflowInterceptor.

Here is an intercpetor implementation you could use as a reference.

1 Like