You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
More logs become corrupted when the application is handling many web requests simultaneously.
Looking at the code, we see that JsonLayout uses a single JsonEventWriter instance shared across threads. As soon as multiple threads write logs at the same time, logs can become corrupted easily. An automated test proves this trivially.
A quick fix is to synchronize the method with the caveat that in high-contention scenarios threads will block each other when logging. There are other options, such as creating a new writer instance per event at the cost of increased memory allocations (this is what logback's builtin PatternLayout does) or using a pool of writers, allowing parallelism of up to N threads (could be made configurable via logback.xml) requiring a constant amount of memory.
I'll gladly provide a PR shortly, including a test to protect against regressions of this type of error.
Anybody experiencing the same problem and unable to upgrade to a version with the fix, can use the following workaround for the time being:
public class SynchronizedJsonLayout extends JsonLayout {
@Override
public synchronized String doLayout(final ILoggingEvent event) {
return super.doLayout(event);
}
}
And then change their logback.xml to use the synchronized layout:
Using loki-logback-appender with the a simple config, similar to the one from the docs:
results in corrupted JSON log entries in Loki, such as the following (1 line = 1 log entry):
More logs become corrupted when the application is handling many web requests simultaneously.
Looking at the code, we see that
JsonLayout
uses a singleJsonEventWriter
instance shared across threads. As soon as multiple threads write logs at the same time, logs can become corrupted easily. An automated test proves this trivially.A quick fix is to synchronize the method with the caveat that in high-contention scenarios threads will block each other when logging. There are other options, such as creating a new writer instance per event at the cost of increased memory allocations (this is what logback's builtin
PatternLayout
does) or using a pool of writers, allowing parallelism of up to N threads (could be made configurable vialogback.xml
) requiring a constant amount of memory.I'll gladly provide a PR shortly, including a test to protect against regressions of this type of error.
Anybody experiencing the same problem and unable to upgrade to a version with the fix, can use the following workaround for the time being:
And then change their
logback.xml
to use the synchronized layout:The text was updated successfully, but these errors were encountered: