Closed robatipoor closed 2 days ago
I can see that you tweaked the example a bit. That's great, but some tweaks need to be applied in very specific context.
I noticed that you'd like to use JacksonJsonLayout singleThread="true"
. This will not work with Logger
(as stated in the documentation: Use ONLY with AsyncLogger
)
singleThread=true
enables SingleThreadJsonFactory
. Usage of this factory is strictly limited - in Log4j2 library context - to AsyncLogger
(s). singleThread
means "it can only be used by one thread at a time". This condition (unless running a single-thread app) will never be satisfied when using Logger
- logger (and appender) can be accessed concurrently by more than 1 thread, mangling the underlying, shared JsonWriteContext
that SingleThreadJsonFactory
provides.
In contrast, AsyncLogger
consumes LogEvent
(s) from a RingBuffer in a separate (single) thread, ensuring that re-used data structures are ready (and safe) to serialize next log event.
Given the above, I suggest either:
singleThread=true
- depending on the amount of logs you'd like to produce, while it reduces the memory allocation during serialisation to JSON, it's not really noticeable in low-traffic applicationsAsyncLogger
- if you can continue if log was not written (that depends on how reliable your application logging must be), ensuring only-one-thread-at-a-time access to SingleThreadJsonFactory
is critical for this factory to work properly.Also, this doesn't seem to be correct. See Object Pooling docs
<ByteBufItemSourceFactory itemSizeInBytes="1024" maxItemSizeInBytes="16384" initialPoolSize="20000"/>
<PooledItemSourceFactory poolName="itemPool" itemSizeInBytes="1024" initialPoolSize="6000"
monitored="true" monitorTaskInterval="10000" resizeTimeout="500">...
Thank you for your response. it seems my problem is solved.
Happy to help :+1:
I tried using this example example in my Spring Boot project, but I encountered the following error:
pom.xml:
log4j2.xml:
Thank you for your consideration.