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

Remko Popma commented on LOG4J2-1010:
-------------------------------------

The best place to inject context map data into the LogEvent is probably the 
place where the LogEvent is created or (in the case of reused mutable log 
events) initialized. 

Side note: Doing this will allow users to inject any kind of data into the 
LogEvent (not just Strings), and this data can come from anywhere (not 
necessarily from ThreadContext). See also LOG4J2-1349, which proposes to make 
the LogEvent internal data structure more general to support this. The below 
assumes that LogEvents will use the {{CustomData<K, V>}} data structure 
proposed in LOG4J2-1349 instead of the current {{Map<String, String>}}.

One idea is to introduce an interface like this:

{code}
/**
 * Updates the specified LogEvent's CustomData with context values.
 * By default context data is obtained from the ThreadContext but implementers 
are free to do something different.
 *
 * @param properties Properties from the configuration to be added to the log 
event
 * @event the log event whose CustomData to initialize
 */
interface ContextDataInjector {
    void injectContextData(List<Property> properties, LogEvent event);
}

// Default implementation that copies data from ThreadContext.
// The Scala version of this will likely use Local to get Context key-value 
pairs.
class DefaultContextDataInjector implements ContextDataInjector {
    public void injectContextData(List<Property> properties, LogEvent event) {
        Map<String, String> map = Log4jLogEvent.createMap(properties);
        event.getCustomData().clear();
        for (String key : map.keySet()) {
            event.getCustomData().put(key, map.get(key));
        }
    }
}

// example usage in DefaultLogEventFactory
public class DefaultLogEventFactory implements LogEventFactory {
    private ContextDataInjector injector = ContextDataInjectorFactory.create();

    public LogEvent createEvent(final String loggerName, final Marker marker,
                                final String fqcn, final Level level, final 
Message data,
                                final List<Property> properties, final 
Throwable t) {
        LogEvent result = new Log4jLogEvent(loggerName, marker, fqcn, level, 
data, t);
        injector.injectContextData(properties, result);
        return result;
    }
}
{code}

> Injectable context properties
> -----------------------------
>
>                 Key: LOG4J2-1010
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1010
>             Project: Log4j 2
>          Issue Type: Improvement
>          Components: API
>    Affects Versions: 2.2
>            Reporter: Mikael Ståldal
>         Attachments: properties.patch
>
>
> It would be useful to have a way to inject context properties into a 
> {{LogEvent}}, as an alternative to {{ThreadContext}}.
> In an asynchronous environment, using ThreadContext as currently implemented 
> is not so useful since JVM threads might not be coupled to the logical flow 
> of the application.



--
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