简体   繁体   中英

External log4j2 configuration file

I have a spring web application using log4j2 as a logging system. log4j2 : 2.6.2 slf4j : 1.7.21

i have got a log4j2.xml in my classpath used in production.

I want to use a different configuration file for development, especially when i run the server from Eclipse.

i tried to add -Dlog4j.configurationFile=C:\\Dev\\log4j2.xml to the Default VM arguments in eclipse as well as the server parameters but no luck so far, it's always picking up the log4j2.xml from the classpath

Any idea ?

Thank you

Try prepending the file with the file: prefix. -Dlog4j.configurationFile=file:///C:/Dev/log4j2.xml

If you're using a spring properties file you can also do:

logging.config = file:///C:/Dev/log4j2.xml

You can do the things in two ways.

  1. Application.properties
  2. VM Arguments under Run-Configurations.

Case 1 : Application-properties

(Mac OS) logging.config=file:/Users/XXX/XXX/XXX/log4j2.xml

(Windows OS) logging.config=file:/XXX/XXX/XXX/log4j2.xml

Case 2: Project Run-Configurations

-Dlogging.config=/Users/XXX/XXXX/log4j2.xml

Hope this helps to solve your problem.

Cheers!!!

We have requirement to config the external log file, if not exist then use default config file in WEB-INF.

Here is the code in our projects, which i used the load external configuration log file if exist, if not load default in WEB-INF:

public class Log4jLoader extends Log4jServletContextListener {

  private String LOG_FROM_CLASSPATH = "WEB-INF/log4j2.xml";

  @Override
  public void contextInitialized(ServletContextEvent event) {
    File f = new File(getLog4jPath());
    String path = f.exists() ? f.getAbsolutePath() : getFilefromClassPath(event);
    Configurator.initialize(null, path);
    LoggerFactory.getLogger(Log4jLoader.class)
        .info("load log4j2 from: {}", path);
  }
  private String getLog4jPath(){
       return "logFilePath";
  }
}

I tried below approach and it works for me;

String fullyQualifiedFilePath = "/mnt/test/log4j2local.properties"; Configurator.initialize(null,fullyQualifiedFilePath );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM