Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BasicConfigurator replacement in log4j2

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.

like image 847
Andy897 Avatar asked Jan 03 '17 11:01

Andy897


People also ask

What is BasicConfigurator in Log4j?

public class BasicConfigurator extends Object. Configures the package. For file based configuration, see PropertyConfigurator . For XML based configuration, see DOMConfigurator .

What is the use of BasicConfigurator configure ()?

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.

What is the difference between Log4j and log4j2?

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.


2 Answers

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);
like image 83
John Smith Avatar answered Sep 23 '22 07:09

John Smith


Configurator.initialize(new DefaultConfiguration());
like image 25
Robert Tupelo-Schneck Avatar answered Sep 25 '22 07:09

Robert Tupelo-Schneck