First, I strongly recommend you switch from properties to either XML, Yaml, or 
JSON. 

To prgormatically configure Log4j2 do

  Configurator.initialize(“ConfigName”, “log4j2.xml”);

Note that there are several variations of the initialize method. If the 
configuration contains a monitorInterval than Log4j will automatically watch 
for changes.

Ralph

> On Mar 17, 2023, at 8:59 PM, Viraj Jasani <vjas...@apache.org> wrote:
> 
> Hi,
> 
> Could you please help with log4j2 replacement for PropertyConfigurator APIs
> configureAndWatch and configure?
> 
> For instance, this is the logic we have that we need to migrate to log4j2:
> 
> 
> 
>  protected void initLog() throws ServerException {
>    verifyDir(logDir);
>    LogManager.resetConfiguration();
>    File log4jFile = new File(configDir, name + "-log4j.properties");
>    if (log4jFile.exists()) {
>      PropertyConfigurator.configureAndWatch(log4jFile.toString(), 10 *
> 1000); //every 10 secs
>      log = LoggerFactory.getLogger(Server.class);
>    } else {
>      Properties props = new Properties();
>      try {
>        InputStream is = getResource(DEFAULT_LOG4J_PROPERTIES);
>        try {
>          props.load(is);
>        } finally {
>          is.close();
>        }
>      } catch (IOException ex) {
>        throw new ServerException(ServerException.ERROR.S03,
> DEFAULT_LOG4J_PROPERTIES, ex.getMessage(), ex);
>      }
>      PropertyConfigurator.configure(props);
>      log = LoggerFactory.getLogger(Server.class);
>      log.warn("Log4j [{}] configuration file not found, using default
> configuration from classpath", log4jFile);
>    }
>  }

Reply via email to