Description
With each logging call, context map data is copied from the ThreadContext map into the LogEvent.
The LogEvent interface exposes this data via the getContextMap() : Map<String, String> method, and this is implementated by storing the data in a Map<String, String>. The JDK Map is not an easy data structure to make garbage-free. It would also be nice to have the ability in LogEvent to carry data of any type.
One idea is to introduce a small interface that is general enough to be map-like but can be implemented in a garbage-free manner.
LogEvent implementations would have an instance of this interface instead of the current java.util.Map<String, String> contextMap attribute.
The interface could look something like this:
interface ContextData<String, V> { /** Called to implement {@link LogEvent#getContextMap()}. */ Map<String, String> asMap(); /** Put key-value pair into the table. Remove key if value is null. */ void put(String key, V value); /** Returns the value for the specified key. */ V getValue(String key); /** Number of key-value pairs. */ int size(); /** Removes all key-value pairs. */ void clear(); // Instead of Iterators, client code provides the consumer. /** * Performs the given action for each key-value pair in this data structure * until all entries have been processed or the action throws an exception. */ void forEach(BiConsumer<String, ? super V> action); } /** * An operation that accepts two input arguments and returns no result. */ interface BiConsumer<T,U> { /** Performs the operation given the specified arguments. */ void accept(T t, U u); }
The LogEvent interface would have an additional method getContextData() : ContextData<K, V> that gives downstream components direct access to the new data structure.
Existing downstream components would still be able to call logEvent.getContextMap() to get a Map<String, String> view of the context map data and this would work as expected.
Attachments
Issue Links
- is depended upon by
-
LOG4J2-1401 Support changing the log level for all messages related to some domain object
- Closed
-
LOG4J2-1010 Injectable context properties
- Closed
- is related to
-
LOG4J2-1648 Allow non-String values in the ThreadContext
- Open
-
LOG4J2-1349 Garbage-free ThreadContext map
- Closed
-
LOG4J2-1607 Improve performance of SortedArrayStringMap data structure
- Closed
- is superceded by
-
LOG4J2-1681 Introduce interfaces IndexedStringMap and IndexedReadOnlyStringMap
- Closed
- relates to
-
LOG4J2-1516 Add ThreadContextMap.putAll(Map<String, String>)
- Resolved
-
LOG4J2-1517 Add ThreadContext.setContext(Map<String, String>)
- Resolved
-
LOG4J2-1519 Add ThreadContext.putAll(Map<String, String>)
- Closed