I am working on a log4j 1 to log4j 2 migration. Inside a AppConfigInitializer file we use something like this -
BasicConfigurator.configure(consoleAppender);
What shall I replace this with to get it working in log4j2. I am using the backward compatibility bridge as well but that has broken BasicConfigurator implementation.
I think I need to do this using config file, but not able to find how.
public class BasicConfigurator extends Object. Configures the package. For file based configuration, see PropertyConfigurator . For XML based configuration, see DOMConfigurator .
What BasicConfigurator. configure() do is adding root logger to a ConsoleAppender and set the appender layout to a PatternLayout with the pattern "%r [%t] %-5p %c - %m%n". So you need to set the root logger's level.
Community support: Log4j 1. x is not actively maintained, whereas Log4j 2 has an active community where questions are answered, features are added and bugs are fixed. Automatically reload its configuration upon modification without losing log events while reconfiguring.
The solution above works, but since default log level is ERROR, not all logs are shown. Log level can be adjusted with Configurator.setRootLevel method:
Configurator.initialize(new DefaultConfiguration());
Configurator.setRootLevel(Level.INFO);
Configurator.initialize(new DefaultConfiguration());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With