Java Code Examples for org.apache.logging.log4j.LogManager#getRootLogger()

The following examples show how to use org.apache.logging.log4j.LogManager#getRootLogger() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProjectFileManagerTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void assertEventsSize(int size) {
	waitForPostedSwingRunnables();
	int eventCount = events.size();
	if (eventCount == size) {
		return; // all is well
	}

	// need to print an error and then fail
	Logger rootLogger = LogManager.getRootLogger();
	rootLogger.error("Expected " + size + " events and found: " + eventCount);
	for (MyEvent event : events) {
		rootLogger.error("\tevent: " + event);
	}

	Assert.fail(
		"Expected " + size + " events and found: " + eventCount + "(see log for details)");
}
 
Example 2
Source File: CsvParameterLayoutTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testLogJsonArgument() throws InterruptedException {
    final ListAppender appender = (ListAppender) init.getAppender("List");
    appender.countDownLatch = new CountDownLatch(4);
    appender.clear();
    final Logger logger = (Logger) LogManager.getRootLogger();
    final String json = "{\"id\":10,\"name\":\"Alice\"}";
    logger.error("log:{}", json);
    // wait until background thread finished processing
    final int msgCount = 1;
    if (appender.getMessages().size() < msgCount) {
        appender.countDownLatch.await(5, TimeUnit.SECONDS);
    }
    assertEquals("Background thread did not finish processing: msg count", msgCount, appender.getMessages().size());

    // don't stop appender until background thread is done
    appender.stop();
    final List<String> list = appender.getMessages();
    final String eventStr = list.get(0).toString();
    Assert.assertTrue(eventStr, eventStr.contains(json));
}
 
Example 3
Source File: DeobfuscatingRewritePolicy.java    From VanillaFix with MIT License 6 votes vote down vote up
/** Modifies the log4j config to add the policy **/
public static void install() {
    Logger rootLogger = (Logger) LogManager.getRootLogger();
    LoggerConfig loggerConfig = rootLogger.get();

    // Remove appender refs from config
    List<AppenderRef> appenderRefs = new ArrayList<>(loggerConfig.getAppenderRefs());
    for (AppenderRef appenderRef : appenderRefs) {
        loggerConfig.removeAppender(appenderRef.getRef());
    }

    // Create the RewriteAppender, which wraps the appenders
    RewriteAppender rewriteAppender = RewriteAppender.createAppender(
            "VanillaFixDeobfuscatingAppender",
            "true",
            appenderRefs.toArray(new AppenderRef[0]),
            rootLogger.getContext().getConfiguration(),
            new DeobfuscatingRewritePolicy(),
            null
    );
    rewriteAppender.start();

    // Add the new appender
    loggerConfig.addAppender(rewriteAppender, null, null);
}
 
Example 4
Source File: MCRLoggingCommands.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param name
 *            the name of the java class or java package to set the log
 *            level for
 * @param logLevelToSet
 *            the log level to set e.g. TRACE, DEBUG, INFO, WARN, ERROR and
 *            FATAL, providing any other value will lead to DEBUG as new log
 *            level
 */
@MCRCommand(syntax = "change log level of {0} to {1}",
    help = "{0} the package or class name for which to change the log level, {1} the log level to set.",
    order = 10)
public static synchronized void changeLogLevel(String name, String logLevelToSet) {
    LOGGER.info("Setting log level for \"{}\" to \"{}\"", name, logLevelToSet);
    Level newLevel = Level.getLevel(logLevelToSet);
    if (newLevel == null) {
        LOGGER.error("Unknown log level \"{}\"", logLevelToSet);
        return;
    }
    Logger log = "ROOT".equals(name) ? LogManager.getRootLogger() : LogManager.getLogger(name);
    if (log == null) {
        LOGGER.error("Could not get logger for \"{}\"", name);
        return;
    }
    LOGGER.info("Change log level from {} to {}", log.getLevel(), newLevel);
    Configurator.setLevel(log.getName(), newLevel);
}
 
Example 5
Source File: Log4J2Appender.java    From javamelody with Apache License 2.0 5 votes vote down vote up
void register() {
	if (LogManager.getContext(false) instanceof LoggerContext) {
		final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
		if (ctx.getConfiguration() instanceof AbstractConfiguration) {
			final AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration();
			final Appender appender = getSingleton();
			appender.start();
			config.addAppender(appender);
			final Logger rootLogger = LogManager.getRootLogger();
			final LoggerConfig loggerConfig = config.getLoggerConfig(rootLogger.getName());
			loggerConfig.addAppender(appender, null, null);
			ctx.updateLoggers();
		}
	}
}
 
Example 6
Source File: LoggingListener.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Obtain the logger with the given name.
 *
 * @param loggerName the logger to obtain
 * @return the logger
 */
private static Logger resolveLogger(String loggerName) {
    if (loggerName.equalsIgnoreCase("_root")) {
        return LogManager.getRootLogger();
    }
    return LogManager.getLogger(loggerName);
}
 
Example 7
Source File: Log4J2Appender.java    From javamelody with Apache License 2.0 5 votes vote down vote up
void deregister() {
	if (LogManager.getContext(false) instanceof LoggerContext) {
		final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
		if (ctx.getConfiguration() instanceof AbstractConfiguration) {
			final AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration();
			final Appender appender = getSingleton();
			appender.stop();
			config.removeAppender(appender.getName());
			final Logger rootLogger = LogManager.getRootLogger();
			final LoggerConfig loggerConfig = config.getLoggerConfig(rootLogger.getName());
			loggerConfig.removeAppender(appender.getName());
			ctx.updateLoggers();
		}
	}
}
 
Example 8
Source File: PGMServer.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
protected Logger setupLogger() {
  AnsiConsole.systemInstall();

  final java.util.logging.Logger global = java.util.logging.Logger.getLogger("");
  global.setUseParentHandlers(false);

  final Handler[] handlers = global.getHandlers();
  for (int i = 0; i < handlers.length; ++i) {
    global.removeHandler(handlers[i]);
  }

  global.addHandler(new ForwardLogHandler());
  org.apache.logging.log4j.Logger logger = LogManager.getRootLogger();

  if (logger instanceof org.apache.logging.log4j.core.Logger) {
    final Iterator<org.apache.logging.log4j.core.Appender> appenders =
        ((org.apache.logging.log4j.core.Logger) logger).getAppenders().values().iterator();
    while (appenders.hasNext()) {
      final org.apache.logging.log4j.core.Appender appender = appenders.next();
      if (appender instanceof ConsoleAppender) {
        ((org.apache.logging.log4j.core.Logger) logger).removeAppender(appender);
      }
    }
  }

  new Thread(new TerminalConsoleWriterThread(System.out, reader)).start();
  System.setOut(new PrintStream(new LoggerOutputStream(logger, Level.INFO), true));
  System.setErr(new PrintStream(new LoggerOutputStream(logger, Level.WARN), true));

  BasicConfigurator.resetConfiguration();

  try {
    return (Logger) PGMServer.class.getField("LOGGER").get(PGMServer.class);
  } catch (IllegalAccessException | NoSuchFieldException e) {
    // No-op
  }

  return logger;
}
 
Example 9
Source File: LogUtil.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
public static void configureLogLevel(String logLevel) {

        Level level = Level.toLevel(logLevel);
        try {
            final Logger logger = (Logger) LogManager.getRootLogger();
            logger.setLevel(level);
        } catch (Throwable t) {
            //We catch throwable as there is a case where logger level setting fails when old SLF4j library interferes.
            log.error("Could not set the log level to : " + level + ". Probably inconsistent Log4J class is loaded.",
                    t);
        }
    }
 
Example 10
Source File: LogUtil.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
public static void configureAndAddConsoleAppender() {

        final NullAppender appender = new NullAppender("NullAppender", null, null);
        appender.start();
        final Logger logger = (Logger) LogManager.getRootLogger();
        logger.addAppender(appender);

    }
 
Example 11
Source File: LoggingStrategyFunctionalTest.java    From pocket-etl with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    logger = (Logger) LogManager.getRootLogger();
    appender = OutputStreamAppender.newBuilder().setName("Appender").setTarget(out).build();
    appender.start();
    logger.addAppender(appender);
    logger.setLevel(Level.INFO);
}
 
Example 12
Source File: ConsoleAppender.java    From DiscordSRV with GNU General Public License v3.0 4 votes vote down vote up
public void shutdown() {
    Logger rootLogger = (Logger) LogManager.getRootLogger();
    rootLogger.removeAppender(this);
}
 
Example 13
Source File: ConsoleAppender.java    From DiscordSRV with GNU General Public License v3.0 4 votes vote down vote up
public ConsoleAppender() {
    super("DiscordSRV-ConsoleChannel", null, PATTERN_LAYOUT, false);

    Logger rootLogger = (Logger) LogManager.getRootLogger();
    rootLogger.addAppender(this);
}
 
Example 14
Source File: LogUtil.java    From iaf with Apache License 2.0 4 votes vote down vote up
public static Logger getRootLogger() {
	return LogManager.getRootLogger();
}
 
Example 15
Source File: OnStartupTasks.java    From AuthMeReloaded with GNU General Public License v3.0 4 votes vote down vote up
private static void setLog4JFilter() {
    org.apache.logging.log4j.core.Logger logger;
    logger = (org.apache.logging.log4j.core.Logger) LogManager.getRootLogger();
    logger.addFilter(new Log4JFilter());
}
 
Example 16
Source File: SystemPropsTestLoggingListener.java    From crate with Apache License 2.0 4 votes vote down vote up
private static Logger resolveLogger(String loggerName) {
    if (loggerName.equalsIgnoreCase("_root")) {
        return LogManager.getRootLogger();
    }
    return LogManager.getLogger(loggerName);
}
 
Example 17
Source File: CsvParameterLayoutAllAsyncTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testLayoutDefaultNormal() throws Exception {
    final Logger root = (Logger) LogManager.getRootLogger();
    CsvParameterLayoutTest.testLayoutNormalApi(root, CsvParameterLayout.createDefaultLayout(), false);
}
 
Example 18
Source File: CsvParameterLayoutAllAsyncTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testLayoutDefaultObjectArrayMessage() throws Exception {
    final Logger root = (Logger) LogManager.getRootLogger();
    CsvParameterLayoutTest.testLayoutNormalApi(root, CsvParameterLayout.createDefaultLayout(), true);
}
 
Example 19
Source File: CsvParameterLayoutAllAsyncTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testLayoutTab() throws Exception {
    final Logger root = (Logger) LogManager.getRootLogger();
    CsvParameterLayoutTest.testLayoutNormalApi(root, CsvParameterLayout.createLayout(CSVFormat.TDF), true);
}