Search in sources :

Example 1 with OnConsoleStatusListener

use of ch.qos.logback.core.status.OnConsoleStatusListener 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 2 with OnConsoleStatusListener

use of ch.qos.logback.core.status.OnConsoleStatusListener in project cdap by caskdata.

the class LogAppenderInitializer method initialize.

@VisibleForTesting
public synchronized void initialize(String loggerName) {
    LoggerContext loggerContext = getLoggerContext();
    if (loggerContext != null) {
        Logger logger = loggerContext.getLogger(loggerName);
        // Check if the logger already contains the logAppender
        if (Iterators.contains(logger.iteratorForAppenders(), logAppender)) {
            LOG.warn("Log appender {} is already initialized.", logAppender.getName());
            return;
        }
        logAppender.setContext(loggerContext);
        LOG.info("Initializing log appender {}", logAppender.getName());
        // Display any errors during initialization of log appender to console
        StatusManager statusManager = loggerContext.getStatusManager();
        OnConsoleStatusListener onConsoleListener = new OnConsoleStatusListener();
        statusManager.add(onConsoleListener);
        logAppender.start();
        logger.addAppender(logAppender);
    }
}
Also used : StatusManager(ch.qos.logback.core.status.StatusManager) OnConsoleStatusListener(ch.qos.logback.core.status.OnConsoleStatusListener) Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

OnConsoleStatusListener (ch.qos.logback.core.status.OnConsoleStatusListener)2 Logger (ch.qos.logback.classic.Logger)1 LoggerContext (ch.qos.logback.classic.LoggerContext)1 JoranConfigurator (ch.qos.logback.classic.joran.JoranConfigurator)1 SaxEvent (ch.qos.logback.core.joran.event.SaxEvent)1 StatusListener (ch.qos.logback.core.status.StatusListener)1 StatusListenerAsList (ch.qos.logback.core.status.StatusListenerAsList)1 StatusManager (ch.qos.logback.core.status.StatusManager)1 StatusUtil (ch.qos.logback.core.status.StatusUtil)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1