/** * Auxiliary method to change the log level. * * @param logLevel * The log level to set. * * @return * A ResponseContext containing the result of the command. */ public static ResponseContext changeLogLevel(String logLevel) { ResponseContext responseContext = new ResponseContext(); responseContext.setContentType(ResponseContext.PLAIN_TEXT_CONTENT_TYPE); LoggerContext loggerContext = LoggerContext.getContext(false); Configuration configuration = loggerContext.getConfiguration(); LoggerConfig rootLogger = configuration.getRootLogger(); if (rootLogger != null) { switch (StandardLevel.getStandardLevel(Level.toLevel(logLevel, Level.INFO).intLevel())) { case ERROR: rootLogger.setLevel(Level.ERROR); responseContext.setResponse("Log level changed to ERROR"); break; case WARN: rootLogger.setLevel(Level.WARN); responseContext.setResponse("Log level changed to WARN"); break; case INFO: rootLogger.setLevel(Level.INFO); responseContext.setResponse("Log level changed to INFO"); break; case DEBUG: rootLogger.setLevel(Level.DEBUG); responseContext.setResponse("Log level changed to DEBUG"); break; case OFF: rootLogger.setLevel(Level.OFF); responseContext.setResponse("Logging switched off"); break; default: responseContext.setResponse("Log level unchanged, unsupported level: " + logLevel); } loggerContext.updateLoggers(); } else { responseContext.setResponse("Unable to change log level, no ROOT logger found..."); responseContext.setErrorCode(String.valueOf(HttpURLConnection.HTTP_INTERNAL_ERROR)); } return responseContext; }
@Test public void levels() { for (StandardLevel std : StandardLevel.values()) { Level level = Level.getLevel(std.name()); Assert.assertEquals(std, LevelTag.get(level).standardLevel()); } }
private Level(final String name, final int intLevel) { if (Strings.isEmpty(name)) { throw new IllegalArgumentException("Illegal null or empty Level name."); } if (intLevel < 0) { throw new IllegalArgumentException("Illegal Level int less than zero."); } this.name = name; this.intLevel = intLevel; this.standardLevel = StandardLevel.getStandardLevel(intLevel); if (LEVELS.putIfAbsent(name, this) != null) { throw new IllegalStateException("Level " + name + " has already been defined."); } }
/** * Gets the standard Level values as an enum. * * @return an enum of the standard Levels. */ public StandardLevel getStandardLevel() { return standardLevel; }