Search in sources :

Example 1 with NoSystemLevelEventsFilter

use of com.axway.ats.log.autodb.filters.NoSystemLevelEventsFilter in project ats-framework by Axway.

the class RemoteLoggingConfigurator method apply.

@Override
@SuppressWarnings("unchecked")
public void apply() {
    if (needsToConfigureDbAppender) {
        //first get all appenders in the root category and apply the filter
        //which will deny logging of system events
        Logger rootLogger = Logger.getRootLogger();
        Enumeration<Appender> appenders = rootLogger.getAllAppenders();
        while (appenders.hasMoreElements()) {
            Appender appender = appenders.nextElement();
            if (!(appender instanceof AbstractDbAppender)) {
                // apply this filter on all appenders which are not coming from ATS
                appender.addFilter(new NoSystemLevelEventsFilter());
            }
        }
        final String caller = ThreadsPerCaller.getCaller();
        //create the new appender
        PassiveDbAppender attachedAppender = new PassiveDbAppender(caller);
        attachedAppender.setAppenderConfig(appenderConfiguration);
        //use a default pattern, as we log in the db
        attachedAppender.setLayout(new PatternLayout("%c{2}: %m%n"));
        attachedAppender.activateOptions();
        //attach the appender to the logging system
        Category log;
        if ("root".equals(appenderLogger)) {
            log = Logger.getRootLogger();
        } else {
            log = Logger.getLogger(appenderLogger);
        }
        log.setLevel(Level.toLevel(appenderConfiguration.getLoggingThreshold().toInt()));
        log.addAppender(attachedAppender);
    }
    if (needsToConfigureUserLoggers) {
        for (Entry<String, Integer> userLogger : otherLoggerLevels.entrySet()) {
            /* 
                 * We want to set the level of this logger.
                 * It is not important if this logger is already attached to log4j system or 
                 * not as the next code will obtain it(in case logger exists) or will create it 
                 * and then will set its level
                 */
            Logger.getLogger(userLogger.getKey()).setLevel(Level.toLevel(userLogger.getValue()));
        }
    }
}
Also used : ActiveDbAppender(com.axway.ats.log.appenders.ActiveDbAppender) PassiveDbAppender(com.axway.ats.log.appenders.PassiveDbAppender) Appender(org.apache.log4j.Appender) AbstractDbAppender(com.axway.ats.log.appenders.AbstractDbAppender) NoSystemLevelEventsFilter(com.axway.ats.log.autodb.filters.NoSystemLevelEventsFilter) Category(org.apache.log4j.Category) PatternLayout(org.apache.log4j.PatternLayout) AbstractDbAppender(com.axway.ats.log.appenders.AbstractDbAppender) Logger(org.apache.log4j.Logger) PassiveDbAppender(com.axway.ats.log.appenders.PassiveDbAppender)

Aggregations

AbstractDbAppender (com.axway.ats.log.appenders.AbstractDbAppender)1 ActiveDbAppender (com.axway.ats.log.appenders.ActiveDbAppender)1 PassiveDbAppender (com.axway.ats.log.appenders.PassiveDbAppender)1 NoSystemLevelEventsFilter (com.axway.ats.log.autodb.filters.NoSystemLevelEventsFilter)1 Appender (org.apache.log4j.Appender)1 Category (org.apache.log4j.Category)1 Logger (org.apache.log4j.Logger)1 PatternLayout (org.apache.log4j.PatternLayout)1