Example usage:
import static org.apache.logging.log4j.util.Unbox.box;
...
long longValue = 123456L;
double doubleValue = 3.14;
// prevent primitive values from being auto-boxed
logger.debug("Long value={}, double value={}", box(longValue), box(doubleValue));
This class manages a small thread-local ring buffer of StringBuilders.
Each time one of the box() methods is called, the next slot in the ring buffer is used, until the ring
buffer is full and the first slot is reused. By default the Unbox ring buffer has 32 slots, so user code can
have up to 32 boxed primitives in a single logger call.
If more slots are required, set system property log4j.unbox.ringbuffer.size to the desired ring buffer size.
Note that the specified number will be rounded up to the nearest power of 2.
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| private static class | Unbox.
State implementation that only puts JDK classes in ThreadLocals, so this is safe to be used from web applications. |
| Modifier and Type | Field and Description |
|---|---|
| private static final int | |
| private static final Logger | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static ThreadLocal | |
| private static Unbox. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static StringBuilder | Returns: aStringBuilder containing the text representation of the specified primitive valuethe value whose text representation to return value)Returns a |
| public static StringBuilder | Returns: aStringBuilder containing the text representation of the specified primitive valuethe value whose text representation to return value)Returns a |
| public static StringBuilder | Returns: aStringBuilder containing the text representation of the specified primitive valuethe value whose text representation to return value)Returns a |
| public static StringBuilder | Returns: aStringBuilder containing the text representation of the specified primitive valuethe value whose text representation to return value)Returns a |
| public static StringBuilder | Returns: aStringBuilder containing the text representation of the specified primitive valuethe value whose text representation to return value)Returns a |
| public static StringBuilder | Returns: aStringBuilder containing the text representation of the specified primitive valuethe value whose text representation to return value)Returns a |
| public static StringBuilder | Returns: aStringBuilder containing the text representation of the specified primitive valuethe value whose text representation to return value)Returns a |
| public static StringBuilder | Returns: aStringBuilder containing the text representation of the specified primitive valuethe value whose text representation to return value)Returns a |
| private static int | |
| private static int | Returns: The next power of 2 from x inclusiveValue to round up x)Calculate the next power of 2, greater than or equal to x. |
| pack-priv static int | |
| private static StringBuilder | |
| private static Unbox. |
| BITS_PER_INT | back to summary |
|---|---|
| private static final int BITS_PER_INT | |
| LOGGER | back to summary |
|---|---|
| private static final Logger LOGGER | |
| MASK | back to summary |
|---|---|
| private static final int MASK | |
| RINGBUFFER_MIN_SIZE | back to summary |
|---|---|
| private static final int RINGBUFFER_MIN_SIZE | |
| RINGBUFFER_SIZE | back to summary |
|---|---|
| private static final int RINGBUFFER_SIZE | |
| threadLocalState | back to summary |
|---|---|
| private static ThreadLocal<Unbox. | |
| webSafeState | back to summary |
|---|---|
| private static Unbox. | |
| Unbox | back to summary |
|---|---|
| private Unbox() | |
| box | back to summary |
|---|---|
| public static StringBuilder box(final float value) Returns a
| |
| box | back to summary |
|---|---|
| public static StringBuilder box(final double value) Returns a
| |
| box | back to summary |
|---|---|
| public static StringBuilder box(final short value) Returns a
| |
| box | back to summary |
|---|---|
| public static StringBuilder box(final int value) Returns a
| |
| box | back to summary |
|---|---|
| public static StringBuilder box(final char value) Returns a
| |
| box | back to summary |
|---|---|
| public static StringBuilder box(final long value) Returns a
| |
| box | back to summary |
|---|---|
| public static StringBuilder box(final byte value) Returns a
| |
| box | back to summary |
|---|---|
| public static StringBuilder box(final boolean value) Returns a
| |
| calculateRingBufferSize | back to summary |
|---|---|
| private static int calculateRingBufferSize(final String propertyName) | |
| ceilingNextPowerOfTwo | back to summary |
|---|---|
| private static int ceilingNextPowerOfTwo(final int x) Calculate the next power of 2, greater than or equal to x. From Hacker's Delight, Chapter 3, Harry S. Warren Jr.
| |
| getRingbufferSize | back to summary |
|---|---|
| pack-priv static int getRingbufferSize() For testing. | |
| getSB | back to summary |
|---|---|
| private static StringBuilder getSB() | |
| getState | back to summary |
|---|---|
| private static Unbox. | |
| Modifier and Type | Field and Description |
|---|---|
| private int | |
| private final StringBuilder[] |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public StringBuilder |
| current | back to summary |
|---|---|
| private int current | |
| ringBuffer | back to summary |
|---|---|
| private final StringBuilder[] ringBuffer | |
| State | back to summary |
|---|---|
| pack-priv State() | |
| getStringBuilder | back to summary |
|---|---|
| public StringBuilder getStringBuilder() | |
Such memory leaks will not occur if only JDK classes are stored in ThreadLocals.
| Modifier and Type | Field and Description |
|---|---|
| private final ThreadLocal | |
| private final ThreadLocal |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public StringBuilder |
| current | back to summary |
|---|---|
| private final ThreadLocal<int[]> current | |
| ringBuffer | back to summary |
|---|---|
| private final ThreadLocal<StringBuilder[]> ringBuffer | |
| WebSafeState | back to summary |
|---|---|
| private WebSafeState() | |
| getStringBuilder | back to summary |
|---|---|
| public StringBuilder getStringBuilder() | |