1

I have a particular JUnit test which processes a big data file and when the logging level is left on TRACE, it kills Eclipse - something to do with the console handling, which is not relevant to this question.

I often switch between running all my tests using m2e, in which case I don't need any debugging log output, and running individual tests, where I want often want to see the TRACE output.

To avoid the necessity of editing my log4j2.xml config every time, I coded the log4j config to increase the logging level to INFO in this particular test, like this from programmatically-change-log-level-in-log4j2:

@Before
public void beforeTest() {
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration config = ctx.getConfiguration();
    LoggerConfig loggerConfig = config.getLoggerConfig(
        LogManager.ROOT_LOGGER_NAME);
    initialLogLevel = loggerConfig.getLevel();
    loggerConfig.setLevel(Level.INFO);
}

But it has no effect.

If the "ROOT_LOGGER" that I am manipulating here represents the same logger as the <root> in my log4j2.xml, then this is not going to work, is it? I need to override all the other loggers, or shut it down completely, but how?

Could it be influenced by my use of slf4j as the log4j2 wrapper in all of my other classes?

I have tried getting hold of the Appenders and using append.stop() but that doesn't work.

Community
  • 1
  • 1
Adam
  • 5,215
  • 5
  • 51
  • 90

1 Answers1

0

You can put a log4j2 config file in src/test/resources/ directory. During unit tests that file will be used.

Jose Martinez
  • 11,452
  • 7
  • 53
  • 68
  • 1
    i don't think you understood the question. Both scenarios for which I want different logging configs are testing scenarios. Please re-read the second paragraph. – Adam Jul 19 '15 at 10:34
  • Thanks for clarifying without downvoting. I have done this before, let me look up my old code and provide another answer. – Jose Martinez Jul 19 '15 at 12:37
  • @Adam, i looked through my code and I noticed some differences that would not make it appropriate for your specific situation. I actually create an individual logger for each specific need. WHich is not the same for what you are trying to do. I agree that the questions you pose in your post are key to getting the right answer. – Jose Martinez Jul 20 '15 at 13:59