/**
   * Converts a standard or custom priority level to a Level object.
   *
   * <p>If <code>value</code> is of form "level#classname", then the specified class' toLevel method
   * is called to process the specified level string; if no '#' character is present, then the
   * default {@link org.apache.log4j.Level} class is used to process the level value.
   *
   * <p>As a special case, if the <code>value</code> parameter is equal to the string "NULL", then
   * the value <code>null</code> will be returned.
   *
   * <p>If any error occurs while converting the value to a level, the <code>defaultValue</code>
   * parameter, which may be <code>null</code>, is returned.
   *
   * <p>Case of <code>value</code> is insignificant for the level level, but is significant for the
   * class name part, if present.
   *
   * @since 1.1
   */
  public static Level toLevel(String value, Level defaultValue) {
    if (value == null) return defaultValue;

    value = value.trim();

    int hashIndex = value.indexOf('#');
    if (hashIndex == -1) {
      if ("NULL".equalsIgnoreCase(value)) {
        return null;
      } else {
        // no class name specified : use standard Level class
        return (Level) Level.toLevel(value, defaultValue);
      }
    }

    Level result = defaultValue;

    String clazz = value.substring(hashIndex + 1);
    String levelName = value.substring(0, hashIndex);

    // This is degenerate case but you never know.
    if ("NULL".equalsIgnoreCase(levelName)) {
      return null;
    }

    LogLog.debug("toLevel" + ":class=[" + clazz + "]" + ":pri=[" + levelName + "]");

    try {
      Class customLevel = Loader.loadClass(clazz);

      // get a ref to the specified class' static method
      // toLevel(String, org.apache.log4j.Level)
      Class[] paramTypes = new Class[] {String.class, org.apache.log4j.Level.class};
      java.lang.reflect.Method toLevelMethod = customLevel.getMethod("toLevel", paramTypes);

      // now call the toLevel method, passing level string + default
      Object[] params = new Object[] {levelName, defaultValue};
      Object o = toLevelMethod.invoke(null, params);

      result = (Level) o;
    } catch (ClassNotFoundException e) {
      LogLog.warn("custom level class [" + clazz + "] not found.");
    } catch (NoSuchMethodException e) {
      LogLog.warn(
          "custom level class ["
              + clazz
              + "]"
              + " does not have a constructor which takes one string parameter",
          e);
    } catch (java.lang.reflect.InvocationTargetException e) {
      LogLog.warn("custom level class [" + clazz + "]" + " could not be instantiated", e);
    } catch (ClassCastException e) {
      LogLog.warn("class [" + clazz + "] is not a subclass of org.apache.log4j.Level", e);
    } catch (IllegalAccessException e) {
      LogLog.warn("class [" + clazz + "] cannot be instantiated due to access restrictions", e);
    } catch (Exception e) {
      LogLog.warn("class [" + clazz + "], level [" + levelName + "] conversion failed.", e);
    }
    return result;
  }
 /**
  * Very similar to <code>System.getProperty</code> except that the {@link SecurityException} is
  * hidden.
  *
  * @param key The key to search for.
  * @param def The default value to return.
  * @return the string value of the system property, or the default value if there is no property
  *     with that key.
  * @since 1.1
  */
 public static String getSystemproperty(String key, String def) {
   try {
     return System.getProperty(key, def);
   } catch (Throwable e) { // MS-Java throws com.ms.security.SecurityExceptionEx
     LogLog.debug("Was not allowed to read system property \"" + key + "\".");
     return def;
   }
 }
Exemplo n.º 3
0
  /**
   * This method will search for <code>resource</code> in different places. The search order is as
   * follows:
   *
   * <ol>
   *   <p>
   *   <li>Search for <code>resource</code> using the thread context class loader under Java2. If
   *       that fails, search for <code>resource</code> using the class loader that loaded this
   *       class (<code>Loader</code>). Under JDK 1.1, only the the class loader that loaded this
   *       class (<code>Loader</code>) is used.
   *       <p>
   *   <li>Try one last time with <code>ClassLoader.getSystemResource(resource)</code>, that is is
   *       using the system class loader in JDK 1.2 and virtual machine's built-in class loader in
   *       JDK 1.1.
   * </ol>
   */
  public static URL getResource(String resource) {
    ClassLoader classLoader = null;
    URL url = null;

    try {
      if (!java1) {
        classLoader = getTCL();
        if (classLoader != null) {
          LogLog.debug(
              "Trying to find [" + resource + "] using context classloader " + classLoader + ".");
          url = classLoader.getResource(resource);
          if (url != null) {
            return url;
          }
        }
      }

      // We could not find resource. Ler us now try with the
      // classloader that loaded this class.
      classLoader = Loader.class.getClassLoader();
      if (classLoader != null) {
        LogLog.debug("Trying to find [" + resource + "] using " + classLoader + " class loader.");
        url = classLoader.getResource(resource);
        if (url != null) {
          return url;
        }
      }
    } catch (Throwable t) {
      LogLog.warn(TSTR, t);
    }

    // Last ditch attempt: get the resource from the class path. It
    // may be the case that clazz was loaded by the Extentsion class
    // loader which the parent of the system class loader. Hence the
    // code below.
    LogLog.debug("Trying to find [" + resource + "] using ClassLoader.getSystemResource().");
    return ClassLoader.getSystemResource(resource);
  }
  /**
   * Configure log4j given a URL.
   *
   * <p>The url must point to a file or resource which will be interpreted by a new instance of a
   * log4j configurator.
   *
   * <p>All configurations steps are taken on the <code>hierarchy</code> passed as a parameter.
   *
   * <p>
   *
   * @param url The location of the configuration file or resource.
   * @param clazz The classname, of the log4j configurator which will parse the file or resource at
   *     <code>url</code>. This must be a subclass of {@link Configurator}, or null. If this value
   *     is null then a default configurator of {@link PropertyConfigurator} is used, unless the
   *     filename pointed to by <code>url</code> ends in '.xml', in which case {@link
   *     org.apache.log4j.xml.DOMConfigurator} is used.
   * @param hierarchy The {@link org.apache.log4j.Hierarchy} to act on.
   * @since 1.1.4
   */
  public static void selectAndConfigure(URL url, String clazz, LoggerRepository hierarchy) {
    Configurator configurator = null;
    String filename = url.getFile();

    if (clazz == null && filename != null && filename.endsWith(".xml")) {
      clazz = "org.apache.log4j.xml.DOMConfigurator";
    }

    if (clazz != null) {
      LogLog.debug("Preferred configurator class: " + clazz);
      configurator = (Configurator) instantiateByClassName(clazz, Configurator.class, null);
      if (configurator == null) {
        LogLog.error("Could not instantiate configurator [" + clazz + "].");
        return;
      }
    } else {
      configurator = new PropertyConfigurator();
    }

    configurator.doConfigure(url, hierarchy);
  }
Exemplo n.º 5
0
  protected void checkAndConfigure() {
    boolean fileExists;
    try {
      fileExists = file.exists();
    } catch (SecurityException e) {
      LogLog.warn("Was not allowed to read check file existance, file:[" + filename + "].");
      interrupted = true; // there is no point in continuing
      return;
    }

    if (fileExists) {
      long l = file.lastModified(); // this can also throw a SecurityException
      if (l > lastModif) { // however, if we reached this point this
        lastModif = l; // is very unlikely.
        doOnChange();
        warnedAlready = false;
      }
    } else {
      if (!warnedAlready) {
        LogLog.debug("[" + filename + "] does not exist.");
        warnedAlready = true;
      }
    }
  }
Exemplo n.º 6
0
 void dump() {
   LogLog.debug("min=" + min + ", max=" + max + ", leftAlign=" + leftAlign);
 }