Search in sources :

Example 1 with StatusUtil

use of ch.qos.logback.core.status.StatusUtil in project sling by apache.

the class SlingStatusPrinter method printInCaseOfErrorsOrWarnings.

/**
     * Based on StatusPrinter. printInCaseOfErrorsOrWarnings. This has been adapted
     * to print more context i.e. some message from before the error message to better understand
     * the failure scenario
     *
     * @param threshold   time since which the message have to be checked for errors/warnings
     * @param msgSince    time form which we are interested in the message logs
     * @param initSuccess flag indicating if Logback configuration failed or not
     */
public static void printInCaseOfErrorsOrWarnings(Context context, long threshold, long msgSince, boolean initSuccess) {
    if (context == null) {
        throw new IllegalArgumentException("Context argument cannot be null");
    }
    PrintStream ps = System.out;
    StatusManager sm = context.getStatusManager();
    if (sm == null) {
        ps.println("WARN: Context named \"" + context.getName() + "\" has no status manager");
    } else {
        StatusUtil statusUtil = new StatusUtil(context);
        if (statusUtil.getHighestLevel(threshold) >= ErrorStatus.WARN) {
            List<Status> filteredList = StatusUtil.filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), msgSince);
            print(filteredList, initSuccess);
        }
    }
}
Also used : ErrorStatus(ch.qos.logback.core.status.ErrorStatus) Status(ch.qos.logback.core.status.Status) PrintStream(java.io.PrintStream) StatusManager(ch.qos.logback.core.status.StatusManager) StatusUtil(ch.qos.logback.core.status.StatusUtil)

Example 2 with StatusUtil

use of ch.qos.logback.core.status.StatusUtil 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)

Aggregations

StatusUtil (ch.qos.logback.core.status.StatusUtil)2 JoranConfigurator (ch.qos.logback.classic.joran.JoranConfigurator)1 SaxEvent (ch.qos.logback.core.joran.event.SaxEvent)1 ErrorStatus (ch.qos.logback.core.status.ErrorStatus)1 OnConsoleStatusListener (ch.qos.logback.core.status.OnConsoleStatusListener)1 Status (ch.qos.logback.core.status.Status)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 PrintStream (java.io.PrintStream)1