Search in sources :

Example 21 with Appender

use of org.apache.log4j.Appender in project ats-framework by Axway.

the class AtsDbLogger method getCurrentTestCaseState.

/**
     * This event can not go through the regular way of sending log4j events in the case with Passive DB appenders. 
     * The reason is that we have to evaluate the result after the work of each passive appender and stop
     * calling these appenders when the first one(the only one serving this caller) has processed the event. 
     */
@SuppressWarnings("unchecked")
public TestCaseState getCurrentTestCaseState() {
    GetCurrentTestCaseEvent event = new GetCurrentTestCaseEvent(ATS_DB_LOGGER_CLASS_NAME, logger);
    Enumeration<Appender> appenders = Logger.getRootLogger().getAllAppenders();
    while (appenders.hasMoreElements()) {
        Appender appender = appenders.nextElement();
        if (appender instanceof ActiveDbAppender) {
            // Comes here on Test Executor side. There is just 1 Active appender
            return ((ActiveDbAppender) appender).getCurrentTestCaseState(event).getTestCaseState();
        } else if (appender instanceof PassiveDbAppender) {
            // Comes here on Agent side. There will be 1 Passive appender per caller
            // Pass the event to any existing appender.
            // The correct one will return result, wrong appenders will return null.
            GetCurrentTestCaseEvent resultEvent = ((PassiveDbAppender) appender).getCurrentTestCaseState(event);
            if (resultEvent != null) {
                // we found the right Passive appender
                return resultEvent.getTestCaseState();
            }
        }
    }
    // no appropriate appender found
    return null;
}
Also used : ActiveDbAppender(com.axway.ats.log.appenders.ActiveDbAppender) PassiveDbAppender(com.axway.ats.log.appenders.PassiveDbAppender) Appender(org.apache.log4j.Appender) ActiveDbAppender(com.axway.ats.log.appenders.ActiveDbAppender) GetCurrentTestCaseEvent(com.axway.ats.log.autodb.events.GetCurrentTestCaseEvent) PassiveDbAppender(com.axway.ats.log.appenders.PassiveDbAppender)

Example 22 with Appender

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

the class PlaybackUtil method setup.

private static void setup() throws ServiceException {
    // set up log4j
    ZimbraLog.toolSetupLog4j("INFO", LC.zimbra_log4j_properties.value());
    // remove the console appender if any
    Logger rootLogger = Logger.getRootLogger();
    Appender consoleAppender = null;
    Enumeration appenders = rootLogger.getAllAppenders();
    while (appenders.hasMoreElements()) {
        Appender appender = (Appender) appenders.nextElement();
        if (appender instanceof ConsoleAppender) {
            consoleAppender = appender;
        }
    }
    if (consoleAppender != null)
        rootLogger.removeAppender(consoleAppender);
    DbPool.startup();
    Zimbra.startupCLI();
}
Also used : Appender(org.apache.log4j.Appender) ConsoleAppender(org.apache.log4j.ConsoleAppender) ConsoleAppender(org.apache.log4j.ConsoleAppender) Enumeration(java.util.Enumeration) Logger(org.apache.log4j.Logger)

Example 23 with Appender

use of org.apache.log4j.Appender in project apex-core by apache.

the class ApexCli method preImpersonationInit.

public void preImpersonationInit(String[] args) throws IOException {
    Signal.handle(new Signal("INT"), new SignalHandler() {

        @Override
        public void handle(Signal sig) {
            System.out.println("^C");
            if (commandThread != null) {
                commandThread.interrupt();
                mainThread.interrupt();
            } else {
                System.out.print(prompt);
                System.out.flush();
            }
        }
    });
    consolePresent = (System.console() != null);
    Options options = new Options();
    options.addOption("e", true, "Commands are read from the argument");
    options.addOption("v", false, "Verbose mode level 1");
    options.addOption("vv", false, "Verbose mode level 2");
    options.addOption("vvv", false, "Verbose mode level 3");
    options.addOption("vvvv", false, "Verbose mode level 4");
    options.addOption("r", false, "JSON Raw mode");
    options.addOption("p", true, "JSONP padding function");
    options.addOption("h", false, "Print this help");
    options.addOption("f", true, "Use the specified prompt at all time");
    options.addOption("kp", true, "Use the specified kerberos principal");
    options.addOption("kt", true, "Use the specified kerberos keytab");
    CommandLineParser parser = new BasicParser();
    try {
        CommandLine cmd = parser.parse(options, args);
        if (cmd.hasOption("v")) {
            verboseLevel = 1;
        }
        if (cmd.hasOption("vv")) {
            verboseLevel = 2;
        }
        if (cmd.hasOption("vvv")) {
            verboseLevel = 3;
        }
        if (cmd.hasOption("vvvv")) {
            verboseLevel = 4;
        }
        if (cmd.hasOption("r")) {
            raw = true;
        }
        if (cmd.hasOption("e")) {
            commandsToExecute = cmd.getOptionValues("e");
            consolePresent = false;
        }
        if (cmd.hasOption("p")) {
            jsonp = cmd.getOptionValue("p");
        }
        if (cmd.hasOption("f")) {
            forcePrompt = cmd.getOptionValue("f");
        }
        if (cmd.hasOption("h")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(ApexCli.class.getSimpleName(), options);
            System.exit(0);
        }
        if (cmd.hasOption("kp")) {
            kerberosPrincipal = cmd.getOptionValue("kp");
        }
        if (cmd.hasOption("kt")) {
            kerberosKeyTab = cmd.getOptionValue("kt");
        }
    } catch (ParseException ex) {
        System.err.println("Invalid argument: " + ex);
        System.exit(1);
    }
    if (kerberosPrincipal == null && kerberosKeyTab != null) {
        System.err.println("Kerberos key tab is specified but not the kerberos principal. Please specify it using the -kp option.");
        System.exit(1);
    }
    if (kerberosPrincipal != null && kerberosKeyTab == null) {
        System.err.println("Kerberos principal is specified but not the kerberos key tab. Please specify it using the -kt option.");
        System.exit(1);
    }
    Level logLevel;
    switch(verboseLevel) {
        case 0:
            logLevel = Level.OFF;
            break;
        case 1:
            logLevel = Level.ERROR;
            break;
        case 2:
            logLevel = Level.WARN;
            break;
        case 3:
            logLevel = Level.INFO;
            break;
        default:
            logLevel = Level.DEBUG;
            break;
    }
    for (org.apache.log4j.Logger logger : new org.apache.log4j.Logger[] { org.apache.log4j.Logger.getRootLogger(), org.apache.log4j.Logger.getLogger(ApexCli.class) }) {
        /*
      * Override logLevel specified by user, the same logLevel would be inherited by all
      * appenders related to logger.
      */
        logger.setLevel(logLevel);
        @SuppressWarnings("unchecked") Enumeration<Appender> allAppenders = logger.getAllAppenders();
        while (allAppenders.hasMoreElements()) {
            Appender appender = allAppenders.nextElement();
            if (appender instanceof ConsoleAppender) {
                ((ConsoleAppender) appender).setThreshold(logLevel);
            }
        }
    }
    if (commandsToExecute != null) {
        for (String command : commandsToExecute) {
            LOG.debug("Command to be executed: {}", command);
        }
    }
    if (kerberosPrincipal != null && kerberosKeyTab != null) {
        StramUserLogin.authenticate(kerberosPrincipal, kerberosKeyTab);
    } else {
        Configuration config = new YarnConfiguration();
        StramClientUtils.addDTLocalResources(config);
        StramUserLogin.attemptAuthentication(config);
    }
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) Appender(org.apache.log4j.Appender) ConsoleAppender(org.apache.log4j.ConsoleAppender) Options(org.apache.commons.cli.Options) Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) DTConfiguration(com.datatorrent.stram.client.DTConfiguration) LogicalPlanConfiguration(com.datatorrent.stram.plan.logical.LogicalPlanConfiguration) Logger(org.slf4j.Logger) HelpFormatter(org.apache.commons.cli.HelpFormatter) BasicParser(org.apache.commons.cli.BasicParser) Signal(sun.misc.Signal) CommandLine(org.apache.commons.cli.CommandLine) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) SignalHandler(sun.misc.SignalHandler) Level(org.apache.log4j.Level) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 24 with Appender

use of org.apache.log4j.Appender in project apex-core by apache.

the class LoggerUtil method getFileAppender.

private static FileAppender getFileAppender() {
    Enumeration<Appender> e = LogManager.getRootLogger().getAllAppenders();
    FileAppender fileAppender = null;
    while (e.hasMoreElements()) {
        Appender appender = e.nextElement();
        if (appender instanceof FileAppender) {
            if (fileAppender == null) {
                fileAppender = (FileAppender) appender;
            } else {
                //skip fetching log file information if we have multiple file Appenders
                return null;
            }
        }
    }
    return fileAppender;
}
Also used : Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) FileAppender(org.apache.log4j.FileAppender)

Example 25 with Appender

use of org.apache.log4j.Appender in project apex-core by apache.

the class LoggerUtil method addAppender.

/**
   * Adds Logger Appender
   * @param name Appender name
   * @param properties Appender properties
   * @return True if the appender has been added successfully
   */
public static boolean addAppender(String name, Properties properties) {
    if (getAppendersNames().contains(name)) {
        logger.warn("A logger appender with the name '{}' exists. Cannot add a new logger appender with the same name", name);
    } else {
        try {
            Method method = PropertyConfigurator.class.getDeclaredMethod("parseAppender", Properties.class, String.class);
            method.setAccessible(true);
            Appender appender = (Appender) method.invoke(new PropertyConfigurator(), properties, name);
            if (appender == null) {
                logger.warn("Cannot add a new logger appender. Name: {}, Properties: {}", name, properties);
            } else {
                LogManager.getRootLogger().addAppender(appender);
                return true;
            }
        } catch (Exception ex) {
            logger.warn("Cannot add a new logger appender. Name: {}, Properties: {}", name, properties, ex);
        }
    }
    return false;
}
Also used : Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) Method(java.lang.reflect.Method) PropertyConfigurator(org.apache.log4j.PropertyConfigurator) IOException(java.io.IOException)

Aggregations

Appender (org.apache.log4j.Appender)54 FileAppender (org.apache.log4j.FileAppender)17 Logger (org.apache.log4j.Logger)15 File (java.io.File)13 ConsoleAppender (org.apache.log4j.ConsoleAppender)11 RollingFileAppender (org.apache.log4j.RollingFileAppender)11 Log4JLogger (org.apache.commons.logging.impl.Log4JLogger)8 Enumeration (java.util.Enumeration)7 AsyncAppender (org.apache.log4j.AsyncAppender)6 PatternLayout (org.apache.log4j.PatternLayout)6 IOException (java.io.IOException)5 ActiveDbAppender (com.axway.ats.log.appenders.ActiveDbAppender)4 PassiveDbAppender (com.axway.ats.log.appenders.PassiveDbAppender)4 MalformedURLException (java.net.MalformedURLException)4 Map (java.util.Map)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)4 Category (org.apache.log4j.Category)4 DailyRollingFileAppender (org.apache.log4j.DailyRollingFileAppender)4 Layout (org.apache.log4j.Layout)4