Java Code Examples for org.apache.logging.log4j.core.config.LoggerConfig#setLevel()

The following examples show how to use org.apache.logging.log4j.core.config.LoggerConfig#setLevel() . 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: Log4JController.java    From GreenSummer with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Sets the.
 *
 * @param name
 *        the name
 * @param level
 *        the level
 * @return the response entity
 */
@RequestMapping(value = "set/{name}/{level}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET,
    headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<LogResponse> set(@PathVariable("name")
final String name, @PathVariable("level")
final Level level) {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    synchronized (ctx) {
        final Configuration config = ctx.getConfiguration();
        LoggerConfig loggerConfig = config.getLoggerConfig(name);
        if (name.equalsIgnoreCase(loggerConfig.getName())) {
            loggerConfig.setLevel(level);
        } else {
            LoggerConfig newloggerConfig = new LoggerConfig(name, level, true);
            config.addLogger(name, newloggerConfig);
        }
        ctx.updateLoggers();
    }
    return new ResponseEntity<>(listLoggers(ctx), HttpStatus.OK);
}
 
Example 2
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 3
Source File: LoggingMixin.java    From picocli with Apache License 2.0 6 votes vote down vote up
/**
 * Configures the Log4j2 console appender(s), using the specified verbosity:
 * <ul>
 *   <li>{@code -vvv} : enable TRACE level</li>
 *   <li>{@code -vv} : enable DEBUG level</li>
 *   <li>{@code -v} : enable INFO level</li>
 *   <li>(not specified) : enable WARN level</li>
 * </ul>
 */
public void configureLoggers() {
    Level level = getTopLevelCommandLoggingMixin(mixee).calcLogLevel();

    LoggerContext loggerContext = LoggerContext.getContext(false);
    LoggerConfig rootConfig = loggerContext.getConfiguration().getRootLogger();
    for (Appender appender : rootConfig.getAppenders().values()) {
        if (appender instanceof ConsoleAppender) {
            rootConfig.removeAppender(appender.getName());
            rootConfig.addAppender(appender, level, null);
        }
    }
    if (rootConfig.getLevel().isMoreSpecificThan(level)) {
        rootConfig.setLevel(level);
    }
    loggerContext.updateLoggers(); // apply the changes
}
 
Example 4
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 5
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 6
Source File: ConditionEvaluator.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 7
Source File: LogUtil.java    From raml-module-builder with Apache License 2.0 5 votes vote down vote up
public static void setLevelForRootLoggers(Level level){
  LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
  Configuration config = ctx.getConfiguration();
  LoggerConfig loggerConfig = config.getRootLogger();
  loggerConfig.setLevel(level);
  ctx.updateLoggers();
}
 
Example 8
Source File: ElasticNetLogisticTrainerTest.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.OFF);
    ctx.updateLoggers();
    test3();
}
 
Example 9
Source File: XmlConfigurationFactory.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Used internally to parse a level  element.
 */
private void parseLevel(Element element, LoggerConfig logger, boolean isRoot) {
    String catName = logger.getName();
    if (isRoot) {
        catName = "root";
    }

    String priStr = subst(element.getAttribute(VALUE_ATTR));
    LOGGER.debug("Level value for {} is [{}}].", catName, priStr);

    if (INHERITED.equalsIgnoreCase(priStr) || NULL.equalsIgnoreCase(priStr)) {
        if (isRoot) {
            LOGGER.error("Root level cannot be inherited. Ignoring directive.");
        } else {
            logger.setLevel(null);
        }
    } else {
        String className = subst(element.getAttribute(CLASS_ATTR));
        if (EMPTY_STR.equals(className)) {
            logger.setLevel(convertLevel(OptionConverter.toLevel(priStr, Level.DEBUG)));
        } else {
            LOGGER.debug("Desired Level sub-class: [{}]", className);
            try {
                Class<?> clazz = LoaderUtil.loadClass(className);
                Method toLevelMethod = clazz.getMethod("toLevel", ONE_STRING_PARAM);
                Level pri = (Level) toLevelMethod.invoke(null, new Object[]{priStr});
                logger.setLevel(convertLevel(pri));
            } catch (Exception oops) {
                if (oops instanceof InterruptedException || oops instanceof InterruptedIOException) {
                    Thread.currentThread().interrupt();
                }
                LOGGER.error("Could not create level [" + priStr +
                        "]. Reported error follows.", oops);
                return;
            }
        }
    }
    LOGGER.debug("{} level set to {}", catName,  logger.getLevel());
}
 
Example 10
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 11
Source File: Config.java    From meghanada-server with GNU General Public License v3.0 5 votes vote down vote up
private void setLogLevel(final String logLevel) {
  Object ctx = LogManager.getContext(false);
  if (ctx instanceof LoggerContext) {
    LoggerContext context = (LoggerContext) ctx;
    Level level = Level.toLevel(logLevel);
    Configuration configuration = context.getConfiguration();
    LoggerConfig loggerConfig = configuration.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
    loggerConfig.setLevel(level);
    context.updateLoggers();
    this.debug = !logLevel.toLowerCase().equals("info");
  }
}
 
Example 12
Source File: BasicConfigurationFactory.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
public BasicConfiguration(final LoggerContext loggerContext) {
    super(loggerContext, ConfigurationSource.NULL_SOURCE);

    final LoggerConfig root = getRootLogger();
    setName("BasicConfiguration");
    final String levelName = System.getProperty(DEFAULT_LEVEL);
    final Level level = (levelName != null && Level.getLevel(levelName) != null) ? Level.getLevel(levelName)
            : Level.DEBUG;
    root.setLevel(level);
}
 
Example 13
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 14
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 15
Source File: The5zigMod.java    From The-5zig-Mod with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Configures the main logger.
 */
private static void setupLogger() {
	LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
	org.apache.logging.log4j.core.config.Configuration config = ctx.getConfiguration();
	LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
	if (DEBUG) {
		loggerConfig.setLevel(Level.DEBUG);
	}
	ctx.updateLoggers();
	logger.debug("Debug Mode ENABLED!");
}
 
Example 16
Source File: RegressionSynthesizerTest.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 17
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 18
Source File: DeployUtil.java    From jumbune with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void turnLoggingLevelToDebug() {
	LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
	Configuration config = ctx.getConfiguration();
	LoggerConfig loggerConfig = config.getLoggerConfig("rollingFileAppender");
	loggerConfig.setLevel(Level.DEBUG);
	ctx.updateLoggers();
	CONSOLE_LOGGER.info("logging level changed to [DEBUG]");
	CONSOLE_LOGGER.info("Further details can be found in log file");
}
 
Example 19
Source File: LoggerTest.java    From feign-reactive with Apache License 2.0 5 votes vote down vote up
private static Level setLogLevel(Level logLevel) {
  LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
  Configuration configuration = loggerContext.getConfiguration();
  LoggerConfig loggerConfig = configuration.getLoggerConfig(LOGGER_NAME);
  Level previousLevel = loggerConfig.getLevel();
  loggerConfig.setLevel(logLevel);
  loggerContext.updateLoggers();
  return previousLevel;
}
 
Example 20
Source File: The5zigMod.java    From The-5zig-Mod with MIT License 5 votes vote down vote up
/**
 * Configures the main logger.
 */
private static void setupLogger() {
	LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
	org.apache.logging.log4j.core.config.Configuration config = ctx.getConfiguration();
	LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
	if (DEBUG) {
		loggerConfig.setLevel(Level.DEBUG);
	}
	ctx.updateLoggers();
	logger.debug("Debug Mode ENABLED!");
}