Search in sources :

Example 26 with ConsoleAppender

use of org.apache.log4j.ConsoleAppender in project databus by linkedin.

the class BootstrapAddSource method parseArgs.

@SuppressWarnings("static-access")
public static Config parseArgs(String[] args) throws Exception {
    CommandLineParser cliParser = new GnuParser();
    Option helpOption = OptionBuilder.withLongOpt(HELP_OPT_LONG_NAME).withDescription("Help screen").create(HELP_OPT_CHAR);
    Option sourceIdOption = OptionBuilder.withLongOpt(SOURCE_ID_OPT_LONG_NAME).withDescription("Source ID for which tables need to be added").hasArg().withArgName("Source ID").create(SOURCE_ID_OPT_CHAR);
    Option sourceNameOption = OptionBuilder.withLongOpt(SOURCE_NAME_OPT_LONG_NAME).withDescription("Source Name for which tables need to be added").hasArg().withArgName("Source ID").create(SOURCE_NAME_OPT_CHAR);
    Option dbOption = OptionBuilder.withLongOpt(BOOTSTRAP_DB_PROPS_OPT_LONG_NAME).withDescription("Bootstrap producer properties to use").hasArg().withArgName("property_file").create(BOOTSTRAP_DB_PROP_OPT_CHAR);
    Option cmdLinePropsOption = OptionBuilder.withLongOpt(CMD_LINE_PROPS_OPT_LONG_NAME).withDescription("Cmd line override of config properties. Semicolon separated.").hasArg().withArgName("Semicolon_separated_properties").create(CMD_LINE_PROPS_OPT_CHAR);
    Option log4jPropsOption = OptionBuilder.withLongOpt(LOG4J_PROPS_OPT_LONG_NAME).withDescription("Log4j properties to use").hasArg().withArgName("property_file").create(LOG4J_PROPS_OPT_CHAR);
    Options options = new Options();
    options.addOption(helpOption);
    options.addOption(sourceIdOption);
    options.addOption(sourceNameOption);
    options.addOption(dbOption);
    options.addOption(cmdLinePropsOption);
    options.addOption(log4jPropsOption);
    CommandLine cmd = null;
    try {
        cmd = cliParser.parse(options, args);
    } catch (ParseException pe) {
        LOG.error("Bootstrap Physical Config: failed to parse command-line options.", pe);
        throw new RuntimeException("Bootstrap Physical Config: failed to parse command-line options.", pe);
    }
    if (cmd.hasOption(LOG4J_PROPS_OPT_CHAR)) {
        String log4jPropFile = cmd.getOptionValue(LOG4J_PROPS_OPT_CHAR);
        PropertyConfigurator.configure(log4jPropFile);
        LOG.info("Using custom logging settings from file " + log4jPropFile);
    } else {
        PatternLayout defaultLayout = new PatternLayout("%d{ISO8601} +%r [%t] (%p) {%c} %m%n");
        ConsoleAppender defaultAppender = new ConsoleAppender(defaultLayout);
        Logger.getRootLogger().removeAllAppenders();
        Logger.getRootLogger().addAppender(defaultAppender);
        LOG.info("Using default logging settings");
    }
    if (cmd.hasOption(HELP_OPT_CHAR)) {
        printCliHelp(options);
        System.exit(0);
    }
    if (!cmd.hasOption(SOURCE_ID_OPT_CHAR))
        throw new RuntimeException("Source ID is not provided");
    if (!cmd.hasOption(SOURCE_NAME_OPT_CHAR))
        throw new RuntimeException("Source Name is not provided");
    if (!cmd.hasOption(BOOTSTRAP_DB_PROP_OPT_CHAR))
        throw new RuntimeException("Bootstrap config is not provided");
    String propFile = cmd.getOptionValue(BOOTSTRAP_DB_PROP_OPT_CHAR);
    LOG.info("Loading bootstrap DB config from properties file " + propFile);
    _sBootstrapConfigProps = new Properties();
    FileInputStream f = new FileInputStream(propFile);
    try {
        _sBootstrapConfigProps.load(f);
    } finally {
        f.close();
    }
    if (cmd.hasOption(CMD_LINE_PROPS_OPT_CHAR)) {
        String cmdLinePropString = cmd.getOptionValue(CMD_LINE_PROPS_OPT_CHAR);
        updatePropsFromCmdLine(_sBootstrapConfigProps, cmdLinePropString);
    }
    int srcId = Integer.parseInt(cmd.getOptionValue(SOURCE_ID_OPT_CHAR));
    String srcName = cmd.getOptionValue(SOURCE_NAME_OPT_CHAR);
    BootstrapConfig config = new BootstrapConfig();
    ConfigLoader<BootstrapReadOnlyConfig> configLoader = new ConfigLoader<BootstrapReadOnlyConfig>("bootstrap.", config);
    BootstrapReadOnlyConfig staticConfig = configLoader.loadConfig(_sBootstrapConfigProps);
    Config cfg = new Config();
    cfg.setDbConfig(staticConfig);
    cfg.setSrcId(srcId);
    cfg.setSrcName(srcName);
    return cfg;
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) Options(org.apache.commons.cli.Options) ConfigLoader(com.linkedin.databus.core.util.ConfigLoader) BootstrapConfig(com.linkedin.databus.bootstrap.common.BootstrapConfig) BootstrapReadOnlyConfig(com.linkedin.databus.bootstrap.common.BootstrapReadOnlyConfig) PatternLayout(org.apache.log4j.PatternLayout) BootstrapConfig(com.linkedin.databus.bootstrap.common.BootstrapConfig) GnuParser(org.apache.commons.cli.GnuParser) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) BootstrapReadOnlyConfig(com.linkedin.databus.bootstrap.common.BootstrapReadOnlyConfig) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 27 with ConsoleAppender

use of org.apache.log4j.ConsoleAppender in project databus by linkedin.

the class TestUtil method setupLogging.

public static void setupLogging(boolean logToConsole, String fileLogPath, Level logLevel) {
    PatternLayout defaultLayout = new PatternLayout("%d{ISO8601} [%t] (%p) {%c{1}} %m%n");
    Logger.getRootLogger().removeAllAppenders();
    if (logToConsole) {
        ConsoleAppender defaultAppender = new ConsoleAppender(defaultLayout);
        Logger.getRootLogger().addAppender(defaultAppender);
    }
    if (null != fileLogPath && 0 < fileLogPath.length()) {
        File logFile = new File(fileLogPath);
        if (null != logFile.getParentFile() && !logFile.getParentFile().exists()) {
            if (!logFile.getParentFile().mkdirs()) {
                Logger.getRootLogger().error("unable to create parent directory for log file: " + logFile);
            }
        }
        FileAppender fileAppender = null;
        try {
            fileAppender = new FileAppender(defaultLayout, fileLogPath);
        } catch (IOException io) {
            Logger.getRootLogger().error(io);
        }
        if (null != fileAppender)
            Logger.getRootLogger().addAppender(fileAppender);
    }
    Logger.getRootLogger().setLevel(logLevel);
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) FileAppender(org.apache.log4j.FileAppender) PatternLayout(org.apache.log4j.PatternLayout) IOException(java.io.IOException) File(java.io.File)

Example 28 with ConsoleAppender

use of org.apache.log4j.ConsoleAppender in project databus by linkedin.

the class BaseCli method processCommandLineArgs.

/**
   * Parses the command line arguments
   * @param cliArgs		the command line arguments
   * @return true iff parsing was successful
   */
public boolean processCommandLineArgs(String[] cliArgs) {
    constructCommandLineOptions();
    CommandLineParser cliParser = new GnuParser();
    _cmd = null;
    try {
        _cmd = cliParser.parse(_cliOptions, cliArgs);
    } catch (ParseException pe) {
        printError("failed to parse command-line options: " + pe.toString(), true);
        return false;
    }
    if (_cmd.hasOption(HELP_OPT_CHAR)) {
        printCliHelp();
        return false;
    }
    int verbosityInc = 0;
    if (_cmd.hasOption(VERBOSE3_OPT_LONG_NAME)) {
        //-vvv is always Level.ALL
        verbosityInc = VERBOSITIES.length;
    } else if (_cmd.hasOption(VERBOSE2_OPT_LONG_NAME)) {
        verbosityInc = 2;
    } else if (_cmd.hasOption(VERBOSE1_OPT_LONG_NAME)) {
        verbosityInc = 1;
    }
    Level effectiveLevel = VERBOSITIES[Math.min(_defaultLogLevelIdx + verbosityInc, VERBOSITIES.length - 1)];
    if (_cmd.hasOption(QUIET_OPT_CHAR)) {
        effectiveLevel = Level.OFF;
    }
    Logger.getRootLogger().setLevel(effectiveLevel);
    if (_cmd.hasOption(LOG4J_PROPS_OPT_CHAR)) {
        String log4jPropFile = _cmd.getOptionValue(LOG4J_PROPS_OPT_CHAR);
        PropertyConfigurator.configure(log4jPropFile);
        _log.debug("Using custom logging settings from file " + log4jPropFile);
    } else {
        PatternLayout defaultLayout = new PatternLayout("%d{ISO8601} +%r [%t] (%p) {%c{1}} %m%n");
        ConsoleAppender defaultAppender = new ConsoleAppender(defaultLayout);
        Logger.getRootLogger().removeAllAppenders();
        Logger.getRootLogger().addAppender(defaultAppender);
        _log.debug("Using default logging settings");
    }
    processProperties();
    return true;
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) PatternLayout(org.apache.log4j.PatternLayout) GnuParser(org.apache.commons.cli.GnuParser) Level(org.apache.log4j.Level) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 29 with ConsoleAppender

use of org.apache.log4j.ConsoleAppender in project cpsolver by UniTime.

the class Test method setupLogging.

/**
     * Setup log4j logging
     * 
     * @param logFile
     *            log file
     * @param debug
     *            true if debug messages should be logged (use -Ddebug=true to
     *            enable debug message)
     */
public static void setupLogging(File logFile, boolean debug) {
    Logger root = Logger.getRootLogger();
    ConsoleAppender console = new ConsoleAppender(new PatternLayout("[%t] %m%n"));
    console.setThreshold(Level.INFO);
    root.addAppender(console);
    if (logFile != null) {
        try {
            FileAppender file = new FileAppender(new PatternLayout("%d{dd-MMM-yy HH:mm:ss.SSS} [%t] %-5p %c{2}> %m%n"), logFile.getPath(), false);
            file.setThreshold(Level.DEBUG);
            root.addAppender(file);
        } catch (IOException e) {
            sLog.fatal("Unable to configure logging, reason: " + e.getMessage(), e);
        }
    }
    if (!debug)
        root.setLevel(Level.INFO);
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) FileAppender(org.apache.log4j.FileAppender) PatternLayout(org.apache.log4j.PatternLayout) IOException(java.io.IOException) Logger(org.apache.log4j.Logger)

Example 30 with ConsoleAppender

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

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