Example #1
0
 public static void logInit(boolean force) throws IOException {
   if (!IsLogInitialized || force) {
     LogManager.getLogManager()
         .readConfiguration(
             LibRt.class.getClassLoader().getResourceAsStream("cfg.logging.properties"));
     String logFile = System.getProperty("CfgLog");
     if (logFile != null) {
       LogManager.getLogManager().readConfiguration(new FileInputStream(logFile));
     }
     try {
       LogLvlMax = Integer.parseInt(System.getProperty("LogLvlMax", LogLvlMax + ""));
     } catch (Exception ex) {
     }
     ; // XXX: mostrar mensaje?
   }
 }
Example #2
0
  private void initLogger() {
    ConsoleHandler consoleHandler = null;

    Logger rootLogger = LogManager.getLogManager().getLogger("");
    Handler[] handlers = rootLogger.getHandlers();
    for (Handler handler : handlers) {
      if (handler instanceof ConsoleHandler) {
        consoleHandler = (ConsoleHandler) handler;
        rootLogger.removeHandler(handler);
      }
    }

    logger = Logger.getLogger(contextId);
    logger.setLevel(logLevel);
    if (!logLevel.equals(Level.OFF)) {
      LogFormatter formatter = new LogFormatter();
      if (consoleLog) {
        if (consoleHandler == null) {
          consoleHandler = new ConsoleHandler();
        }
        consoleHandler.setFormatter(formatter);
        consoleHandler.setLevel(logLevel);
        logger.addHandler(consoleHandler);
      }
      String userHomePath = getProperty("user.home", ".");
      File logDir = new File(userHomePath, '.' + contextId + "/log");
      logDir.mkdirs();
      String logFilePattern = new File(logDir, contextId + "-%g.log").getPath();
      try {
        FileHandler fileHandler = new FileHandler(logFilePattern);
        fileHandler.setFormatter(formatter);
        fileHandler.setLevel(logLevel);
        logger.addHandler(fileHandler);
      } catch (IOException e) {
        System.err.println("Error: Failed to create log file: " + logFilePattern);
      }
    }
  }
Example #3
0
 /**
  * The instance is stored in a place directly accessible through the Java SE API, so that it can
  * be recovered from any class loader.
  */
 protected MockingBridge(Class<? extends MockingBridge> subclass) {
   super("mockit." + subclass.hashCode(), null);
   LogManager.getLogManager().addLogger(this);
 }
public class ParentLoggersTest {
  static final LogManager logMgr = LogManager.getLogManager();
  static final PrintStream out = System.out;

  static final boolean PASSED = true;
  static final boolean FAILED = false;
  static final String MSG_PASSED = "ParentLoggersTest: passed";
  static final String MSG_FAILED = "ParentLoggersTest: failed";

  /* Properties */
  static final String TST_SRC_PROP = "test.src";
  static final String CFG_FILE_PROP = "java.util.logging.config.file";
  static final String LM_PROP_FNAME = "ParentLoggersTest.props";

  /* Logger names */
  static final String PARENT_NAME_1 = "myParentLogger";
  static final String PARENT_NAME_2 = "abc.xyz.foo";
  static final String LOGGER_NAME_1 = PARENT_NAME_1 + ".myLogger";
  static final String LOGGER_NAME_2 = PARENT_NAME_2 + ".myBar.myLogger";

  static final List<String> initialLoggerNames = new ArrayList<String>();

  public static void main(String args[]) throws Exception {
    // cache the initial set of loggers before this test begins
    // to add any loggers
    Enumeration<String> e = logMgr.getLoggerNames();
    List<String> defaultLoggers = getDefaultLoggerNames();
    while (e.hasMoreElements()) {
      String logger = e.nextElement();
      if (!defaultLoggers.contains(logger)) {
        initialLoggerNames.add(logger);
      }
    }
    ;

    String tstSrc = System.getProperty(TST_SRC_PROP);
    File fname = new File(tstSrc, LM_PROP_FNAME);
    String prop = fname.getCanonicalPath();
    System.setProperty(CFG_FILE_PROP, prop);
    logMgr.readConfiguration();

    System.out.println();
    if (checkLoggers() == PASSED) {
      System.out.println(MSG_PASSED);
    } else {
      System.out.println(MSG_FAILED);
      throw new Exception(MSG_FAILED);
    }
  }

  public static List<String> getDefaultLoggerNames() {
    List<String> expectedLoggerNames = new ArrayList<String>();

    // LogManager always creates two loggers:
    expectedLoggerNames.add(""); // root   logger: ""
    expectedLoggerNames.add("global"); // global logger: "global"
    return expectedLoggerNames;
  }

  /* Check: getLoggerNames() must return correct names
   *        for registered loggers and their parents.
   * Returns boolean values: PASSED or FAILED
   */
  public static boolean checkLoggers() {
    String failMsg = "# checkLoggers: getLoggerNames() returned unexpected loggers";
    Vector<String> expectedLoggerNames = new Vector<String>(getDefaultLoggerNames());

    // Create the logger LOGGER_NAME_1
    Logger.getLogger(LOGGER_NAME_1);
    expectedLoggerNames.addElement(PARENT_NAME_1);
    expectedLoggerNames.addElement(LOGGER_NAME_1);

    // Create the logger LOGGER_NAME_2
    Logger.getLogger(LOGGER_NAME_2);
    expectedLoggerNames.addElement(PARENT_NAME_2);
    expectedLoggerNames.addElement(LOGGER_NAME_2);

    Enumeration<String> returnedLoggersEnum = logMgr.getLoggerNames();
    Vector<String> returnedLoggerNames = new Vector<String>(0);
    while (returnedLoggersEnum.hasMoreElements()) {
      String logger = returnedLoggersEnum.nextElement();
      if (!initialLoggerNames.contains(logger)) {
        // filter out the loggers that have been added before this test runs
        returnedLoggerNames.addElement(logger);
      }
    }
    ;

    return checkNames(expectedLoggerNames, returnedLoggerNames, failMsg);
  }

  // Returns boolean values: PASSED or FAILED
  private static boolean checkNames(
      Vector<String> expNames, Vector<String> retNames, String failMsg) {
    boolean status = PASSED;

    if (expNames.size() != retNames.size()) {
      status = FAILED;
    } else {
      boolean checked[] = new boolean[retNames.size()];
      for (int i = 0; i < expNames.size(); i++) {
        int j = 0;
        for (; j < retNames.size(); j++) {
          if (!checked[j] && expNames.elementAt(i).equals(retNames.elementAt(j))) {
            checked[j] = true;
            break;
          }
        }
        if (j >= retNames.size()) {
          status = FAILED;
          break;
        }
      }
    }
    if (!status) {
      printFailMsg(expNames, retNames, failMsg);
    }
    return status;
  }

  private static void printFailMsg(
      Vector<String> expNames, Vector<String> retNames, String failMsg) {
    out.println();
    out.println(failMsg);
    if (expNames.size() == 0) {
      out.println("# there are NO expected logger names");
    } else {
      out.println("# expected logger names (" + expNames.size() + "):");
      for (int i = 0; i < expNames.size(); i++) {
        out.println(" expNames[" + i + "] = " + expNames.elementAt(i));
      }
    }
    if (retNames.size() == 0) {
      out.println("# there are NO returned logger names");
    } else {
      out.println("# returned logger names (" + retNames.size() + "):");
      for (int i = 0; i < retNames.size(); i++) {
        out.println("  retNames[" + i + "] = " + retNames.elementAt(i));
      }
    }
  }
}