Java Code Examples for ch.qos.logback.classic.LoggerContext#addListener()

The following examples show how to use ch.qos.logback.classic.LoggerContext#addListener() . 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: LoggingContextSelector.java    From red5-server-common with Apache License 2.0 6 votes vote down vote up
public LoggingContextSelector(LoggerContext context) {
    if (DEFAULT_CONTEXT == null) {
        if (Red5LoggerFactory.DEBUG) {
            System.out.printf("Setting default logging context: %s%n", context.getName());
        }
        DEFAULT_CONTEXT = context;
        // add listener
        context.addListener(new Red5LoggerContextListener());
        String defaultContextName = DEFAULT_CONTEXT.getName();
        if (Red5LoggerFactory.DEBUG) {
            System.out.printf("defaultContextName: %s%n", defaultContextName);
        }
        if (defaultContextName == null) {
            DEFAULT_CONTEXT.setName(CoreConstants.DEFAULT_CONTEXT_NAME);
        }
        // inject the name of the current application as "application-name" property of the LoggerContext
        DEFAULT_CONTEXT.putProperty(KEY_APP_NAME, "red5");
        // attach to the map
        attachLoggerContext(defaultContextName, DEFAULT_CONTEXT);
        // set on thread local
        threadLocal.set(DEFAULT_CONTEXT);
    } else {
        threadLocal.set(context);
    }
}
 
Example 2
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 3
Source File: LoggingConfiguration.java    From cubeai with Apache License 2.0 4 votes vote down vote up
private void addContextListener(LoggerContext context) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener();
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}
 
Example 4
Source File: LoggingConfiguration.java    From e-commerce-microservice with Apache License 2.0 4 votes vote down vote up
private void addContextListener(LoggerContext context) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener();
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}
 
Example 5
Source File: LoggingConfiguration.java    From TeamDojo with Apache License 2.0 4 votes vote down vote up
private void addContextListener(LoggerContext context) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener();
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}
 
Example 6
Source File: LoggingConfiguration.java    From e-commerce-microservice with Apache License 2.0 4 votes vote down vote up
private void addContextListener(LoggerContext context) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener();
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}
 
Example 7
Source File: LoggingConfiguration.java    From cubeai with Apache License 2.0 4 votes vote down vote up
private void addContextListener(LoggerContext context) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener();
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}
 
Example 8
Source File: LoggingConfiguration.java    From cubeai with Apache License 2.0 4 votes vote down vote up
private void addContextListener(LoggerContext context) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener();
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}
 
Example 9
Source File: LoggingConfiguration.java    From cubeai with Apache License 2.0 4 votes vote down vote up
private void addContextListener(LoggerContext context) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener();
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}
 
Example 10
Source File: LoggingConfiguration.java    From flair-registry with Apache License 2.0 4 votes vote down vote up
private void addContextListener(LoggerContext context) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener();
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}
 
Example 11
Source File: LoggingConfiguration.java    From cubeai with Apache License 2.0 4 votes vote down vote up
private void addContextListener(LoggerContext context) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener();
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}
 
Example 12
Source File: LoggingConfiguration.java    From tutorials with MIT License 4 votes vote down vote up
private void addContextListener(LoggerContext context) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener();
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}
 
Example 13
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 14
Source File: LoggingConfiguration.java    From cubeai with Apache License 2.0 4 votes vote down vote up
private void addContextListener(LoggerContext context) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener();
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}
 
Example 15
Source File: LoggingConfiguration.java    From tutorials with MIT License 4 votes vote down vote up
private void addContextListener(LoggerContext context) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener();
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}
 
Example 16
Source File: LoggingConfiguration.java    From Full-Stack-Development-with-JHipster with MIT License 4 votes vote down vote up
private void addContextListener(LoggerContext context) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener();
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}
 
Example 17
Source File: LoggingConfiguration.java    From java-microservices-examples with Apache License 2.0 4 votes vote down vote up
private void addContextListener(LoggerContext context) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener(this.jHipsterProperties);
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}
 
Example 18
Source File: LoggingConfiguration.java    From tutorials with MIT License 4 votes vote down vote up
private void addContextListener(LoggerContext context) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener();
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}
 
Example 19
Source File: LoggingConfiguration.java    From alchemy with Apache License 2.0 4 votes vote down vote up
private void addContextListener(LoggerContext context) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener(this.jHipsterProperties);
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}
 
Example 20
Source File: LoggingUtils.java    From jhipster with Apache License 2.0 2 votes vote down vote up
/**
 * <p>addContextListener.</p>
 *
 * @param context a {@link ch.qos.logback.classic.LoggerContext} object.
 * @param customFields a {@link java.lang.String} object.
 * @param properties a {@link io.github.jhipster.config.JHipsterProperties.Logging} object.
 */
public static void addContextListener(LoggerContext context, String customFields, JHipsterProperties.Logging properties) {
    LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener(properties, customFields);
    loggerContextListener.setContext(context);
    context.addListener(loggerContextListener);
}