Search in sources :

Example 1 with StatusManager

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

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

StatusManager (ch.qos.logback.core.status.StatusManager)2 Logger (ch.qos.logback.classic.Logger)1 LoggerContext (ch.qos.logback.classic.LoggerContext)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 StatusUtil (ch.qos.logback.core.status.StatusUtil)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 PrintStream (java.io.PrintStream)1