ch.qos.logback.classic.jul.LevelChangePropagator Java Examples

The following examples show how to use ch.qos.logback.classic.jul.LevelChangePropagator. 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: LoggingFactory.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
private Logger configureLevels() {

        final Logger root = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
        root.getLoggerContext().reset();

        final LevelChangePropagator propagator = new LevelChangePropagator();
        propagator.setContext(root.getLoggerContext());
        propagator.setResetJUL(true);

        root.getLoggerContext().addListener(propagator);

        root.setLevel(config.getLevel());

        // Decode the packages and levels
        for (Map.Entry<String, Level> entry : config.getLoggers().entrySet()) {
            ((Logger) LoggerFactory.getLogger(entry.getKey())).setLevel(entry.getValue());
        }

        return root;
    }
 
Example #2
Source File: LoggingFactory.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
private Logger configureLevels() {

        final Logger root = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
        root.getLoggerContext().reset();

        final LevelChangePropagator propagator = new LevelChangePropagator();
        propagator.setContext(root.getLoggerContext());
        propagator.setResetJUL(true);

        root.getLoggerContext().addListener(propagator);

        root.setLevel(config.getLevel());

        // Decode the packages and levels
        for (Map.Entry<String, Level> entry : config.getLoggers().entrySet()) {
            ((Logger) LoggerFactory.getLogger(entry.getKey())).setLevel(entry.getValue());
        }

        return root;
    }
 
Example #3
Source File: LoggingConfig.java    From mirror with Apache License 2.0 5 votes vote down vote up
public synchronized static void init() {
  if (started) {
    return;
  }
  started = true;

  // setup java.util.logging (which grpc-java uses) to go to slf4j
  SLF4JBridgeHandler.removeHandlersForRootLogger();
  SLF4JBridgeHandler.install();

  LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

  LevelChangePropagator p = new LevelChangePropagator();
  p.setContext(context);
  p.start();
  context.addListener(p);

  PatternLayoutEncoder encoder = new PatternLayoutEncoder();
  encoder.setContext(context);
  encoder.setPattern(pattern);
  encoder.start();

  ConsoleAppender<ILoggingEvent> console = new ConsoleAppender<>();
  console.setContext(context);
  console.setEncoder(encoder);
  console.start();

  Logger root = getLogger(Logger.ROOT_LOGGER_NAME);
  root.detachAndStopAllAppenders();
  root.addAppender(console);
  root.setLevel(Level.INFO);

  getLogger("io.grpc").setLevel(Level.INFO);
  // silence a noisy DNS warning when we cannot resolve the other host
  getLogger("io.grpc.internal.ManagedChannelImpl").setLevel(Level.ERROR);
  // silence "ConnectivityStateManager is already disabled" warning
  getLogger("io.grpc.internal.ChannelExecutor").setLevel(Level.ERROR);
  getLogger("mirror").setLevel(Level.INFO);
}
 
Example #4
Source File: BaleenLogging.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Configure logging based on a list of builders provided to it. Injects the configured logging to
 * replace the default UIMA loggers, and also sets up metrics on the logging.
 *
 * @param builders The builders to use to configure the logging
 */
public void configure(List<BaleenLoggerBuilder> builders) {
  // Install JUL to SLF4J handling (JUL is default for UIMA)
  SLF4JBridgeHandler.removeHandlersForRootLogger();
  SLF4JBridgeHandler.install();

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

  // Install the level change propagator to reduce the impact of JUL logging
  context.addListener(new LevelChangePropagator());

  // Remove all the existing appenders
  rootLogger.detachAndStopAllAppenders();

  for (BaleenLoggerBuilder builder : builders) {
    PatternLayoutEncoder ple = new PatternLayoutEncoder();
    ple.setCharset(StandardCharsets.UTF_8);
    ple.setContext(context);
    ple.setPattern(builder.getPattern());
    ple.start();

    Appender<ILoggingEvent> appender = builder.build(context, ple);
    if (!appender.isStarted()) {
      appender.start();
    }

    rootLogger.addAppender(appender);
  }

  LOGGER.debug("Adding instrumented metrics for logging");
  // Add an instrumented appender so we get the information about logging
  // through metrics
  InstrumentedAppender instrumentedAppender =
      new InstrumentedAppender(MetricsFactory.getInstance().getRegistry());
  instrumentedAppender.setContext(context);
  instrumentedAppender.start();
  rootLogger.addAppender(instrumentedAppender);
}
 
Example #5
Source File: DefaultLogbackReInitializer.java    From sofa-common-tools with Apache License 2.0 4 votes vote down vote up
private void addLevelChangePropagator(LoggerContext loggerContext) {
    LevelChangePropagator levelChangePropagator = new LevelChangePropagator();
    levelChangePropagator.setResetJUL(true);
    levelChangePropagator.setContext(loggerContext);
    loggerContext.addListener(levelChangePropagator);
}
 
Example #6
Source File: LogbackLoggingSystem.java    From super-cloudops with Apache License 2.0 4 votes vote down vote up
private void addLevelChangePropagator(LoggerContext loggerContext) {
	LevelChangePropagator levelChangePropagator = new LevelChangePropagator();
	levelChangePropagator.setResetJUL(true);
	levelChangePropagator.setContext(loggerContext);
	loggerContext.addListener(levelChangePropagator);
}
 
Example #7
Source File: LogbackConfiguration.java    From IridiumApplicationTesting with MIT License 4 votes vote down vote up
@Override
public void configureLogging(@NotNull final String logfile) {
	checkArgument(StringUtils.isNotBlank(logfile));

	try {
		final LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

		final FileAppender<ILoggingEvent> fileAppender = new FileAppender<>();
		fileAppender.setContext(loggerContext);
		fileAppender.setName("timestamp");
		// set the file name
		fileAppender.setFile(logfile);

		final PatternLayoutEncoder encoder = new PatternLayoutEncoder();
		encoder.setContext(loggerContext);
		encoder.setPattern("%r %thread %level - %msg%n");
		encoder.start();

		fileAppender.setEncoder(encoder);
		fileAppender.start();

		// attach the rolling file appender to the logger of your choice
		final Logger logbackLogger =
			(Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
		logbackLogger.addAppender(fileAppender);

		/*
			We only want to hear about errors from the libraries that are used by Iridium
		 */
		logbackLogger.setLevel(Level.ERROR);

		/*
			Our own code should be info level
		 */
		final Logger iridiumLogger =
			(Logger) LoggerFactory.getLogger("au.com.agic");
		iridiumLogger.setLevel(Level.INFO);

		/*
			Performance increase for redirected JUL loggers
		 */
		final LevelChangePropagator levelChangePropagator = new LevelChangePropagator();
		levelChangePropagator.setContext(loggerContext);
		levelChangePropagator.setResetJUL(true);
		loggerContext.addListener(levelChangePropagator);

		/*
			Redirect java logging and sys out to slf4j
		 */
		LogManager.getLogManager().reset();
		SLF4JBridgeHandler.removeHandlersForRootLogger();
		SysOutOverSLF4J.sendSystemOutAndErrToSLF4J();
		SLF4JBridgeHandler.install();
	} catch (final Exception ex) {
		LOGGER.error("WEBAPPTESTER-BUG-0006: Could not configure Logback", ex);
	}
}
 
Example #8
Source File: LogbackLogManager.java    From seed with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public synchronized void configure(LoggingConfig loggingConfig) {
    if (context.isStarted()) {
        context.stop();
    }
    context.reset();
    context.start();

    boolean autoConfigurationFailed = false;
    try {
        new ContextInitializer(context).autoConfig();
    } catch (JoranException e) {
        autoConfigurationFailed = true;
    }

    if (autoConfigurationFailed || !isExplicitlyConfigured()) {
        context.reset();

        // Root logger level
        Logger rootLogger = context.getLogger(Logger.ROOT_LOGGER_NAME);
        rootLogger.setLevel(convertLevel(loggingConfig.getLevel()));

        // Configure
        configureConsole(loggingConfig.console(), rootLogger);
        configureFile(loggingConfig.file(), rootLogger);

        // Nuun is quite verbose so it is set to WARN by default
        Logger nuunLogger = context.getLogger("io.nuun");
        nuunLogger.setLevel(Level.WARN);

        // When running under Tomcat with a LevelChangePropagator, DEBUG level and below lead to an exception
        // so we force INFO level
        if (underTomcat && (loggingConfig.getLevel() == LoggingConfig.Level.DEBUG || loggingConfig.getLevel() ==
                LoggingConfig.Level.TRACE)) {
            context.getLogger("org.apache.catalina").setLevel(Level.INFO);
            context.getLogger("org.apache.juli").setLevel(Level.INFO);
        }

        // Configure explicit loggers
        for (Map.Entry<String, LoggingConfig.LoggerConfig> loggerLevelEntry : loggingConfig.loggers()
                .entrySet()) {
            Logger logger = context.getLogger(loggerLevelEntry.getKey());
            LoggingConfig.LoggerConfig config = loggerLevelEntry.getValue();
            logger.setLevel(convertLevel(config.getLevel()));
            logger.setAdditive(config.isAdditive());
        }

        // Add level propagator for performance of JUL
        LevelChangePropagator levelChangePropagator = new LevelChangePropagator();
        levelChangePropagator.setContext(context);
        levelChangePropagator.setResetJUL(true);
        context.addListener(levelChangePropagator);
        levelChangePropagator.start();
    }
}