1

Migrating log4j 1.2.x to log4j 2.16.0

Using PropertyConfigurator.configure method to load all log4j file are located at specific location on the system.

static {
   PropertyConfigurator.configure("C:/users/log4j.properties").
}

What is alternative way for configure log4j properties from external location?

What is alternative way of using PropertyConfigurator.configure in log4j2?

  • 2
    Does this answer your question? [How to configure log4j with a properties file](https://stackoverflow.com/questions/2288876/how-to-configure-log4j-with-a-properties-file) – Martin Zeitler Dec 17 '21 at 06:05
  • to load log4j.properties from external location. –  Dec 17 '21 at 06:08
  • Above link is specific to log4j 1.x version where we have support for PropertyConfigurator class. but in log4j 2.x this support is not exists then what should be approach to achieve the same –  Dec 17 '21 at 06:15
  • Does this answer your question? [PropertyConfigurator in log4j2](https://stackoverflow.com/questions/32043770/propertyconfigurator-in-log4j2) – Leponzo Aug 08 '22 at 19:01

1 Answers1

-1

You have to do some code changes after adding new dependencies.

Add the following dependencies to the pom.xml

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.16.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.16.0</version>
    </dependency>

replace your java class file import import org.apache.log4j.Logger; with import org.apache.logging.log4j.LogManager; and import org.apache.logging.log4j.Logger;

then replace private static final Logger logger = Logger.getLogger(Test.class); with private static final Logger logger = LogManager.getLogger(Test.class);

For further reference: click here

Miran Senanayaka
  • 154
  • 1
  • 13
  • As there is change in package structure from log4j1.x to log4j2.x so we have change these statement but my question is not related changing package –  Dec 17 '21 at 08:09
  • From Log4J 2's documentation: https://logging.apache.org/log4j/2.0/faq.html#reconfig_from_code – Miran Senanayaka Dec 20 '21 at 07:27