Search in sources :

Example 6 with JoranConfigurator

use of ch.qos.logback.classic.joran.JoranConfigurator in project spring-boot by spring-projects.

the class LogbackConfigurationTests method consolePatternCanBeOverridden.

@Test
public void consolePatternCanBeOverridden() throws JoranException {
    JoranConfigurator configurator = new JoranConfigurator();
    LoggerContext context = new LoggerContext();
    configurator.setContext(context);
    configurator.doConfigure(new File("src/test/resources/custom-console-log-pattern.xml"));
    Appender<ILoggingEvent> appender = context.getLogger("ROOT").getAppender("CONSOLE");
    assertThat(appender).isInstanceOf(ConsoleAppender.class);
    Encoder<?> encoder = ((ConsoleAppender<?>) appender).getEncoder();
    assertThat(encoder).isInstanceOf(PatternLayoutEncoder.class);
    assertThat(((PatternLayoutEncoder) encoder).getPattern()).isEqualTo("foo");
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) File(java.io.File) Test(org.junit.Test)

Example 7 with JoranConfigurator

use of ch.qos.logback.classic.joran.JoranConfigurator in project chassis by Kixeye.

the class LoggingConfiguration method reloadLogging.

/**
	 * Reloads logging.
	 * 
	 * @param logbackConfig XML containing the logback configuration
	 */
public void reloadLogging(String logbackConfig) {
    LoggerContext logContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    try {
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(logContext);
        logContext.reset();
        configurator.doConfigure(new ByteArrayInputStream(logbackConfig.getBytes(Charsets.UTF_8)));
    } catch (JoranException je) {
    // StatusPrinter will handle this
    } catch (Exception ex) {
        // Just in case, so we see a stacktrace
        ex.printStackTrace();
    }
    StatusPrinter.printInCaseOfErrorsOrWarnings(logContext);
    applicationContext.publishEvent(new LoggingReloadedApplicationEvent(this));
}
Also used : LoggingReloadedApplicationEvent(com.kixeye.chassis.support.events.LoggingReloadedApplicationEvent) ByteArrayInputStream(java.io.ByteArrayInputStream) JoranException(ch.qos.logback.core.joran.spi.JoranException) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) LoggerContext(ch.qos.logback.classic.LoggerContext) JoranException(ch.qos.logback.core.joran.spi.JoranException)

Example 8 with JoranConfigurator

use of ch.qos.logback.classic.joran.JoranConfigurator in project perun by CESNET.

the class PerunLogbackConfigurator method configure.

@Override
public void configure(LoggerContext loggerContext) {
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(loggerContext);
    String confDir = System.getProperty("perun.conf.custom", "/etc/perun/");
    File confFile = Paths.get(confDir, "logback.xml").toFile();
    if (confFile.exists()) {
        System.out.println("Loading logback config file " + confFile);
        try {
            // loads logback file
            configurator.doConfigure(confFile.toString());
        } catch (JoranException e) {
            e.printStackTrace();
        }
    } else {
        System.out.println("Loading logback-default.xml file from classpath");
        try (InputStream configStream = this.getClass().getResourceAsStream("/logback-default.xml")) {
            // loads logback file
            configurator.doConfigure(configStream);
            configStream.close();
        } catch (IOException | JoranException e) {
            e.printStackTrace();
            System.out.println("Falling back to logback basic configurator");
            BasicConfigurator basicConfigurator = new BasicConfigurator();
            basicConfigurator.setContext(loggerContext);
            basicConfigurator.configure(loggerContext);
        }
    }
}
Also used : BasicConfigurator(ch.qos.logback.classic.BasicConfigurator) JoranException(ch.qos.logback.core.joran.spi.JoranException) InputStream(java.io.InputStream) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) IOException(java.io.IOException) File(java.io.File)

Example 9 with JoranConfigurator

use of ch.qos.logback.classic.joran.JoranConfigurator in project sling by apache.

the class LogbackManager method configure.

private void configure(ConfiguratorCallback cb) {
    long startTime = System.currentTimeMillis();
    StatusListener statusListener = new StatusListenerAsList();
    if (debug) {
        statusListener = new OnConsoleStatusListener();
    }
    getStatusManager().add(statusListener);
    addInfo("Resetting context: " + getLoggerContext().getName());
    resetContext(statusListener);
    StatusUtil statusUtil = new StatusUtil(getLoggerContext());
    JoranConfigurator configurator = createConfigurator();
    final List<SaxEvent> eventList = configurator.recallSafeConfiguration();
    final long threshold = System.currentTimeMillis();
    boolean success = false;
    try {
        cb.perform(configurator);
        if (statusUtil.hasXMLParsingErrors(threshold)) {
            cb.fallbackConfiguration(eventList, createConfigurator(), statusListener);
        }
        addInfo("Context: " + getLoggerContext().getName() + " reloaded.");
        success = true;
    } catch (Throwable t) {
        //Need to catch any error as Logback must work in all scenarios
        //The error would be dumped to sysout in later call to Status printer
        addError("Error occurred while configuring Logback", t);
    } finally {
        if (!success) {
            cb.fallbackConfiguration(eventList, createConfigurator(), statusListener);
        }
        getStatusManager().remove(statusListener);
        SlingStatusPrinter.printInCaseOfErrorsOrWarnings(getLoggerContext(), resetStartTime, startTime, success);
    }
}
Also used : JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) SaxEvent(ch.qos.logback.core.joran.event.SaxEvent) OnConsoleStatusListener(ch.qos.logback.core.status.OnConsoleStatusListener) StatusListener(ch.qos.logback.core.status.StatusListener) OnConsoleStatusListener(ch.qos.logback.core.status.OnConsoleStatusListener) StatusListenerAsList(ch.qos.logback.core.status.StatusListenerAsList) StatusUtil(ch.qos.logback.core.status.StatusUtil)

Example 10 with JoranConfigurator

use of ch.qos.logback.classic.joran.JoranConfigurator in project midpoint by Evolveum.

the class StartupConfiguration method setupInitialLogging.

private void setupInitialLogging(File midpointHome) {
    File logbackConfigFile = new File(midpointHome, LOGBACK_CONFIG_FILENAME);
    boolean clear = false;
    if (logbackConfigFile.exists()) {
        clear = true;
    } else {
        logbackConfigFile = new File(midpointHome, LOGBACK_EXTRA_CONFIG_FILENAME);
        if (!logbackConfigFile.exists()) {
            return;
        }
    }
    LOGGER.info("Loading logging configuration from {} ({})", logbackConfigFile, clear ? "clearing default configuration" : "extending defalt configuration");
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    if (clear) {
        context.reset();
    }
    try {
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(context);
        configurator.doConfigure(logbackConfigFile);
    } catch (Exception e) {
        // This will logged by defalt logging configuration
        LOGGER.error("Error loading additional logging configuration: {}", e.getMessage(), e);
        // If normal logging fail make sure it is logged by web container
        e.printStackTrace();
    }
    StatusPrinter.printInCaseOfErrorsOrWarnings(context);
}
Also used : JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) File(java.io.File) LoggerContext(ch.qos.logback.classic.LoggerContext) SystemException(com.evolveum.midpoint.util.exception.SystemException) JoranException(ch.qos.logback.core.joran.spi.JoranException)

Aggregations

JoranConfigurator (ch.qos.logback.classic.joran.JoranConfigurator)24 LoggerContext (ch.qos.logback.classic.LoggerContext)15 JoranException (ch.qos.logback.core.joran.spi.JoranException)12 File (java.io.File)7 Test (org.junit.Test)6 InputStream (java.io.InputStream)4 Logger (org.slf4j.Logger)4 AppenderContext (co.cask.cdap.api.logging.AppenderContext)3 NoOpMetricsCollectionService (co.cask.cdap.common.metrics.NoOpMetricsCollectionService)3 DatasetFramework (co.cask.cdap.data2.dataset2.DatasetFramework)3 LocalAppenderContext (co.cask.cdap.logging.framework.LocalAppenderContext)3 StringReader (java.io.StringReader)3 URL (java.net.URL)3 InputSource (org.xml.sax.InputSource)3 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)2 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)2 ContextInitializer (ch.qos.logback.classic.util.ContextInitializer)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 StringWriter (java.io.StringWriter)2 Transformer (javax.xml.transform.Transformer)2