How to configure log4j with a properties file

前端 未结 9 1915
春和景丽
春和景丽 2020-11-29 03:01

How do I get log4j to pick up a properties file.

I\'m writing a Java desktop app which I want to use log4j. In my main method if have this:

   Proper         


        
9条回答
  •  我在风中等你
    2020-11-29 03:42

    import org.apache.log4j.PropertyConfigurator;
    

    Import this, then:

    Properties props = new Properties();
    InputStream is = Main.class.getResourceAsStream("/log4j.properties");
    
    try {
        props.load(is);
    } catch (Exception e) {
        // ignore this exception
        log.error("Unable to load log4j properties file.",e);
    }
    PropertyConfigurator.configure(props);
    

    My java files directory like this:

    src/main/java/com/abc/xyz
    

    And log4j directory like this:

    src/main/resources
    

提交回复
热议问题