24

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.

Andy897
  • 6,915
  • 11
  • 51
  • 86
  • Please refer this link : [Very simple log4j2 XML configuration file using Console and File appender](http://stackoverflow.com/questions/21206993/very-simple-log4j2-xml-configuration-file-using-console-and-file-appender). – Rohit Gulati Jan 03 '17 at 11:07
  • Thanks a lot. In that case, I should not even put anything in code? This is for a test package. So shall I just create the config file and remove all the setup code ? And it will auto initialize things reading from config file. – Andy897 Jan 03 '17 at 11:12
  • Yes either do in code or in config file.Refer [Apache log4j 2 docs](https://logging.apache.org/log4j/2.x/manual/configuration.html) – Rohit Gulati Jan 03 '17 at 11:18

2 Answers2

24

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);
John Smith
  • 256
  • 2
  • 5
6
Configurator.initialize(new DefaultConfiguration());
Robert Tupelo-Schneck
  • 10,047
  • 4
  • 47
  • 58