コード例 #1
0
  protected static void setupSidlogger(Level lvl) {
    if (!Level.OFF.equals(lvl)) {
      Filter bslf = new BoshSidLoggerFilter();

      Logger BoshConnectionManagerLogger = Logger.getLogger(BoshConnectionManager.class.getName());
      Logger BoshSessionLogger = Logger.getLogger(BoshSession.class.getName());

      if (BoshConnectionManagerLogger.getLevel() == null
          || BoshSessionLogger.getLevel() == null
          || BoshConnectionManagerLogger.getLevel().intValue() < lvl.intValue()) {
        BoshConnectionManagerLogger.setLevel(lvl);
        BoshConnectionManagerLogger.setFilter(bslf);
        BoshSessionLogger.setLevel(lvl);
        BoshSessionLogger.setFilter(bslf);

        BoshConnectionManagerLogger.getParent().setFilter(bslf);
      }

      try {
        if (null == sidFilehandler) {
          sidFilehandler = new FileHandler("logs/bosh_sid.log", 10000000, 5, false);
          sidFilehandler.setLevel(lvl);
          sidFilehandler.setFilter(bslf);
          BoshConnectionManagerLogger.getParent().addHandler(sidFilehandler);
        }
      } catch (IOException ex) {
        log.log(Level.CONFIG, "Error creating BOSH SID logger" + ex);
      }
    }
  }
コード例 #2
0
ファイル: RootLogConfig.java プロジェクト: nsanch/commons
 private static void setConsoleHandler(Logger logger, boolean removeOtherHandlers) {
   Handler consoleHandler = null;
   for (Handler h : logger.getHandlers()) {
     if (h instanceof ConsoleHandler) {
       consoleHandler = h;
     } else if (removeOtherHandlers) {
       logger.removeHandler(h);
     }
   }
   if (consoleHandler == null) {
     consoleHandler = new ConsoleHandler();
     logger.addHandler(new ConsoleHandler());
   }
   consoleHandler.setLevel(Level.ALL);
   consoleHandler.setFilter(null);
 }
コード例 #3
0
ファイル: JDKLogger.java プロジェクト: ZhernakovMikhail/fax4j
  /**
   * This function initializes and returns the logger.
   *
   * @return The logger
   */
  protected static final Logger initializeLogger() {
    // get logger
    Logger logger = Logger.getLogger(FaxClient.class.getName());

    // enable all log events (fax4j logger filters out uneeded log events)
    logger.setLevel(Level.ALL);
    logger.setFilter(null);

    // enable to pass log events to parent loggers
    logger.setUseParentHandlers(true);

    // create handler
    Formatter formatter = new SimpleFormatter();
    Handler handler = new StreamHandler(System.out, formatter);

    // set filtering
    handler.setLevel(logger.getLevel());
    handler.setFilter(logger.getFilter());

    // add handler
    logger.addHandler(handler);

    return logger;
  }
コード例 #4
0
  private Handler configureHandler(final Properties properties, final String handlerName)
      throws IOException {
    if (configuredHandlers.containsKey(handlerName)) {
      return configuredHandlers.get(handlerName);
    }

    // Get handler class name, instantiate it
    final String handlerClassName = getStringProperty(properties, getKey("handler", handlerName));
    if (handlerClassName == null) {
      throw new IllegalArgumentException("Handler " + handlerName + " has no class name");
    }
    final Handler handler;
    try {
      handler = (Handler) Class.forName(handlerClassName).getConstructor().newInstance();
    } catch (Exception e) {
      throw new IllegalArgumentException(
          "Handler " + handlerName + " could not be instantiated", e);
    }
    configuredHandlers.put(handlerName, handler);

    // Get handler level
    final String levelName = getStringProperty(properties, getKey("handler", handlerName, "level"));
    if (levelName != null) {
      try {
        handler.setLevel(LogContext.getSystemLogContext().getLevelForName(levelName));
      } catch (IllegalArgumentException e) {
        System.err.printf("Failed to set level %s on %s: %s\n", levelName, handler, e.getMessage());
      }
    }

    // Get handler encoding
    final String encodingName =
        getStringProperty(properties, getKey("handler", handlerName, "encoding"));
    if (encodingName != null) {
      try {
        handler.setEncoding(encodingName);
      } catch (UnsupportedEncodingException e) {
        System.err.printf(
            "Failed to set encoding %s on %s: %s\n", encodingName, handler, e.getMessage());
      }
    }

    // Get error handler
    final String errorManagerName =
        getStringProperty(properties, getKey("handler", handlerName, "errorManager"));
    if (errorManagerName != null) {
      try {
        handler.setErrorManager(configureErrorManager(properties, errorManagerName));
      } catch (IllegalArgumentException e) {
        System.err.printf(
            "Failed to set error manager %s on %s: %s\n",
            errorManagerName, handler, e.getMessage());
      }
    }

    // Get filter
    final String filterName =
        getStringProperty(properties, getKey("handler", handlerName, "filter"));
    if (filterName != null) {
      try {
        handler.setFilter(configureFilter(properties, filterName));
      } catch (IllegalArgumentException e) {
        System.err.printf(
            "Failed to set filter %s on %s: %s\n", filterName, handler, e.getMessage());
      }
    }

    // Get formatter
    final String formatterName =
        getStringProperty(properties, getKey("handler", handlerName, "formatter"));
    if (formatterName != null) {
      try {
        handler.setFormatter(configureFormatter(properties, formatterName));
      } catch (IllegalArgumentException e) {
        System.err.printf(
            "Failed to set formatter %s on %s: %s\n", filterName, handler, e.getMessage());
      }
    }

    // Get properties
    configureProperties(properties, handler, getKey("handler", handlerName));

    return handler;
  }
コード例 #5
0
ファイル: RootLogger.java プロジェクト: daniel-busquets/ET21
  public static void setup(LoggerConfig config) {
    Logger log = config.getRootLogger();

    DefaultFormatter.setAppId(config.getLogFileName());

    try {
      File logDir = Directories.getLogDir();
      if (!logDir.exists()) {
        logDir.mkdirs();
      }

      FileHandler fileHandler =
          new FileHandler(
              config.getLogFilePattern(),
              config.getMaxBytes(),
              config.getMaxFiles(),
              config.doAppend());
      LogManager manager = LogManager.getLogManager();

      // retrieve the Level-setting from the property file for the FileHandler
      Level level = Level.ALL;
      String val = manager.getProperty(FileHandler.class.getName() + ".level");
      if (val != null) {
        try {
          level = Level.parse(val.trim());
        } catch (Exception ex) {
        }
      }
      fileHandler.setLevel(level);
      log.addHandler(fileHandler);
    } catch (Exception e) {
      System.err.println(
          TextUtils.merge(
              "Unable to setup log file for ''{0}''. Reason: {1}",
              config.getLogFileName(), e.getLocalizedMessage()));
    }

    for (Handler handler : log.getHandlers()) {
      handler.setFormatter(config.getFormatter());

      // ugly hack: don't display this error message in the console until we
      // get feedback from tomcat-users mailing list,
      // see http://mail-archives.apache.org/mod_mbox/tomcat-users/201104.mbox/browser
      if (handler instanceof ConsoleHandler) {
        handler.setFilter(
            new Filter() {

              @Override
              public boolean isLoggable(LogRecord record) {
                if (record.getMessage().equals("persistentManager.swapInInvalid")) {
                  return false;
                }

                return true;
              }
            });
      }
    }

    // report this even when in WARN-loglevel
    Logger initLOGGER = Logger.getLogger("Init");
    Level level = initLOGGER.getLevel();
    try {
      initLOGGER.setLevel(Level.INFO);
      initLOGGER.info(
          "-----------------------------------------------------------------------------");
      initLOGGER.info(
          TextUtils.merge(
              "easyTravel Demo Application - Copyright (C) 2010-{0,date,yyyy} dynaTrace software GmbH",
              new Date()));
      initLOGGER.info(
          "-----------------------------------------------------------------------------");

      Version version = Version.read();
      initLOGGER.info("Version: " + version.toString());
      initLOGGER.info("Build Date: " + version.getBuilddate());
      initLOGGER.info("Platform: " + getPlatformDescription());
    } finally {
      // restore previous level
      initLOGGER.setLevel(level);
    }
  }