Search in sources :

Example 6 with ILoggerFactory

use of org.slf4j.ILoggerFactory in project pulsar by yahoo.

the class MessagingServiceShutdownHook method immediateFlushBufferedLogs.

public static void immediateFlushBufferedLogs() {
    ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
    if (loggerFactory.getClass().getName().equals(LogbackLoggerContextClassName)) {
        // Use reflection to force the flush on the logger
        try {
            Class<?> logbackLoggerContextClass = Class.forName(LogbackLoggerContextClassName);
            Method stop = logbackLoggerContextClass.getMethod("stop");
            stop.invoke(loggerFactory);
        } catch (Throwable t) {
            LOG.info("Failed to flush logs", t);
        }
    }
}
Also used : ILoggerFactory(org.slf4j.ILoggerFactory) Method(java.lang.reflect.Method)

Example 7 with ILoggerFactory

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

the class ProgramRunners method createLogbackJar.

/**
   * Creates a jar that contains a logback.xml configured for the current process
   *
   * @param targetLocation the jar location
   * @return the {@link Location} where the jar was created to or {@code null} if "logback.xml" is not found
   *         in the current ClassLoader.
   * @throws IOException if failed in reading the logback xml or writing out the jar
   */
@Nullable
public static Location createLogbackJar(Location targetLocation) throws IOException {
    ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
    if (!(loggerFactory instanceof Context)) {
        return null;
    }
    URL logbackURL = ConfigurationWatchListUtil.getMainWatchURL((Context) loggerFactory);
    if (logbackURL == null) {
        return null;
    }
    try (InputStream input = logbackURL.openStream()) {
        try (JarOutputStream output = new JarOutputStream(targetLocation.getOutputStream())) {
            output.putNextEntry(new JarEntry("logback.xml"));
            ByteStreams.copy(input, output);
        }
        return targetLocation;
    }
}
Also used : Context(ch.qos.logback.core.Context) ILoggerFactory(org.slf4j.ILoggerFactory) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) Nullable(javax.annotation.Nullable)

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