Java Code Examples for ch.qos.logback.classic.Logger#iteratorForAppenders()

The following examples show how to use ch.qos.logback.classic.Logger#iteratorForAppenders() . 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: LogbackController.java    From GreenSummer with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 
 * @param name The name of the logger we want to manipulate the level of
 * @param level The new log level
 * @return The list of log levels configured and their settings
 */
@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 String level) {
    final LoggerContext ctx = (LoggerContext) LoggerFactory.getILoggerFactory();
    synchronized (ctx) {
        Logger root = ctx.getLogger(Logger.ROOT_LOGGER_NAME);
        Logger logger = ctx.getLogger(name);
        if (logger != null) {
            logger.setLevel(Level.toLevel(level));
            logger.setAdditive(false);
            for (Iterator<Appender<ILoggingEvent>> it = root.iteratorForAppenders(); it.hasNext();) {
                logger.addAppender(it.next());
            }
        }
    }
    return new ResponseEntity<>(listLoggers(ctx), HttpStatus.OK);
}
 
Example 2
Source File: LogService.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public List<FileAppender<ILoggingEvent>> getFileAppenders() {
	List<FileAppender<ILoggingEvent>> fileAppenders = new LinkedList<FileAppender<ILoggingEvent>>();

	for (Logger logger : getLoggers()) {
		for (Iterator<Appender<ILoggingEvent>> index = logger
				.iteratorForAppenders(); index.hasNext();) {

			Appender<ILoggingEvent> appender = index.next();
			if (appender instanceof FileAppender)
				fileAppenders.add((FileAppender<ILoggingEvent>) appender);
		}
	}

	return fileAppenders;
}
 
Example 3
Source File: LogService.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public FileAppender<ILoggingEvent> getFileAppender(String name) {
	if (name == null)
		return null;

	for (Logger logger : getLoggers()) {
		for (Iterator<Appender<ILoggingEvent>> index = logger
				.iteratorForAppenders(); index.hasNext();) {

			Appender<ILoggingEvent> appender = index.next();
			if (appender instanceof FileAppender
					&& name.equals(appender.getName()))
				return (FileAppender<ILoggingEvent>) appender;
		}
	}

	return null;
}
 
Example 4
Source File: LogService.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public List<RollingFileAppender<ILoggingEvent>> getRollingFileAppenders() {
	List<RollingFileAppender<ILoggingEvent>> rollingFileAppenders = new LinkedList<RollingFileAppender<ILoggingEvent>>();

	for (Logger logger : getLoggers()) {
		for (Iterator<Appender<ILoggingEvent>> index = logger
				.iteratorForAppenders(); index.hasNext();) {

			Appender<ILoggingEvent> appender = index.next();
			if (appender instanceof RollingFileAppender)
				rollingFileAppenders
						.add((RollingFileAppender<ILoggingEvent>) appender);
		}
	}

	return rollingFileAppenders;
}
 
Example 5
Source File: LogBackHookProxy.java    From uavstack with Apache License 2.0 6 votes vote down vote up
private void figureoutLogConfiguration(Logger Logger, LinkedList<LogProfileInfo> list, String appid) {

        Iterator<Appender<ILoggingEvent>> appenders = Logger.iteratorForAppenders();
        while (appenders != null && appenders.hasNext()) {
            Appender<ILoggingEvent> appender = appenders.next();
            if (appender instanceof FileAppender<?>) {
                getAppenderInfo((FileAppender<ILoggingEvent>)appender, list, appid);
            }
            else if(appender instanceof AsyncAppender) {
                Iterator<Appender<ILoggingEvent>> itAppenders = (Iterator<Appender<ILoggingEvent>>)((AsyncAppender) appender).iteratorForAppenders();
                while (itAppenders != null && itAppenders.hasNext()) {
                    Appender<ILoggingEvent> ap = itAppenders.next();
                    if (ap instanceof FileAppender<?>) {
                        getAppenderInfo((FileAppender<ILoggingEvent>)ap, list, appid);
                    }
                } 
            }
        }
    }
 
Example 6
Source File: LogBackHookProxy.java    From uavstack with Apache License 2.0 6 votes vote down vote up
private void figureoutLogConfiguration(Logger Logger, LinkedList<LogProfileInfo> list, String appid) {

        Iterator<Appender<ILoggingEvent>> appenders = Logger.iteratorForAppenders();
        while (appenders != null && appenders.hasNext()) {
            Appender<ILoggingEvent> appender = appenders.next();
            if (appender instanceof FileAppender<?>) {
                getAppenderInfo((FileAppender<ILoggingEvent>)appender, list, appid);
            }
            else if(appender instanceof AsyncAppender) {
                Iterator<Appender<ILoggingEvent>> itAppenders = (Iterator<Appender<ILoggingEvent>>)((AsyncAppender) appender).iteratorForAppenders();
                while (itAppenders != null && itAppenders.hasNext()) {
                    Appender<ILoggingEvent> ap = itAppenders.next();
                    if (ap instanceof FileAppender<?>) {
                        getAppenderInfo((FileAppender<ILoggingEvent>)ap, list, appid);
                    }
                } 
            }
        }
    }
 
Example 7
Source File: DiagnosticLogging.java    From hivemq-community-edition with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the trace log to the root logger. Also adds filter, to make sure that
 * the appender which are already defined for HiveMQ are not affected by this logging
 * level change.
 * <p>
 * <b>This will significantly slow down HiveMQ, since the root level loggers Level is changed
 * to the finest logging level!</b>
 *
 * @param filePath the file path
 */
static void setTraceLog(final String filePath) {

    log.info("Creating trace log {}", filePath);

    final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    final Level originalLoggingLevel = logger.getLevel();
    final Iterator<Appender<ILoggingEvent>> appenderIterator = logger.iteratorForAppenders();
    while (appenderIterator.hasNext()) {
        final Appender<ILoggingEvent> next = appenderIterator.next();
        next.addFilter(new PreserveOriginalLoggingLevelFilter(originalLoggingLevel));
    }

    final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    final PatternLayoutEncoder ple = new PatternLayoutEncoder();

    ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
    ple.setContext(lc);
    ple.start();

    final FileAppender<ILoggingEvent> fileAppender = new FileAppender<>();
    fileAppender.setFile(filePath);
    fileAppender.setEncoder(ple);
    fileAppender.setContext(lc);
    fileAppender.start();

    logger.addAppender(fileAppender);
    logger.setLevel(Level.ALL);
    logger.setAdditive(false);
}
 
Example 8
Source File: LogControlImpl.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected Map<String, Appender> getAllAppenders() {
    Map<String, Appender> map = new HashMap<>();
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    for (Logger logger : context.getLoggerList()) {
        for (Iterator<Appender<ILoggingEvent>> index = logger.iteratorForAppenders(); index.hasNext();) {
            Appender<ILoggingEvent> appender = index.next();
            if (appender.getName() != null) {
                map.put(appender.getName(), appender);
            }
        }
    }
    return map;
}
 
Example 9
Source File: BaleenLoggingTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
  BaleenLogging logging = new BaleenLogging();
  InMemoryLoggingBuilder builder = new InMemoryLoggingBuilder();
  logging.configure(
      Arrays.asList(
          builder,
          new BaleenConsoleLoggerBuilder(PATTERN, new MinMaxFilter(Level.INFO, Level.ERROR))));

  LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
  Logger rootLogger = context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);

  int count = 0;

  Iterator<Appender<ILoggingEvent>> it = rootLogger.iteratorForAppenders();
  while (it.hasNext()) {
    Appender<ILoggingEvent> appender = it.next();

    if (appender instanceof OutputStreamAppender) {
      Encoder<ILoggingEvent> e = ((OutputStreamAppender<ILoggingEvent>) appender).getEncoder();
      assertTrue(e instanceof PatternLayoutEncoder);

      assertEquals(PATTERN, ((PatternLayoutEncoder) e).getPattern());
    }

    count++;
  }

  // 3 = 2 + instrumented appender
  assertEquals(3, count);
}
 
Example 10
Source File: FileLocationProvider.java    From gocd with Apache License 2.0 5 votes vote down vote up
private Appender[] getAppenders(List<Logger> loggers) {
    LinkedHashSet<Appender<ILoggingEvent>> appenders = new LinkedHashSet<>();

    for (Logger logger : loggers) {
        Iterator<Appender<ILoggingEvent>> appenderIterator = logger.iteratorForAppenders();
        while (appenderIterator.hasNext()) {
            Appender<ILoggingEvent> appender = appenderIterator.next();
            appenders.add(appender);
        }
    }
    return appenders.toArray(new Appender[0]);
}
 
Example 11
Source File: BaleenLoggingTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void config() throws Exception {
  YamlConfiguration configuration =
      new YamlConfiguration(BaleenLoggingTest.class, "dummyConfig.yaml");

  BaleenLogging logging = new BaleenLogging();
  logging.configure(configuration);

  LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
  Logger rootLogger = context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);

  int count = 0;

  Iterator<Appender<ILoggingEvent>> it = rootLogger.iteratorForAppenders();
  while (it.hasNext()) {
    Appender<ILoggingEvent> appender = it.next();

    switch (count) {
      case 0:
        assertTrue(appender instanceof ConsoleAppender);
        break;
      case 1:
        assertTrue(appender instanceof RollingFileAppender);
        break;
      case 2:
        assertTrue(appender instanceof FileAppender);
        assertFalse(appender instanceof RollingFileAppender);
        break;
      case 3:
        if (appender instanceof OutputStreamAppender) {
          Encoder<ILoggingEvent> e =
              ((OutputStreamAppender<ILoggingEvent>) appender).getEncoder();
          assertTrue(e instanceof PatternLayoutEncoder);
          assertEquals(PATTERN, ((PatternLayoutEncoder) e).getPattern());
        }
        break;
      case 4:
        if (appender instanceof EvictingQueueAppender) {
          assertEquals(
              EvictingQueueAppender.DEFAULT_MAX_SIZE,
              ((EvictingQueueAppender<ILoggingEvent>) appender).getMaxSize());
        } else {
          fail("Unknown additional appender");
        }
        break;
      case 5:
        // Allow additional appenders for checking, otherwise throw an error
        if (!(appender instanceof InstrumentedAppender)) {
          fail("Unknown additional appender");
        }
        break;
      default:
        fail("Too many appenders" + appender.getName());
    }
    count++;
  }

  assertEquals(6, count);

  // TODO: test the instance parameters match the configuration
}
 
Example 12
Source File: LoggingUtil.java    From ha-bridge with Apache License 2.0 3 votes vote down vote up
/**
 * Get the logfile information for the roor logger.
 * 
 * @return List of LogFileInfo obejcts
 */
public static List<LogFileInfo> getLogFileInfos() {
	
	final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

	final List<LogFileInfo> logFileInfos = new ArrayList<LogFileInfo>();

	final Logger logger = lc.getLogger(Logger.ROOT_LOGGER_NAME);

	final Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders();

	while (it.hasNext()) {

		final Appender<ILoggingEvent> appender = it.next();

		if (appender instanceof FileAppender) {

			final FileAppender<ILoggingEvent> fileAppender = (FileAppender<ILoggingEvent>) appender;

			final File logFile = new File(fileAppender.getFile());
			final LogFileInfo logFileInfo = new LogFileInfo();

			logFileInfo.setFileName(logFile.getName());
			logFileInfo.setFileLastChanged(new Date(logFile.lastModified()));
			logFileInfo.setFileSize(logFile.length());
			logFileInfos.add(logFileInfo);
		}

	}

	return logFileInfos;
}
 
Example 13
Source File: LoggingUtil.java    From ha-bridge with Apache License 2.0 3 votes vote down vote up
/**
 * Get the log file.
 * 
 * @param logFileName The name of the log file 
 * @return The actual file
 */
public static File getLogFile(final String logFileName) {

	if (logFileName == null) {
		throw new IllegalArgumentException("logFileName cannot be null.");
	}

	final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

	final Logger logger = lc.getLogger(Logger.ROOT_LOGGER_NAME);

	final Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders();

	while (it.hasNext()) {

		final Appender<ILoggingEvent> appender = it.next();

		if (appender instanceof FileAppender) {

			final FileAppender<ILoggingEvent> fileAppender = (FileAppender<ILoggingEvent>) appender;

			final File logFile = new File(fileAppender.getFile());

			if (logFile.getName().equalsIgnoreCase(logFileName)) {
				return logFile;
			}

		}

	}

	return null;
}