Swaranga Sarma
Swaranga Sarma

Reputation: 13413

[Log4j2]: Programmatically turn off logging for specific Java packages

I am using the following code to programatically configure my logging config:

 public static void configureLoggers() {
  ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();

  builder.setStatusLevel(Level.DEBUG);
  builder.setConfigurationName("ConfigName");

  AppenderComponentBuilder consoleAppenderBuilder = builder.newAppender("Stdout", "CONSOLE");
  consoleAppenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%m%n"));

  builder.add(consoleAppenderBuilder);

  RootLoggerComponentBuilder rootLoggerBuilder = builder.newRootLogger(LEVEL.DEBUG);
  rootLoggerBuilder.add(builder.newAppenderRef("Stdout")).addAttribute("additivity", true);
  builder.add(rootLoggerBuilder);

  LoggerContext.getContext(false).start(builder.build());
 }

I need to turn off logging for specific java packages in my projects; like org.glassfish. How do I do that?

Upvotes: 0

Views: 1666

Answers (2)

Swaranga Sarma
Swaranga Sarma

Reputation: 13413

Found it:

builder.add(builder.newLogger("org.glassfish", Level.OFF));

Upvotes: 0

rgoers
rgoers

Reputation: 9141

Assuming the org.glassfish loggers are obtaining their loggers using the class name as the logger name then you just need to add another logger to your configuration with "org.glassfish" as the logger name and then set it to whatever level you want it to filter at. If you don't specify an AppenderRef and it is set to additivity "true" (the default) then the events will automatically go to the root logger's appender.

Upvotes: 2

Related Questions