Home > Software design >  unable to write into different logs for different threads using log4j2
unable to write into different logs for different threads using log4j2

Time:05-10

I'm trying to write logs into different files for each Thread/ThreadGroup. My code is creating different files for each thread, but it writing all other threads logs into one file.

MainTest.java

public class MainTest {
    public static void main(String[] args) {
        ThreadGroup tg1 = new ThreadGroup("Group A");

        Thread t1 = new Thread(tg1, new LoggingTest(), "t1");
        t1.start();

        Thread t2 = new Thread(tg1, new LoggingTest(), "t2");
        t2.start();

        //
        Thread t3 = new Thread(new LoggingTest(),"t3");
        t3.start();
    }
}

LoggingTest.java

public class LoggingTest implements Runnable {
    private static final Logger logger      = LogManager.getLogger(LoggingTest.class);
    private static final String logFilePath = "/output/logs/";
    private static final String pattern     = "%d{dd MMM yyyy HH:mm:ss.SSS} (%F:%L) - %m%n";

    public void run() {
        Thread runner         = Thread.currentThread();
        
        LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
        try {
            URL configFile    = LoggingTest.class.getResource("/log4j2.properties");
            context.setConfigLocation(configFile.toURI());
        }catch(Exception e) {
            e.printStackTrace();
        }
        
        LoggerContext ctx     = (LoggerContext) LogManager.getContext(false);
        FileAppender appender = null;
        try {
            appender = FileAppender
                    .newBuilder()
                    .setName("Name")
                    .setLayout(PatternLayout.newBuilder().withPattern(pattern).build())
                    .withFileName(logFilePath   runner.getName()   ".log")
                    .build();
        } catch (Exception e) {
            logger.error("error -> " e);
        }
        
        if (appender != null) {
            if ( runner.getThreadGroup().getParent() != null )
            {
                appender.addFilter( new ThreadGroupFilter( runner.getThreadGroup() ) );
            }
            else
            {
                appender.addFilter( new ThreadFilter( runner ) );
            }
            appender.start();
            ctx.getRootLogger().addAppender(appender);
            ctx.updateLoggers();
        }

        logger.info("info msg");
        logger.debug("debug msg");
        logger.error("error msg");

        if ( appender != null ) {
            ctx.getRootLogger().removeAppender(appender);
            ctx.updateLoggers();
            appender.stop();
        }
    }
}

Here I created couple of filter classes for both Thread & ThreadGroup. But seems like these are not working properly. ThreadGroupFilter.java

import org.apache.logging.log4j.core.Filter;  

public class ThreadGroupFilter implements Filter {
        
        private final ThreadGroup filterThreadGroup;
    
        public ThreadGroupFilter( ThreadGroup filterThreadGroup ) {
            this.filterThreadGroup = filterThreadGroup;
        }
    
        @Override
        public Result filter(LogEvent event) {
            if ( Thread.currentThread().getThreadGroup() != filterThreadGroup )
                return Filter.Result.DENY;
    
            return Filter.Result.NEUTRAL;
        }
    }

ThreadFilter.java

public class ThreadFilter implements Filter {
    private final Thread filterThread;

    public ThreadFilter( Thread filterThread ) {
        this.filterThread = filterThread;
    }
    
    @Override
    public Result filter(LogEvent event) {
        if ( Thread.currentThread() != filterThread )
            return Filter.Result.DENY;

        return Filter.Result.NEUTRAL;
    }
}

log4j2.properties

name=PropertiesConfig
property.basePath = ../logs

appenders = console

appender.console.type = Console
appender.console.name = consoleLogger
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %5p [%c] [%t] (%F:%L) - %m%n

rootLogger.level = debug
rootLogger.appenderRefs = console
rootLogger.appenderRef.stdout.ref = consoleLogger

I'm expecting multiple log files for each Thread/ThreadGroup. But I'm ended up with multiple log files for each Thread and all threads logs into one file.

Thank you in advance.

CodePudding user response:

Each of your threads calls LoggerContext#setConfigLocation, which causes a reconfiguration of Log4j2. The reconfiguration removes all the appenders you added programmatically. That is why you end up with a single appender.

Remark that you do not need programmatic configuration to have a different file appender per thread. You can just use the RoutingAppender as in this question.

Edit: If you really want to use programmatic configuration (which is discouraged for compatibility reasons), you also need to use a unique name for each appender, otherwise they will be silently ignored (the Logger#addAppender method you use is not part of the public API, cf. javadoc, so errors are not reported).

CodePudding user response:

move this piece of code from LoggingTest to MainTest

LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
try {
    URL configFile    = LoggingTest.class.getResource("/log4j2.properties");
    context.setConfigLocation(configFile.toURI());
}catch(Exception e) {
    e.printStackTrace();
}

And minor modifications at Appender configuration.

FileAppender appender = null;
String appenderName = (runner.getThreadGroup() != null) ? "appender_" runner.getThreadGroup().getName() : "appender_" runner.getName();
try {
    appender = FileAppender
            .newBuilder()
            .setName(appenderName)
            .setLayout(PatternLayout.newBuilder().withPattern(pattern).build())
            .withFileName(logFilePath   appenderName   ".log")
            .build();
} catch (Exception e) {
    logger.error("error -> " e);
}

Thanks for the solution Poitr P. Karwasz

  • Related