소소한 개발팁

log4j xml, properties 등록하기.

roynus 2016. 3. 29. 18:51
log4j.xml 등록하기
package com.howtodoinjava;
 
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
 
public class Log4jXmlConfigurationExample
{
    static Logger logger = Logger.getLogger(Log4jXmlConfigurationExample.class);
 
    public static void main(String[] args)
    {
        //DOMConfigurator is used to configure logger from xml configuration file
        DOMConfigurator.configure("config/log4j-config.xml");
 
        //Log in console in and log file
        logger.debug("Log4j appender configuration is successful !!");
    }
}


log4j.properties 등록하기

Properties props = new Properties();
props.load(new FileInputStream("config/log4j.properties"));
PropertyConfigurator.configure(props);

If the properties file is in the jar, then you could do something like this:

Properties props = new Properties();
props.load(getClass().getResourceAsStream("/log4j.properties"));
PropertyConfigurator.configure(props);