Java Code Examples for org.apache.logging.log4j.core.config.Configuration#getLoggerConfig()

The following examples show how to use org.apache.logging.log4j.core.config.Configuration#getLoggerConfig() . 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: LoggerUpdateTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void resetLevel() {
    final org.apache.logging.log4j.Logger logger = context.getLogger("com.apache.test");
    logger.traceEntry();
    List<LogEvent> events = app.getEvents();
    assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
    app.clear();
    final LoggerContext ctx = LoggerContext.getContext(false);
    final Configuration config = ctx.getConfiguration();
    final LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
    /* You could also specify the actual logger name as below and it will return the LoggerConfig used by the Logger.
       LoggerConfig loggerConfig = getLoggerConfig("com.apache.test");
    */
    loggerConfig.setLevel(Level.DEBUG);
    ctx.updateLoggers();  // This causes all Loggers to refetch information from their LoggerConfig.
    logger.traceEntry();
    events = app.getEvents();
    assertEquals("Incorrect number of events. Expected 0, actual " + events.size(), 0, events.size());
}
 
Example 2
Source File: Log.java    From Openfire with Apache License 2.0 6 votes vote down vote up
private static void setLogLevel() {
    // SLF4J doesn't provide a hook into the logging implementation. We'll have to do this 'direct', bypassing slf4j.
    final org.apache.logging.log4j.Level newLevel;
    if (traceEnabled) {
        newLevel = org.apache.logging.log4j.Level.TRACE;
    } else if (debugEnabled) {
        newLevel = org.apache.logging.log4j.Level.DEBUG;
    } else {
        newLevel = org.apache.logging.log4j.Level.INFO;
    }
    final LoggerContext ctx = (LoggerContext) LogManager.getContext( false );
    final Configuration config = ctx.getConfiguration();
    final LoggerConfig loggerConfig = config.getLoggerConfig( LogManager.ROOT_LOGGER_NAME );
    loggerConfig.setLevel( newLevel );
    ctx.updateLoggers();  // This causes all Loggers to refetch information from their LoggerConfig.
}
 
Example 3
Source File: Config.java    From meghanada-server with GNU General Public License v3.0 6 votes vote down vote up
private Config() {
  this.c = ConfigFactory.load();
  final String logLevel = c.getString("log-level");
  Level level = Level.toLevel(logLevel);
  final String lowerLevel = logLevel.toLowerCase();

  if (lowerLevel.equals("debug") || lowerLevel.equals("telemetry")) {
    this.debug = true;
  }
  // force change
  Object ctx = LogManager.getContext(false);
  if (ctx instanceof LoggerContext) {
    LoggerContext context = (LoggerContext) ctx;
    Configuration configuration = context.getConfiguration();
    LoggerConfig loggerConfig = configuration.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
    loggerConfig.setLevel(level);
    context.updateLoggers();
  }
}
 
Example 4
Source File: Log4j2MetricsTest.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@Test
void filterWhenLoggerAdditivityIsFalseShouldWork() {
    Logger additivityDisabledLogger = LogManager.getLogger("additivityDisabledLogger");
    Configurator.setLevel("additivityDisabledLogger", Level.INFO);

    LoggerContext loggerContext = (LoggerContext) LogManager.getContext();
    Configuration configuration = loggerContext.getConfiguration();
    LoggerConfig loggerConfig = configuration.getLoggerConfig("additivityDisabledLogger");
    loggerConfig.setAdditive(false);

    new Log4j2Metrics().bindTo(registry);

    assertThat(registry.get("log4j2.events").tags("level", "info").counter().count()).isEqualTo(0);

    additivityDisabledLogger.info("Hello, world!");
    assertThat(registry.get("log4j2.events").tags("level", "info").counter().count()).isEqualTo(1);
}
 
Example 5
Source File: MyLogger.java    From Flashtool with GNU General Public License v3.0 6 votes vote down vote up
public static void setLevel(Level level) {
	LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
	Configuration config = ctx.getConfiguration();
	LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME); 
	loggerConfig.setLevel(level);
	ctx.updateLoggers();
		if (level == Level.ERROR) {
			logger.error("<- This level is successfully initialized");
		}
		if (level == Level.WARN) {
			logger.warn("<- This level is successfully initialized");
		}
		if (level == Level.DEBUG) {
			logger.debug("<- This level is successfully initialized");
		}
		if (level == Level.INFO) {
			logger.info("<- This level is successfully initialized");
		}
}
 
Example 6
Source File: Nukkit.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public static void setLogLevel(Level level) {
    Preconditions.checkNotNull(level, "level");
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration log4jConfig = ctx.getConfiguration();
    LoggerConfig loggerConfig = log4jConfig.getLoggerConfig(org.apache.logging.log4j.LogManager.ROOT_LOGGER_NAME);
    loggerConfig.setLevel(level);
    ctx.updateLoggers();
}
 
Example 7
Source File: Log4j2LoggerAdapter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void setLevel(Level level) {
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration config = ctx.getConfiguration();
    LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
    loggerConfig.setLevel(toLog4jLevel(level));
    ctx.updateLoggers();
}
 
Example 8
Source File: LoggingUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void setLog4JLoggingLevel(Log.LogLevel verbosity) {
    // Now establish the logging level used by log4j by propagating the requested
    // logging level to all loggers associated with our logging configuration.
    final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
    final Configuration loggerContextConfig = loggerContext.getConfiguration();
    final String contextClassName = LoggingUtils.class.getName();
    final LoggerConfig loggerConfig = loggerContextConfig.getLoggerConfig(contextClassName);

    loggerConfig.setLevel(levelToLog4jLevel(verbosity));
    loggerContext.updateLoggers();
}
 
Example 9
Source File: JumbuneAgent.java    From jumbune with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void turnLoggingLevelToDebug(String verboseMode) {
	LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
	Configuration config = ctx.getConfiguration();
	LoggerConfig loggerConfig = config.getLoggerConfig(ROLLING_FILE_APPENDER);
	loggerConfig.setLevel(Level.DEBUG);
	ctx.updateLoggers();
	LOGGER.debug("logging level changed to [DEBUG]");
}
 
Example 10
Source File: LogisticRegressionTest.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception{
        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        Configuration config = ctx.getConfiguration();
        LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
        loggerConfig.setLevel(Level.DEBUG);
        ctx.updateLoggers();
//        test3();
        test5();
    }
 
Example 11
Source File: IMLLogisticRegressionTest.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception{
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration config = ctx.getConfiguration();
    LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
    loggerConfig.setLevel(Level.DEBUG);
    ctx.updateLoggers();
    test1();
}
 
Example 12
Source File: Logger.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
public PrivateConfig(final Configuration config, final Logger logger) {
    this.config = config;
    this.loggerConfig = config.getLoggerConfig(getName());
    this.loggerConfigLevel = this.loggerConfig.getLevel();
    this.intLevel = this.loggerConfigLevel.intLevel();
    this.logger = logger;
    this.requiresLocation = this.loggerConfig.requiresLocation();
}
 
Example 13
Source File: SpectatorAppender.java    From spectator with Apache License 2.0 5 votes vote down vote up
private static void addToRootLogger(final Appender appender) {
  LoggerContext context = (LoggerContext) LogManager.getContext(false);
  Configuration config = context.getConfiguration();
  LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
  loggerConfig.addAppender(appender, Level.ALL, null);
  context.updateLoggers(config);
}
 
Example 14
Source File: BMTrainerTest.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration config = ctx.getConfiguration();
    LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
    loggerConfig.setLevel(Level.DEBUG);
    ctx.updateLoggers();
    test5();

}
 
Example 15
Source File: ReplicaDB.java    From ReplicaDB with Apache License 2.0 5 votes vote down vote up
private static void setLogToDebugMode() {

        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        Configuration config = ctx.getConfiguration();
        LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
        loggerConfig.setLevel(Level.valueOf("DEBUG"));
        ctx.updateLoggers();  // This causes all Loggers to refetch information from their LoggerConfi

    }
 
Example 16
Source File: Loggers.java    From crate with Apache License 2.0 5 votes vote down vote up
public static Appender findAppender(final Logger logger, final Class<? extends Appender> clazz) {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    final LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
    for (final Map.Entry<String, Appender> entry : loggerConfig.getAppenders().entrySet()) {
        if (entry.getValue().getClass().equals(clazz)) {
            return entry.getValue();
        }
    }
    return null;
}
 
Example 17
Source File: RidgeLogisticTrainerTest.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception{
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration config = ctx.getConfiguration();
    LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
    loggerConfig.setLevel(Level.DEBUG);
    ctx.updateLoggers();
    test1();
}
 
Example 18
Source File: DBConnection.java    From yawl with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void setLogLevel(Logger logger, Level level) {
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration config = ctx.getConfiguration();
    LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
    loggerConfig.setLevel(level);
    ctx.updateLoggers();
}
 
Example 19
Source File: Log4j2LoggerAdapter.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Level getLevel() {
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration config = ctx.getConfiguration();
    LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
    return fromLog4jLevel(loggerConfig.getLevel());
}
 
Example 20
Source File: Nukkit.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public static Level getLogLevel() {
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration log4jConfig = ctx.getConfiguration();
    LoggerConfig loggerConfig = log4jConfig.getLoggerConfig(org.apache.logging.log4j.LogManager.ROOT_LOGGER_NAME);
    return loggerConfig.getLevel();
}