I'm creating a logging system where all the logs generated by specific request are stored in a request scoped object and subsequently logged on a ContainerResponseFilter. So instead of having many logs per request, I'll have just one with all the information regarding that specific request.
How I store the logs:
@RequestScoped
class Logs {
private List<Log> logs;
...
}
How I add logs:
@Inject
Logs logs;
...
private void myFunction(){
logs.add(new Log(...));
}
How I write them in a ContainerResponseFilter:
@Inject
Logs logs;
@Inject
Logger logger;
@Override
public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext) {
logger.log(logs.entries());
}
How I launch the async code:
Uni.createFrom().item(myItem)
.emitOn(Infrastructure.getDefaultWorkerPool())
.subscribe().with(this::myFunc)
The issue I'm having is with a request that launches an async Uni which runs on a worker thread. Since it's not on the same thread of the request, I have two problems:
- The ContainerResponseFilter may finish executing before the Uni, which means that any logs performed in the Uni are lost.
- Since the @RequestScoped context seems to be thread local, the logs performed in the Uni are placed on a completely separate structure.
By having the ContainerResponseFilter launch a Uni which runs after the previously mentioned async uni I managed to solve the first issue, but not the second.
Any suggestions on how to solve this last issue? I would be fine with just logging immediately and individually when running on a worker thread or any other context that is asynchronous to the request itself.