0

I have an app that initializes a logger like this:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {
    private static Logger logger = LoggerFactory.getLogger(Main.class);
    public static void main(String[] args) {
        logger.info("Logging something");
    }
}

I want it to read logging config from the log4j.properties file. I have found that I can make an explicit call to org.apache.log4j.PropertyConfigurator to accomplish that, but I was told the app should be able to find the properties file automatically.

Is there a way run the app so that SLF4J automatically configures log4j from the properties file?

Google Fail
  • 103
  • 1
  • 6
  • It's not SLF4J that needs to find the configuration file but rather the implementation behind the facade. Are you using Log4J or Log4J2? – Slaw Jan 17 '20 at 01:12
  • @Slaw, I'm using Log4j 1.2.17 – Google Fail Jan 17 '20 at 01:17
  • 1
    Note that log4j 1.x has been end-of-life since 2015. If you have control over the code base consider switching to log4j 2.x (or some other logging framework if you don't want to stay with Apache). That said, [this answer](https://stackoverflow.com/a/1140498/6395627) suggests log4j 1.x should automatically find the `log4j.properties` resource. Is that resource at the root of the classpath? – Slaw Jan 17 '20 at 01:29
  • 1
    Thanks for the reply. I think I found the issue. The JAR file we had deployed had it's own log4j.properties which was overriding the file that appeared later in the classpath. Adjusting the classpath should work. – Google Fail Jan 17 '20 at 02:18
  • see if this works ``` org.slf4j slf4j-log4j12 1.7.30 test ``` – prashant.kr.mod Jan 17 '20 at 02:49

0 Answers0