Search in sources :

Example 1 with ILoggerFactory

use of org.slf4j.ILoggerFactory in project weave by continuuity.

the class ServiceMain method configureLogger.

private void configureLogger() {
    // Check if SLF4J is bound to logback in the current environment
    ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
    if (!(loggerFactory instanceof LoggerContext)) {
        return;
    }
    LoggerContext context = (LoggerContext) loggerFactory;
    context.reset();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);
    try {
        File weaveLogback = new File(Constants.Files.LOGBACK_TEMPLATE);
        if (weaveLogback.exists()) {
            configurator.doConfigure(weaveLogback);
        }
        new ContextInitializer(context).autoConfig();
    } catch (JoranException e) {
        throw Throwables.propagate(e);
    }
    doConfigure(configurator, getLogConfig(getLoggerLevel(context.getLogger(Logger.ROOT_LOGGER_NAME))));
}
Also used : ContextInitializer(ch.qos.logback.classic.util.ContextInitializer) ILoggerFactory(org.slf4j.ILoggerFactory) JoranException(ch.qos.logback.core.joran.spi.JoranException) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) LoggerContext(ch.qos.logback.classic.LoggerContext) File(java.io.File)

Example 2 with ILoggerFactory

use of org.slf4j.ILoggerFactory in project cdap by caskdata.

the class LogStageInjector method start.

/**
   * Hijacks the appenders for the root logger and replaces them with a {@link LogStageAppender} that will insert
   * the ETL stage name at the start of each message if the stage name is set. Uses {@link org.slf4j.MDC} to look up
   * the current stage name.
   */
public static void start() {
    if (!initialized.compareAndSet(false, true)) {
        return;
    }
    ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
    if (!(loggerFactory instanceof LoggerContext)) {
        LOG.warn("LoggerFactory is not a logback LoggerContext. Stage names will not be injected into log messages.");
        return;
    }
    LoggerContext loggerContext = (LoggerContext) loggerFactory;
    ch.qos.logback.classic.Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
    List<Appender<ILoggingEvent>> appenders = new ArrayList<>();
    Iterator<Appender<ILoggingEvent>> appenderIterator = rootLogger.iteratorForAppenders();
    while (appenderIterator.hasNext()) {
        Appender<ILoggingEvent> appender = appenderIterator.next();
        // appender will have a different classloader than LogStageAppender...
        if (appender.getClass().getName().equals(LogStageAppender.class.getName())) {
            return;
        }
        appenders.add(appender);
    }
    Appender<ILoggingEvent> stageAppender = new LogStageAppender(appenders);
    stageAppender.setContext(loggerContext);
    stageAppender.start();
    rootLogger.addAppender(stageAppender);
    // To avoid duplicate messages, need to detach the original appenders
    for (Appender<ILoggingEvent> appender : appenders) {
        rootLogger.detachAppender(appender);
    }
}
Also used : Appender(ch.qos.logback.core.Appender) ILoggerFactory(org.slf4j.ILoggerFactory) ArrayList(java.util.ArrayList) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 3 with ILoggerFactory

use of org.slf4j.ILoggerFactory in project weave by continuuity.

the class ServiceMain method doMain.

protected final void doMain(final ZKClientService zkClientService, final Service service) throws ExecutionException, InterruptedException {
    configureLogger();
    final String serviceName = service.toString();
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            Services.chainStop(service, zkClientService);
        }
    });
    // Listener for state changes of the service
    ListenableFuture<Service.State> completion = Services.getCompletionFuture(service);
    // Starts the service
    LOG.info("Starting service {}.", serviceName);
    Futures.getUnchecked(Services.chainStart(zkClientService, service));
    LOG.info("Service {} started.", serviceName);
    try {
        completion.get();
        LOG.info("Service {} completed.", serviceName);
    } catch (Throwable t) {
        LOG.warn("Exception thrown from service {}.", serviceName, t);
        throw Throwables.propagate(t);
    } finally {
        ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
        if (loggerFactory instanceof LoggerContext) {
            ((LoggerContext) loggerFactory).stop();
        }
    }
}
Also used : ILoggerFactory(org.slf4j.ILoggerFactory) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 4 with ILoggerFactory

use of org.slf4j.ILoggerFactory in project spring-boot by spring-projects.

the class LogbackLoggingSystem method getLoggerContext.

private LoggerContext getLoggerContext() {
    ILoggerFactory factory = StaticLoggerBinder.getSingleton().getLoggerFactory();
    Assert.isInstanceOf(LoggerContext.class, factory, String.format("LoggerFactory is not a Logback LoggerContext but Logback is on " + "the classpath. Either remove Logback or the competing " + "implementation (%s loaded from %s). If you are using " + "WebLogic you will need to add 'org.slf4j' to " + "prefer-application-packages in WEB-INF/weblogic.xml", factory.getClass(), getLocation(factory)));
    return (LoggerContext) factory;
}
Also used : ILoggerFactory(org.slf4j.ILoggerFactory) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 5 with ILoggerFactory

use of org.slf4j.ILoggerFactory in project spring-boot by spring-projects.

the class LogbackLoggingSystemTests method testBasicConfigLocation.

@Test
public void testBasicConfigLocation() throws Exception {
    this.loggingSystem.beforeInitialize();
    ILoggerFactory factory = StaticLoggerBinder.getSingleton().getLoggerFactory();
    LoggerContext context = (LoggerContext) factory;
    Logger root = context.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    assertThat(root.getAppender("CONSOLE")).isNotNull();
}
Also used : ILoggerFactory(org.slf4j.ILoggerFactory) Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext) Test(org.junit.Test)

Aggregations

ILoggerFactory (org.slf4j.ILoggerFactory)7 LoggerContext (ch.qos.logback.classic.LoggerContext)5 Logger (ch.qos.logback.classic.Logger)1 JoranConfigurator (ch.qos.logback.classic.joran.JoranConfigurator)1 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 ContextInitializer (ch.qos.logback.classic.util.ContextInitializer)1 Appender (ch.qos.logback.core.Appender)1 Context (ch.qos.logback.core.Context)1 JoranException (ch.qos.logback.core.joran.spi.JoranException)1 File (java.io.File)1 InputStream (java.io.InputStream)1 Method (java.lang.reflect.Method)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 JarEntry (java.util.jar.JarEntry)1 JarOutputStream (java.util.jar.JarOutputStream)1 Nullable (javax.annotation.Nullable)1 Test (org.junit.Test)1