[
https://issues.apache.org/jira/browse/LOG4J2-3009?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17274527#comment-17274527
]
Carter Kozak commented on LOG4J2-3009:
--------------------------------------
Hi [~Rohan438], Could you share the precise JVM version you're using? Log4j
attempts to cache the thread name in environments (java 7 and some older java 8
releases) where garbage is created withing `thread.getName()`.
See this code for details:
https://github.com/apache/logging-log4j2/blob/9fabec5277f06f59ceaeff20da055ab7bf1952c4/log4j-core/src/main/java/org/apache/logging/log4j/core/async/ThreadNameCachingStrategy.java#L69-L85
It's very possible the detection logic is incorrect on some builds of java8, if
you could provide the value of {{System.getProperty("java.version")}} I'd
appreciate it!
The {{log4j2.asyncLoggerThreadNameStrategy}} may be set to {{UNCACHED}} to
override this default on all systems. It may be set in a
log4j2.component.properties file, or via a jvm system property.
It's a tad odd that these are described as properties for async logging, in
reality they're more closely associated with gc-logging, and don't really
relate to asynchronous features. Likely a historical artifact.
Docs:
https://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties
https://logging.apache.org/log4j/2.x/manual/async.html#SysPropsAllAsync
> Change of java Thread name is not reflecting in log4j2 logs (pattern %t)
> ------------------------------------------------------------------------
>
> Key: LOG4J2-3009
> URL: https://issues.apache.org/jira/browse/LOG4J2-3009
> Project: Log4j 2
> Issue Type: Bug
> Components: Core, Pattern Converters
> Affects Versions: 2.8.2
> Reporter: Rohan Ramchander
> Priority: Major
> Original Estimate: 336h
> Remaining Estimate: 336h
>
> I have Java based application with a single thread connected to JBOSS MQ.
> Whenever there is a message in the queue then it pulls the message and sets
> the thread name with one of the message's attribute (REQUEST_ID) and
> continues processing. After application startup, the logging is fine for the
> first message. But from the second message, the change in the thread name is
> not reflecting in the logs.
> h2. Example code
> {code:java}
> private static Logger logger =
> LogManager.getLogger(EngineManager.class.getName());
> private synchronized void onMessage(javax.jms.Message msg) {
> Thread.currentThread().setName(msg.getStringProperty("REQUEST_ID"));
> logger.info("Received a Message Event: Time spent:" +
> (System.currentTimeMillis()-msg.getJMSTimestamp()) + "ms");
> // ...
> }
> {code}
> h2. Configuration
> {code:java}
> appender.console.layout.pattern = %d\{ABSOLUTE} %c{1} [%t] - %m%n
> {code}
> h2. Output
> {code:java}
> 19:29:52,726 EngineManager [sddsdsdgdd-dfvdddbfvs-2343412] - Received a
> Message Event: Time spent: 44ms
> {code}
>
> Rest everything is fine that's why I have given small portion of the code.
> What I need is when there is a second message (so on) in the queue then above
> REQUEST_ID should change in the logs. It is always same for all the messages.
> I tried different ways like {{LoggerContext.reconfigure()}},
> {{LogManager.shutdown()}} and then {{getLogger()}} etc but couldn't succeed.
> This used to work fine with log4j 1x where the change in the thread name
> immediately reflects in the logs.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)