Search in sources :

Example 11 with ConsoleAppender

use of org.apache.log4j.ConsoleAppender 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 12 with ConsoleAppender

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

the class GridAbstractTest method resetLog4j.

/**
     * Resets log4j programmatically.
     *
     * @param log4jLevel Level.
     * @param logToFile If {@code true}, then log to file under "work/log" folder.
     * @param cat Category.
     * @param cats Additional categories.
     */
@SuppressWarnings({ "deprecation" })
protected void resetLog4j(Level log4jLevel, boolean logToFile, String cat, String... cats) throws IgniteCheckedException {
    for (String c : F.concat(false, cat, F.asList(cats))) Logger.getLogger(c).setLevel(log4jLevel);
    if (logToFile) {
        Logger log4j = Logger.getRootLogger();
        log4j.removeAllAppenders();
        // Console appender.
        ConsoleAppender c = new ConsoleAppender();
        c.setName("CONSOLE_ERR");
        c.setTarget("System.err");
        c.setThreshold(Priority.WARN);
        c.setLayout(new PatternLayout("[%d{ABSOLUTE}][%-5p][%t][%c{1}] %m%n"));
        c.activateOptions();
        log4j.addAppender(c);
        // File appender.
        RollingFileAppender file = new RollingFileAppender();
        file.setName("FILE");
        file.setThreshold(log4jLevel);
        file.setFile(home() + "/work/log/ignite.log");
        file.setAppend(false);
        file.setMaxFileSize("10MB");
        file.setMaxBackupIndex(10);
        file.setLayout(new PatternLayout("[%d{ABSOLUTE}][%-5p][%t][%c{1}] %m%n"));
        file.activateOptions();
        log4j.addAppender(file);
    }
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) RollingFileAppender(org.apache.log4j.RollingFileAppender) PatternLayout(org.apache.log4j.PatternLayout) NullLogger(org.apache.ignite.logger.NullLogger) GridTestLog4jLogger(org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger) IgniteLogger(org.apache.ignite.IgniteLogger) Logger(org.apache.log4j.Logger)

Example 13 with ConsoleAppender

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

the class GridCacheAbstractLoadTest method initLogger.

/**
     * Initializes logger.
     *
     * @param log Log file name.
     * @return Logger.
     * @throws IgniteCheckedException If file initialization failed.
     */
protected IgniteLogger initLogger(String log) throws IgniteCheckedException {
    Logger impl = Logger.getRootLogger();
    impl.removeAllAppenders();
    String fileName = U.getIgniteHome() + "/work/log/" + log;
    // Configure output that should go to System.out
    RollingFileAppender fileApp;
    String fmt = "[%d{ABSOLUTE}][%-5p][%t][%c{1}] %m%n";
    try {
        fileApp = new RollingFileAppender(new PatternLayout(fmt), fileName);
        fileApp.setMaxBackupIndex(0);
        fileApp.setAppend(false);
        // fileApp.rollOver();
        fileApp.activateOptions();
    } catch (IOException e) {
        throw new IgniteCheckedException("Unable to initialize file appender.", e);
    }
    LevelRangeFilter lvlFilter = new LevelRangeFilter();
    lvlFilter.setLevelMin(Level.DEBUG);
    fileApp.addFilter(lvlFilter);
    impl.addAppender(fileApp);
    // Configure output that should go to System.out
    ConsoleAppender conApp = new ConsoleAppender(new PatternLayout(fmt), ConsoleAppender.SYSTEM_OUT);
    lvlFilter = new LevelRangeFilter();
    lvlFilter.setLevelMin(Level.DEBUG);
    lvlFilter.setLevelMax(Level.INFO);
    conApp.addFilter(lvlFilter);
    conApp.activateOptions();
    impl.addAppender(conApp);
    // Configure output that should go to System.err
    conApp = new ConsoleAppender(new PatternLayout(fmt), ConsoleAppender.SYSTEM_ERR);
    conApp.setThreshold(Level.WARN);
    conApp.activateOptions();
    impl.addAppender(conApp);
    impl.setLevel(Level.INFO);
    return new GridTestLog4jLogger(false);
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) RollingFileAppender(org.apache.log4j.RollingFileAppender) PatternLayout(org.apache.log4j.PatternLayout) LevelRangeFilter(org.apache.log4j.varia.LevelRangeFilter) IOException(java.io.IOException) IgniteLogger(org.apache.ignite.IgniteLogger) Logger(org.apache.log4j.Logger) GridTestLog4jLogger(org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger) GridTestLog4jLogger(org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger)

Example 14 with ConsoleAppender

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

the class GridSingleExecutionTest method initLogger.

/**
     * Initializes logger.
     *
     * @param log Log file name.
     * @return Logger.
     * @throws IgniteCheckedException If file initialization failed.
     */
private static IgniteLogger initLogger(String log) throws IgniteCheckedException {
    Logger impl = Logger.getRootLogger();
    impl.removeAllAppenders();
    String fileName = U.getIgniteHome() + "/work/log/" + log;
    // Configure output that should go to System.out
    RollingFileAppender fileApp;
    String fmt = "[%d{ABSOLUTE}][%-5p][%t][%c{1}] %m%n";
    try {
        fileApp = new RollingFileAppender(new PatternLayout(fmt), fileName);
        fileApp.setMaxBackupIndex(0);
        fileApp.rollOver();
    } catch (IOException e) {
        throw new IgniteCheckedException("Unable to initialize file appender.", e);
    }
    LevelRangeFilter lvlFilter = new LevelRangeFilter();
    lvlFilter.setLevelMin(Level.DEBUG);
    fileApp.addFilter(lvlFilter);
    impl.addAppender(fileApp);
    // Configure output that should go to System.out
    ConsoleAppender conApp = new ConsoleAppender(new PatternLayout(fmt), ConsoleAppender.SYSTEM_OUT);
    lvlFilter = new LevelRangeFilter();
    lvlFilter.setLevelMin(Level.INFO);
    lvlFilter.setLevelMax(Level.INFO);
    conApp.addFilter(lvlFilter);
    impl.addAppender(conApp);
    // Configure output that should go to System.err
    conApp = new ConsoleAppender(new PatternLayout(fmt), ConsoleAppender.SYSTEM_ERR);
    conApp.setThreshold(Level.WARN);
    impl.addAppender(conApp);
    impl.setLevel(Level.INFO);
    Logger.getLogger("org.apache.ignite").setLevel(Level.DEBUG);
    return new GridTestLog4jLogger(false);
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) RollingFileAppender(org.apache.log4j.RollingFileAppender) PatternLayout(org.apache.log4j.PatternLayout) LevelRangeFilter(org.apache.log4j.varia.LevelRangeFilter) IOException(java.io.IOException) IgniteLogger(org.apache.ignite.IgniteLogger) Logger(org.apache.log4j.Logger) GridTestLog4jLogger(org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger) GridTestLog4jLogger(org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger)

Example 15 with ConsoleAppender

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

the class Log4JLogger method createConsoleAppender.

/**
     * Creates console appender with some reasonable default logging settings.
     *
     * @param maxLevel Max logging level.
     * @return New console appender.
     */
private Appender createConsoleAppender(Level maxLevel) {
    String fmt = "[%d{ABSOLUTE}][%-5p][%t][%c{1}] %m%n";
    // Configure output that should go to System.out
    Appender app = new ConsoleAppender(new PatternLayout(fmt), ConsoleAppender.SYSTEM_OUT);
    LevelRangeFilter lvlFilter = new LevelRangeFilter();
    lvlFilter.setLevelMin(Level.TRACE);
    lvlFilter.setLevelMax(maxLevel);
    app.addFilter(lvlFilter);
    return app;
}
Also used : Appender(org.apache.log4j.Appender) ConsoleAppender(org.apache.log4j.ConsoleAppender) FileAppender(org.apache.log4j.FileAppender) ConsoleAppender(org.apache.log4j.ConsoleAppender) PatternLayout(org.apache.log4j.PatternLayout) LevelRangeFilter(org.apache.log4j.varia.LevelRangeFilter)

Aggregations

ConsoleAppender (org.apache.log4j.ConsoleAppender)32 PatternLayout (org.apache.log4j.PatternLayout)23 ParseException (org.apache.commons.cli.ParseException)9 FileAppender (org.apache.log4j.FileAppender)9 Logger (org.apache.log4j.Logger)9 IOException (java.io.IOException)8 CommandLineParser (org.apache.commons.cli.CommandLineParser)8 Appender (org.apache.log4j.Appender)8 FileInputStream (java.io.FileInputStream)7 CommandLine (org.apache.commons.cli.CommandLine)7 GnuParser (org.apache.commons.cli.GnuParser)7 Options (org.apache.commons.cli.Options)7 Properties (java.util.Properties)6 Option (org.apache.commons.cli.Option)6 Enumeration (java.util.Enumeration)4 LevelRangeFilter (org.apache.log4j.varia.LevelRangeFilter)4 IgniteLogger (org.apache.ignite.IgniteLogger)3 GridTestLog4jLogger (org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger)3 RollingFileAppender (org.apache.log4j.RollingFileAppender)3 ConfigLoader (com.linkedin.databus.core.util.ConfigLoader)2