1

I have a web app built in Eclipse/STS with Spring MVC and Maven.

I want to add logging, so I added SLF4 and Log4J to the pom.xml like this ..

     <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.30</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.30</version>
        <scope>test</scope>
    </dependency>

     <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.30</version>
        <scope>test</scope>
    </dependency>

I have a simple log4j.properties file in the project/src/main/resources folder, like this ...

# Root logger option
log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

I created a Logger class in my main WebController, like this ...

private static Logger logger = LoggerFactory.getLogger(WebController.class);

And in my "showMain" method I'm doing some logging stuff, like this ...

@RequestMapping(value={"", "/", "showhome"}, method={RequestMethod.POST,RequestMethod.GET})
public ModelAndView showHome(Model model) {

    logger.info("########### TEST LOG INFO");
    logger.error("########### TEST LOG ERROR");
    logger.warn("########### TEST LOG WARN");
    logger.debug("########### TEST LOG DEBUG");

    /* ... */
}

But when I run the application, I don't see any logging output in the console.

I also don't see anything in the console output to indicate that it is even using the logging framework. And there's no "can't find log4j.properties" message or anything.

I tried putting the log4j.properties in different places in the project, but nothing.

I must be missing something simple? What have I missed?

2 Answers2

2

In your pom.xml is missing the logging engine, you could use log4j2. Please Consider to use the latest version of log4j2 instead of log4j (1.2.x) because you could take advance of:

  1. Lazy log: log4j(1.2.x) build a string also if a level is not activated
  2. lambda in order to avoid evaluation of expensive methods
  3. A lot of appenders for modern platform
  4. More easy way to configure a lot of parameters (reload configuration, appenders, ...)

pom.xml

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.13.3</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.13.3</version>
  </dependency>
</dependencies>
melagrana
  • 21
  • 2
  • You are right, but I got a similar answer from another user so I marked theirs correct first. Thanks for the help! – Elliot.Easton.2020 May 29 '20 at 21:22
  • @Elliot.Easton.2020 This is a better answer though. The latest version of `log4j` (1.2.17) is from May 2012. There's no reason not to use a maintained version that is updated with modern tools. – Babyburger Dec 20 '20 at 14:39
0

You stated that you added log4j in your pom.xml but I do not see it, are you sure this is your newest version of pom.xml?

    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
zawarudo
  • 1,907
  • 2
  • 10
  • 20
  • That's what was missing! Thanks. I incorrectly assumed that slf4j-log4j12 was enough. Thanks! – Elliot.Easton.2020 May 29 '20 at 17:43
  • Thanks for the help, just FYI, I posted another Log4j question, if you could take a look that would be great! https://stackoverflow.com/questions/62094862/why-is-there-log4j-output-to-the-console-from-other-code-but-not-from-my-code – Elliot.Easton.2020 May 29 '20 at 21:48
  • 1
    Sorry man, I was out for a couple of days, I checked, glad you found an answer in your second question. Cheers! – zawarudo May 31 '20 at 08:52