简体   繁体   中英

Log4j2 - Cannot write logs to a file

I'm attempting to use log4j2 to write log statements to both console and log files. However, when I check the log folder, nothing is being created.

The idea is to use the RoutingAppender to have all the test classes of each package in one folder.

Here is the log4j2.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Properties>
        <Property name="LOG_PATTERN">%d{HH:mm:ss.SSS} [%t] %-5level [%5X{tId}] %logger{36} - %msg%n</Property>
    </Properties>
    <Appenders>
        <Routing name="MyRoutingAppender">
            <Routes pattern="$${ctx:threadName}">
                <Route>
                    <RollingFile
                            fileName="logs/${ctx:testGroupName}/${ctx:threadName}.log"
                            name="appender-${ctx:threadName}"
                            filePattern="logs/${date:yyyy-MM}/${ctx:threadName}-%d{yyyy-MM-dd}-%i.log.gz">
                        <PatternLayout pattern="${LOG_PATTERN}"/>
                        <Policies>
                            <TimeBasedTriggeringPolicy interval="6" modulate="true" />
                            <SizeBasedTriggeringPolicy size="10 MB" />
                        </Policies>
                    </RollingFile>
                </Route>
            </Routes>
        </Routing>
        <Console name="LogToConsole" target="SYSTEM_OUT">
            <PatternLayout pattern="${LOG_PATTERN}"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="TestCases" level="info" additivity="false">
            <AppenderRef ref="LogToConsole"/>
            <AppenderRef ref="MyRoutingAppender"/>
        </Logger>
        <Logger name="PageObjects" level="info" additivity="false">
            <AppenderRef ref="LogToConsole"/>
            <AppenderRef ref="MyRoutingAppender"/>
        </Logger>
        <Root level="info">
            <AppenderRef ref="LogToConsole"/>
        </Root>
    </Loggers>
</Configuration>

Here is what I'm having in my java class:

    package TestCases.Groups;

    public class AddDeleteGroup extends TestInit {
        private final Logger logger = LogManager.getLogger(AddDeleteGroup.class.getName());

        @BeforeClass
        public void beforeClass() {
            String threadId = String.valueOf(Thread.currentThread().getId());
            ThreadContext.put("tId", threadId);
            ThreadContext.put("testGroupName", "TestCases.Groups");
            ThreadContext.put("threadName", AddDeleteGroup.class.getName());
        }

        @Test(groups = "test")
        public void setUp(){
            logger.info("SetUp method for {}", AddDeleteGroup.class.getName());
            // do stuffs
        }

        @Test(dependsOnMethods={"SetUp"})
        public void testRunner(){
            logger.info("TestRunner method for {}", AddDeleteGroup.class.getName());
            // do stuffs
        }

The console shows the log statements being printed out, but when I check the logs folder, I do not see anything.

If anyone has any idea what I'm doing wrong, please help me out. Thanks in advance.

Here is for you. I've tried and it works.
JAVA - Separate log files for threads using log4j2

It turns out the "*.log" file wasn't being recognized in IntelliJ. When I tried changing the file type to "*.txt" , the log files are being displayed in the folder.

The configuration works as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM