Exemplo n.º 1
0
 public static Logger getLogger(Parameters params, String name)
     throws com.grey.base.ConfigException, java.io.IOException {
   if (params == null) params = new Parameters();
   params.reconcile();
   Logger log = null;
   try {
     Class<?> clss = DynLoader.loadClass(params.logclass);
     java.lang.reflect.Constructor<?> ctor =
         clss.getDeclaredConstructor(Parameters.class, String.class);
     ctor.setAccessible(true);
     log = Logger.class.cast(ctor.newInstance(params, name));
   } catch (Throwable ex) {
     throw new com.grey.base.ConfigException(
         ex,
         "Failed to create logger="
             + params.logclass
             + " - "
             + com.grey.base.GreyException.summary(ex));
   }
   log.init();
   if (diagtrace) System.out.println("GreyLogger: Created Logger - " + log);
   return log;
 }
Exemplo n.º 2
0
  private static Logger getNamedLogger(String cfgpath, String name, String alias)
      throws com.grey.base.ConfigException, java.io.IOException {
    String tag = (alias == null ? name : (alias + "/" + name));
    if (name == null || name.length() == 0) name = DFLT_LOGNAME;
    String xpath = "/loggers/logger[@name='" + name + "']" + XmlConfig.XPATH_ENABLED;
    java.io.File fh = new java.io.File(cfgpath);
    java.net.URL url = null;
    XmlConfig cfg = null;

    if (fh.exists()) {
      cfg = XmlConfig.getSection(cfgpath, xpath);
    } else {
      url = DynLoader.getLoaderResource(cfgpath, Factory.class.getClassLoader());
      if (url != null) {
        cfgpath = url.toString();
        String xmltxt = FileOps.readAsText(url, null);
        cfg = XmlConfig.makeSection(xmltxt, xpath);
      }
    }
    if (url == null) cfgpath = fh.getCanonicalPath();
    Parameters params = null;

    if (cfg != null) {
      alias = cfg.getValue("alias", false, null);
      if (name.equals(alias))
        throw new com.grey.base.ConfigException(
            "GreyLogger: Infinite loop between " + name + " and " + alias + " - " + cfgpath);
      if (alias != null && alias.length() != 0) return getNamedLogger(cfgpath, alias, tag);
      cfg = cfg.getSection("file");
    }
    if ((cfg == null || !cfg.exists()) && !name.equalsIgnoreCase(DFLT_LOGNAME)) {
      if (sinkstdio) {
        if (diagtrace)
          System.out.println(
              "GreyLogger: Logger=" + tag + " has been directed to stdio - " + cfgpath);
        params = new Parameters();
      } else {
        if (diagtrace)
          System.out.println(
              "GreyLogger: Logger=" + tag + " has been directed to Sink - " + cfgpath);
        return new SinkLogger(name);
      }
    }
    if (params == null) params = new Parameters(cfg);
    params.reconcile();
    String key = params.pthnam + ":" + params.strm;
    Logger log = null;

    synchronized (loggers) {
      log = loggers.get(key);
      if (log != null) {
        if (diagtrace)
          System.out.println(
              "GreyLogger: Logger="
                  + tag
                  + " has been directed to existing Logger "
                  + log
                  + " - "
                  + cfgpath);
        return log;
      }
      log = getLogger(params, name);
      loggers.put(key, log);
      if (diagtrace)
        System.out.println("GreyLogger: Named Logger=" + tag + " yielded " + log + " - " + cfgpath);
    }
    return log;
  }