Esempio n. 1
0
 /**
  * Fixes the Chameleon logging configuration to write the logs in the logs/wisdom.log file instead
  * of chameleon.log file.
  *
  * @param basedir the base directory of the chameleon
  */
 private static void fixLoggingSystem(File basedir) {
   ILoggerFactory factory = LoggerFactory.getILoggerFactory();
   if (factory instanceof LoggerContext) {
     // We know that we are using logback from here.
     LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
     ch.qos.logback.classic.Logger logbackLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
     if (logbackLogger == null) {
       return;
     }
     Appender<ILoggingEvent> appender = logbackLogger.getAppender("FILE");
     if (appender instanceof RollingFileAppender) {
       RollingFileAppender<ILoggingEvent> fileAppender =
           (RollingFileAppender<ILoggingEvent>) appender;
       String file = new File(basedir, "logs/wisdom.log").getAbsolutePath();
       fileAppender.stop();
       // Remove the created log directory.
       // We do that afterwards because on Windows the file cannot be deleted while we still have a
       // logger
       // using it.
       FileUtils.deleteQuietly(new File("logs"));
       fileAppender.setFile(file);
       fileAppender.setContext(lc);
       fileAppender.start();
     }
   }
 }
 @Override
 protected void subAppend(E event) {
   if (!fileCreated) {
     super.start();
     fileCreated = true;
   }
   super.subAppend(event);
 }
Esempio n. 3
0
  protected Appender buildDailyMaxRollingAppender(
      String name, String fileName, String pattern, int maxBackupIndex) {
    RollingFileAppender appender = new RollingFileAppender();
    appender.setContext(loggerContext);
    appender.setName(name);
    appender.setAppend(true);
    appender.setFile(new File(getLogPath(), fileName).getAbsolutePath());

    TimeBasedRollingPolicy rolling = new TimeBasedRollingPolicy();
    rolling.setContext(loggerContext);
    rolling.setFileNamePattern(
        new File(getLogPath(), fileName).getAbsolutePath() + ".%d{yyyy-MM-dd-HH}");
    rolling.setMaxHistory(maxBackupIndex);
    rolling.setParent(appender);
    rolling.start();
    appender.setRollingPolicy(rolling);

    PatternLayout layout = new PatternLayout();
    layout.setContext(loggerContext);
    layout.setPattern(pattern);
    layout.start();
    appender.setLayout(layout);
    // 启动
    appender.start();
    return appender;
  }
Esempio n. 4
0
  private static void initLogging() {
    final File logDir =
        new File(
            System.getProperty(
                "user.home")); // new File("."); //getDir("log", Constants.TEST ?
                               // Context.MODE_WORLD_READABLE : MODE_PRIVATE);
    final File logFile = new File(logDir, "wowdoge.log");

    final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

    final PatternLayoutEncoder filePattern = new PatternLayoutEncoder();
    filePattern.setContext(context);
    filePattern.setPattern("%d{HH:mm:ss.SSS} [%thread] %logger{0} - %msg%n");
    filePattern.start();

    final RollingFileAppender<ILoggingEvent> fileAppender =
        new RollingFileAppender<ILoggingEvent>();
    fileAppender.setContext(context);
    fileAppender.setFile(logFile.getAbsolutePath());

    final TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy =
        new TimeBasedRollingPolicy<ILoggingEvent>();
    rollingPolicy.setContext(context);
    rollingPolicy.setParent(fileAppender);
    rollingPolicy.setFileNamePattern(logDir.getAbsolutePath() + "/wallet.%d.log.gz");
    rollingPolicy.setMaxHistory(7);
    rollingPolicy.start();

    fileAppender.setEncoder(filePattern);
    fileAppender.setRollingPolicy(rollingPolicy);
    fileAppender.start();

    final PatternLayoutEncoder logcatTagPattern = new PatternLayoutEncoder();
    logcatTagPattern.setContext(context);
    logcatTagPattern.setPattern("%logger{0}");
    logcatTagPattern.start();

    final PatternLayoutEncoder logcatPattern = new PatternLayoutEncoder();
    logcatPattern.setContext(context);
    logcatPattern.setPattern("[%thread] %msg%n");
    logcatPattern.start();

    // final LogcatAppender logcatAppender = new LogcatAppender();
    // logcatAppender.setContext(context);
    // logcatAppender.setTagEncoder(logcatTagPattern);
    // logcatAppender.setEncoder(logcatPattern);
    // logcatAppender.start();

    final ch.qos.logback.classic.Logger log = context.getLogger(Logger.ROOT_LOGGER_NAME);
    log.addAppender(fileAppender);
    // log.addAppender(logcatAppender);
    log.setLevel(Level.INFO); // Level.INFO);

    SysOutOverSLF4J.sendSystemOutAndErrToSLF4J();
  }
  private void configureFileLogger() {
    final File logFile = new File(logsDir, DEFAULT_LOG_FILENAME);
    final RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<>();
    fileAppender.setContext((LoggerContext) LoggerFactory.getILoggerFactory());
    fileAppender.setName(LOG_APPENDER);
    fileAppender.setFile(logFile.getAbsolutePath());

    fileAppender.setEncoder(createEncoder());
    fileAppender.setRollingPolicy(createRollingPolicy(fileAppender));
    fileAppender.setTriggeringPolicy(createTriggeringPolicy());
    fileAppender.start();

    final ch.qos.logback.classic.Logger logger =
        (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("ROOT");
    logger.addAppender(fileAppender);

    StatusPrinter.print((LoggerContext) LoggerFactory.getILoggerFactory());
  }
  private void initLogging() {
    // We can't log into the wallet specific directories because
    // logging is initialized well before we've selected one.

    final File logDir = getDir("log", MODE_PRIVATE); // Context.MODE_WORLD_READABLE
    final File logFile = new File(logDir, "wallet32.log");

    final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

    final PatternLayoutEncoder filePattern = new PatternLayoutEncoder();
    filePattern.setContext(context);
    filePattern.setPattern("%d{HH:mm:ss.SSS} [%thread] %logger{0} - %msg%n");
    filePattern.start();

    final RollingFileAppender<ILoggingEvent> fileAppender =
        new RollingFileAppender<ILoggingEvent>();
    fileAppender.setContext(context);
    fileAppender.setFile(logFile.getAbsolutePath());

    final TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy =
        new TimeBasedRollingPolicy<ILoggingEvent>();
    rollingPolicy.setContext(context);
    rollingPolicy.setParent(fileAppender);
    rollingPolicy.setFileNamePattern(logDir.getAbsolutePath() + "/wallet32.%d.log.gz");
    rollingPolicy.setMaxHistory(7);
    rollingPolicy.start();

    fileAppender.setEncoder(filePattern);
    fileAppender.setRollingPolicy(rollingPolicy);
    fileAppender.start();

    final PatternLayoutEncoder logcatTagPattern = new PatternLayoutEncoder();
    logcatTagPattern.setContext(context);
    logcatTagPattern.setPattern("%logger{0}");
    logcatTagPattern.start();

    final PatternLayoutEncoder logcatPattern = new PatternLayoutEncoder();
    logcatPattern.setContext(context);
    logcatPattern.setPattern("[%thread] %msg%n");
    logcatPattern.start();

    final LogcatAppender logcatAppender = new LogcatAppender();
    logcatAppender.setContext(context);
    logcatAppender.setTagEncoder(logcatTagPattern);
    logcatAppender.setEncoder(logcatPattern);
    logcatAppender.start();

    final ch.qos.logback.classic.Logger log = context.getLogger(Logger.ROOT_LOGGER_NAME);
    log.addAppender(fileAppender);
    log.addAppender(logcatAppender);
    log.setLevel(Level.INFO);
  }
Esempio n. 7
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();
    }
  }
  public static void main(String[] args) throws Exception {
    // See : http://patorjk.com/software/taag/#p=display&f=Slant&t=Flamingo%20Collector
    System.out.println(
        "   _  __              ____                  _      _             _                _____                          \n"
            + "  | |/ /__  ____     / __ \\_________ _   __(_)____(_)___  ____  (_)___  ____ _   / ___/___  ______   _____  _____\n"
            + "  |   / _ \\/ __ \\   / /_/ / ___/ __ \\ | / / / ___/ / __ \\/ __ \\/ / __ \\/ __ `/   \\__ \\/ _ \\/ ___/ | / / _ \\/ ___/\n"
            + " /   /  __/ / / /  / ____/ /  / /_/ / |/ / (__  ) / /_/ / / / / / / / / /_/ /   ___/ /  __/ /   | |/ /  __/ /    \n"
            + "/_/|_\\___/_/ /_/  /_/   /_/   \\____/|___/_/____/_/\\____/_/ /_/_/_/ /_/\\__, /   /____/\\___/_/    |___/\\___/_/     \n"
            + "                                                                     /____/                                      \n");

    ////////////////////////////////////////////////////////////////////////////////////

    StringBuilder builder = new StringBuilder();
    printHeader(builder, "Application Information");

    Properties appProps = new Properties();
    Properties systemProperties = System.getProperties();
    appProps.put(
        "Java Version",
        systemProperties.getProperty("java.version", UNKNOWN)
            + " - "
            + systemProperties.getProperty("java.vendor", UNKNOWN));
    appProps.put("Current Working Directory", systemProperties.getProperty("user.dir", UNKNOWN));

    print(builder, appProps);

    Properties memPros = new Properties();
    final Runtime rt = Runtime.getRuntime();
    final long maxMemory = rt.maxMemory() / MEGA_BYTES;
    final long totalMemory = rt.totalMemory() / MEGA_BYTES;
    final long freeMemory = rt.freeMemory() / MEGA_BYTES;
    final long usedMemory = totalMemory - freeMemory;

    memPros.put("Maximum Allowable Memory", maxMemory + "MB");
    memPros.put("Total Memory", totalMemory + "MB");
    memPros.put("Free Memory", freeMemory + "MB");
    memPros.put("Used Memory", usedMemory + "MB");

    print(builder, memPros);

    printHeader(builder, "Java System Properties");
    Properties sysProps = new Properties();
    for (final Map.Entry<Object, Object> entry : systemProperties.entrySet()) {
      sysProps.put(entry.getKey(), entry.getValue());
    }

    print(builder, sysProps);

    printHeader(builder, "System Environments");
    Map<String, String> getenv = System.getenv();
    Properties envProps = new Properties();
    Set<String> strings = getenv.keySet();
    for (String key : strings) {
      String message = getenv.get(key);
      envProps.put(key, message);
    }

    print(builder, envProps);

    System.out.println(builder.toString());

    ////////////////////////////////////////////////////////////////////////////////////

    NativeLoader.loadSigarNative();

    ////////////////////////////////////////////////////////////////////////////////////

    SpringApplication app = new SpringApplication(Application.class);
    app.setShowBanner(false);
    ApplicationContext ctx = app.run(args);

    try {
      LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
      for (Logger LOGGER : context.getLoggerList()) {
        if (LOGGER instanceof ch.qos.logback.classic.Logger) {
          ch.qos.logback.classic.Logger logbackLogger = (ch.qos.logback.classic.Logger) LOGGER;
          for (Iterator<Appender<ILoggingEvent>> index = logbackLogger.iteratorForAppenders();
              index.hasNext(); ) {
            Appender<ILoggingEvent> appender = index.next();
            if ("FILE".equals(appender.getName())
                && appender instanceof ch.qos.logback.core.rolling.RollingFileAppender) {
              ch.qos.logback.core.rolling.RollingFileAppender logbackAppender =
                  (ch.qos.logback.core.rolling.RollingFileAppender) appender;
              logger.info("Log file is {}", logbackAppender.getFile());
            }
          }
        }
      }
    } catch (Exception ex) {
    }
  }