1

I am trying to set up log4j in IntelliJ and having an issue. I am able to see console output, but no log file. My config is located at src/main/resources/Log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT">
        <!--<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> -->
        <PatternLayout pattern="%d{HH:mm}%-5level %logger{36} - %msg%n" />
    </Console>
    <File name="FileAppender"
          fileName="productivity.log"
          immediateFlush="false"
          append="true">
        <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>
</Appenders>
<Loggers>
    <Root level="debug">
        <AppenderRef ref="ConsoleAppender" />
        <AppenderRef ref="FileAppender"/>
    </Root>
</Loggers>
</Configuration>

I have done a lot of searching, most of the questions I found simply say to place the log4j2.xml file in src/main/resources, which I did. I also tried the suggestion here but that had no effect.

Also, this is a maven project...

I am currently just trying to run a test that prints one line of text, and no file is created. Any ideas?

Update

After some further testing and going thru the docs I was actually able to get log output, but only when the application context loadded.

When I run a unit test by itself, no log file is created, but I do get the console output.

The log4j2.xml still seems to be ignored, however. I my application.properties I entered

logging.file.path=log
logging.level.root=debug
logging.level.org.springframework.web=debug

and my logging output did come out in the log file, however, I defined my logger like so...

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
// other code deleted for brevity...

private final static Logger logger = LogManager.getLogger(WidgetController.class);

I added this line in the method where the log message was called...

    logger.info("in getAllWidgets");
    logger.info("logger class : " + logger.getClass());

which created this output:

c.s.w.t.S.controller.WidgetController    : in getAllWidgets
c.s.w.t.S.controller.WidgetController    : logger class : class org.apache.logging.slf4j.SLF4JLogger
  • why is the class of the logger class org.apache.logging.slf4j.SLF4JLogger when it was clearly defined as a log4j logger?
  • if I look in my App/target/classes folder and see my log4j2.xml file there, does that mean it was successfully added to the classpath?

Also, this project was created by SpringInitializr.

badperson
  • 1,554
  • 3
  • 19
  • 41
  • Please share the [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). See also my answer at https://stackoverflow.com/a/42200519/104891. It has a sample project linked which logs into a file using log4j. Does it work for you? – CrazyCoder Dec 03 '21 at 19:52
  • does the fact that my project is a maven project make a difference? – badperson Dec 03 '21 at 20:24
  • Directories layout would be different for Maven, otherwise it should not matter. – CrazyCoder Dec 03 '21 at 20:25
  • I was able to start a new project and get the log file to generate, but when I import a project from Spring Initializr the problem persists – badperson Dec 05 '21 at 13:39

0 Answers0