Hi all,
I have a service method called in scheduled task. Is possible to record log did in service method in result field of task scheduled execution?
Is there another solution to record trace information of scheduled task?
Thanks to all for help
Hi all,
I have a service method called in scheduled task. Is possible to record log did in service method in result field of task scheduled execution?
Is there another solution to record trace information of scheduled task?
Thanks to all for help
Hi,
If you mean log records done by SLF4J logger - then no, it’s not possible.
If you want to keep some messages logged in the inner service methods, and then save them to database, you can use separate class instead of standard Logger. It should utilize StringBuilder and ThreadLocal variable.
Like below:
@Component
public class LogAccumulator {
private ThreadLocal<StringBuilder> logHolder;
public void log(String message) {
StringBuilder builder = logHolder.get();
if (builder == null) {
builder = new StringBuilder();
logHolder.set(builder);
}
builder.append(message).append("\n");
}
public String getAccumulatedLog() {
StringBuilder builder = logHolder.get();
if (builder == null) {
return "";
}
String content = builder.toString();
logHolder.remove();
return content;
}
}
Then you can inject this bean, call log() for logging in any inner service methods.
In the scheduled task - take accumulated messages with getAccumulatedLog() .