0

I am creating a sample Log4j2 configuration with Asyn Appender, After the executing is complete the thread which is generated by AsyncAppender is not killed? is it a bug or any configuration is explicit to kill the thread.

My sample summary Appender

<!-- ####################### SUMMARY FILE APPENDER ####################### -->
    <RollingFile name="SUMMARY_ALL" fileName="./logs/summary.log"
        filePattern="logs/$${date:yyyy-MM}/summary-%d{yyyy-MM-dd-HH}-%i.log.gz">
        <PatternLayout>
            <pattern>%d{ISO8601} [%t] %p %c %L - %m%n</pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy interval="6"
                modulate="true" />
            <SizeBasedTriggeringPolicy size="10 MB" />
        </Policies>
    </RollingFile>

Sample Logger as below

  <logger name="com.test.learn" level="DEBUG">
<appender-ref ref="Async" />
  </logger>

Sample code package com.test.learn;

import org.apache.logging.log4j.LogManager;

public class TestLogger {
private static org.apache.logging.log4j.Logger log = LogManager
        .getLogger(TestLogger.class);

public static void main(String[] args) {
    log.info("testing logger");
}

}

after this is executed, the java process should exit, but its not. Can anyone please help me.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
chaosguru
  • 1,933
  • 4
  • 30
  • 44

3 Answers3

1

This will shut down the logging sub system and stop any async threads:

    ((LifeCycle) LogManager.getContext()).stop();

(This needs to be better documented...)

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • i upgraded to beta-9 and it worked properly, I think the same issue was with tomcat as well which is fixed by log4j2 guys. – chaosguru Oct 02 '13 at 13:16
  • In future please mention if your problem is with a web application. The question sample code has a main method which kind of put me on the wrong track... In general more details like OS, java version, and versions of related software (web container) and the problematic software will be useful to people trying to help you with your problem. – Remko Popma Oct 02 '13 at 14:15
  • I think the problem statement is very clear that it is with sample code with core execution , I meant to only say that after some googling I could read that there was some problem with tomcat logging as well hence told. I know very clear what I am posting. – chaosguru Oct 02 '13 at 14:37
0

I Upgraded to log4j2-beta-9 and it works fine.

chaosguru
  • 1,933
  • 4
  • 30
  • 44
0

What worked best for me in a web application context was

org.apache.logging.log4j.LogManager.shutdown();
mblaszczyk
  • 21
  • 1
  • 3