[ 
https://issues.apache.org/jira/browse/LOG4J2-1397?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15321679#comment-15321679
 ] 

Remko Popma commented on LOG4J2-1397:
-------------------------------------

The byte buffer should not be used by two threads simultaneously. This is a 
valid concern and needs to be addressed either in the application or in the 
logging framework.

How about doing this in the application:
* Pre-allocate a set of ByteBuffers in the application
* How many to pre-allocate? I would say one for each slot in the Log4j 
AsyncLogger RingBuffer. By default this is 256*1024, which may be a lot for 
your purposes. Let's say you change this to 32,768. Then also pre-allocate 
32,768 ByteBuffers. The AsyncLogger RingBuffer size is a power of 2. Use the 
same size for your pre-allocated array to allow you have a long counter that is 
incremented indefinitely and get the current slot by masking the counter by 
length-1.
* How large should each pre-allocated ByteBuffer be? That depends on the size 
(max size) of the messages you are processing.

When you receive a message, obtain the next ByteBuffer from the pre-allocated 
set, copy the data into it and log that pre-allocated ByteBuffer.

{code}
final static int COUNT = 32768; // use same size as Async Logger RingBuffer!
final static int MASK = COUNT - 1
ByteBuffer[] preAllocated = preAllocate(COUNT);
long counter = 0;

private void onMessage(ByteBuffer msg) {
  if (messageLog.isInfoEnabled()) {
    ByteBuffer copy = preAllocated[MASK & (counter++)];
    copy.clear();
    copy.put(buffer);
    buffer.rewind();
    copy.flip();
    // messageLog.info("Message on {}: IN {}", connection, copy); // do not log 
as text
    messageLog.info(copy);
  }
}
{code}


> Support ByteBufferLayout
> ------------------------
>
>                 Key: LOG4J2-1397
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1397
>             Project: Log4j 2
>          Issue Type: New Feature
>          Components: Layouts
>    Affects Versions: 2.6
>            Reporter: Remko Popma
>
> As requested by Kirk Pepperdine on the Mechanical Sympathy [mailing 
> list|https://groups.google.com/d/msg/mechanical-sympathy/klefjRqlpQE/-1WF59IGAwAJ]:
> Support a new Layout that takes ObjectMessages containing a ByteBuffer 
> payload and write them to the appender without modification.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-dev-h...@logging.apache.org

Reply via email to