Example #1
0
  public static void main(final String[] args) {

    try {
      final Arguments arguments = new Arguments(args);

      // turn root log wide open, filters will be set to argument levels
      configureLogging(arguments.getConsoleLogLevel(), arguments.getFileLogLevel());

      LOG.info("Version: {}", arguments.getVersion());
      LOG.info("Command line args: {}", Joiner.on(", ").join(args));
      LOG.info("Console log level: {}", arguments.getConsoleLogLevel().toString());
      LOG.info("Log file log level: {}", arguments.getFileLogLevel().toString());
      LOG.info(CliCommand.getPlatformInformation());
      LOG.info(arguments.getArgumentLog());

      if (arguments.isHelp()) {
        // no need to connect to vend help
        final Ds3Cli runner = new Ds3Cli(null, arguments, null);
        final CommandResponse response = runner.getCommandHelp();
        System.out.println(response.getMessage());
        System.exit(response.getReturnCode());
      }

      final Ds3Client client = createClient(arguments);
      if (!Utils.isVersionSupported(client)) {
        System.out.println(
            String.format(
                "ERROR: Minimum Black Pearl supported is %s", Utils.MINIMUM_VERSION_SUPPORTED));
        System.exit(2);
      }

      final Ds3Provider provider = new Ds3ProviderImpl(client, Ds3ClientHelpers.wrap(client));
      final FileUtils fileUtils = new FileUtilsImpl();

      final Ds3Cli runner = new Ds3Cli(provider, arguments, fileUtils);
      final CommandResponse response = runner.call();
      System.out.println(response.getMessage());
      System.exit(response.getReturnCode());
    } catch (final FailedRequestException e) {
      System.out.println("ERROR: " + e.getMessage());
      LOG.info("Stack trace: ", e);
      LOG.info("Printing out the response from the server:");
      LOG.info(e.getResponseString());
      System.exit(2);
    } catch (final Exception e) {
      System.out.println("ERROR: " + e.getMessage());
      LOG.info("Stack trace: ", e);
      System.exit(2);
    }
  }
Example #2
0
  private static void configureLogging(final Level consoleLevel, final Level fileLevel) {

    final LoggerContext loggerContext = LOG.getLoggerContext();
    loggerContext.reset();

    // set root log to the most permissive filter (affects performance big time (JAVACLI-90))
    final int lowestLogLevel = Math.min(consoleLevel.toInt(), fileLevel.toInt());
    LOG.setLevel(Level.toLevel(lowestLogLevel));

    if (!consoleLevel.equals(Level.OFF)) {
      // create and add console appender
      final PatternLayoutEncoder consoleEncoder = new PatternLayoutEncoder();
      consoleEncoder.setContext(loggerContext);
      consoleEncoder.setPattern(LOG_FORMAT_PATTERN);
      consoleEncoder.start();

      final ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<ILoggingEvent>();
      consoleAppender.setContext(loggerContext);
      consoleAppender.setName("STDOUT");
      consoleAppender.setEncoder(consoleEncoder);

      final ThresholdFilter consoleFilter = new ThresholdFilter();
      consoleFilter.setLevel(consoleLevel.levelStr);
      consoleFilter.setName(consoleLevel.levelStr);
      consoleFilter.start();
      consoleAppender.addFilter(consoleFilter);

      consoleAppender.start();
      LOG.addAppender(consoleAppender);
    }

    if (!fileLevel.equals(Level.OFF)) {
      // create file appender only if needed.
      // if done in the xml, it will create an empty file
      final RollingFileAppender<ILoggingEvent> fileAppender =
          new RollingFileAppender<ILoggingEvent>();
      final FixedWindowRollingPolicy sizeBasedRollingPolicy = new FixedWindowRollingPolicy();
      final SizeBasedTriggeringPolicy<Object> sizeBasedTriggeringPolicy =
          new SizeBasedTriggeringPolicy<Object>();

      fileAppender.setContext(loggerContext);
      sizeBasedTriggeringPolicy.setContext(loggerContext);
      sizeBasedRollingPolicy.setContext(loggerContext);
      fileAppender.setRollingPolicy(sizeBasedRollingPolicy);
      sizeBasedRollingPolicy.setParent(fileAppender);
      sizeBasedRollingPolicy.setMinIndex(0);
      sizeBasedRollingPolicy.setMaxIndex(99);

      final Path logFilePath = FileSystems.getDefault().getPath(LOG_DIR, LOG_FILE_NAME);
      fileAppender.setFile(logFilePath.toString());
      sizeBasedRollingPolicy.setFileNamePattern(LOG_DIR + LOG_ARCHIVE_FILE_PATTERN);
      sizeBasedRollingPolicy.start();

      sizeBasedTriggeringPolicy.setMaxFileSize("10MB");
      sizeBasedTriggeringPolicy.start();

      final PatternLayoutEncoder fileEncoder = new PatternLayoutEncoder();
      fileEncoder.setContext(loggerContext);
      fileEncoder.setPattern(LOG_FORMAT_PATTERN);
      fileEncoder.start();

      fileAppender.setTriggeringPolicy((TriggeringPolicy) sizeBasedTriggeringPolicy);
      fileAppender.setRollingPolicy(sizeBasedRollingPolicy);
      fileAppender.setEncoder(fileEncoder);
      fileAppender.setName("LOGFILE");
      sizeBasedRollingPolicy.start();

      final ThresholdFilter fileFilter = new ThresholdFilter();
      fileFilter.setLevel(fileLevel.levelStr);
      fileFilter.setName(fileLevel.levelStr);
      fileFilter.start();
      fileAppender.addFilter(fileFilter);

      LOG.addAppender((Appender) fileAppender);
      fileAppender.start();
    }
  }