Search in sources :

Example 6 with Category

use of org.apache.log4j.Category 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)

Example 7 with Category

use of org.apache.log4j.Category in project zm-mailbox by Zimbra.

the class ZimbraPatternLayout method main.

public static void main(String[] args) {
    Layout layout = new ZimbraPatternLayout("[%z] - %m%n");
    Category cat = Category.getInstance("some.cat");
    cat.addAppender(new ConsoleAppender(layout, ConsoleAppender.SYSTEM_OUT));
    ZimbraLog.addAccountNameToContext("my@account.com");
    ZimbraLog.addMboxToContext(99);
    cat.debug("Hello, log");
    cat.info("Hello again...");
    ZimbraLog.clearContext();
    cat.info("No more context");
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) Category(org.apache.log4j.Category) Layout(org.apache.log4j.Layout) PatternLayout(org.apache.log4j.PatternLayout)

Example 8 with Category

use of org.apache.log4j.Category in project ignite by apache.

the class Log4JLogger method addConsoleAppenderIfNeeded.

/**
     * Adds console appender when needed with some default logging settings.
     *
     * @param logLevel Optional log level.
     * @param implInitC Optional log implementation init closure.
     */
private void addConsoleAppenderIfNeeded(@Nullable Level logLevel, @Nullable IgniteClosure<Boolean, Logger> implInitC) {
    if (inited) {
        if (implInitC != null)
            // Do not init.
            impl = implInitC.apply(false);
        return;
    }
    synchronized (mux) {
        if (inited) {
            if (implInitC != null)
                // Do not init.
                impl = implInitC.apply(false);
            return;
        }
        if (implInitC != null)
            // Init logger impl.
            impl = implInitC.apply(true);
        boolean quiet = Boolean.valueOf(System.getProperty(IGNITE_QUIET, "true"));
        boolean consoleAppenderFound = false;
        Category rootCategory = null;
        ConsoleAppender errAppender = null;
        for (Category l = impl; l != null; ) {
            if (!consoleAppenderFound) {
                for (Enumeration appenders = l.getAllAppenders(); appenders.hasMoreElements(); ) {
                    Appender appender = (Appender) appenders.nextElement();
                    if (appender instanceof ConsoleAppender) {
                        if ("CONSOLE_ERR".equals(appender.getName())) {
                            // Treat CONSOLE_ERR appender as a system one and don't count it.
                            errAppender = (ConsoleAppender) appender;
                            continue;
                        }
                        consoleAppenderFound = true;
                        break;
                    }
                }
            }
            if (l.getParent() == null) {
                rootCategory = l;
                break;
            } else
                l = l.getParent();
        }
        if (consoleAppenderFound && quiet)
            // User configured console appender, but log is quiet.
            quiet = false;
        if (!consoleAppenderFound && !quiet && Boolean.valueOf(System.getProperty(IGNITE_CONSOLE_APPENDER, "true"))) {
            // Console appender not found => we've looked through all categories up to root.
            assert rootCategory != null;
            // to configuration and did not set IGNITE_CONSOLE_APPENDER to false.
            if (errAppender != null) {
                rootCategory.addAppender(createConsoleAppender(Level.INFO));
                if (errAppender.getThreshold() == Level.ERROR)
                    errAppender.setThreshold(Level.WARN);
            } else
                // No error console appender => create console appender with no level limit.
                rootCategory.addAppender(createConsoleAppender(Level.OFF));
            if (logLevel != null)
                impl.setLevel(logLevel);
        }
        // If still don't have appenders, disable logging.
        if (!isConfigured())
            impl.setLevel(Level.OFF);
        quiet0 = quiet;
        inited = true;
    }
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) Appender(org.apache.log4j.Appender) ConsoleAppender(org.apache.log4j.ConsoleAppender) FileAppender(org.apache.log4j.FileAppender) Category(org.apache.log4j.Category) Enumeration(java.util.Enumeration)

Aggregations

Category (org.apache.log4j.Category)8 Appender (org.apache.log4j.Appender)4 ConsoleAppender (org.apache.log4j.ConsoleAppender)3 Logger (org.apache.log4j.Logger)3 PatternLayout (org.apache.log4j.PatternLayout)3 Test (org.junit.Test)3 AbstractDbAppender (com.axway.ats.log.appenders.AbstractDbAppender)2 ActiveDbAppender (com.axway.ats.log.appenders.ActiveDbAppender)2 PassiveDbAppender (com.axway.ats.log.appenders.PassiveDbAppender)2 Enumeration (java.util.Enumeration)2 FileAppender (org.apache.log4j.FileAppender)2 Layout (org.apache.log4j.Layout)2 LoggingEvent (org.apache.log4j.spi.LoggingEvent)2 NoSystemLevelEventsFilter (com.axway.ats.log.autodb.filters.NoSystemLevelEventsFilter)1 JULBridgeHandler (com.twitter.common.logging.julbridge.JULBridgeHandler)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 LogRecord (java.util.logging.LogRecord)1 LocationInfo (org.apache.log4j.spi.LocationInfo)1