是否可以在GWT中添加额外的loglevel

时间:2015-10-14 09:54:29

标签: gwt

在普通的Java中,添加自己的自定义日志级别非常容易。 但是我知道的所有Java方法都不能在GWT中工作,因为GWT有自己的java.util.logging.Level实现。

说我想在SEVERE(1000)和警告(900)之间获得额外的等级 - >错误(950)。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

我设法为自己解决这个问题。 万一有人对此感兴趣。

你要做的就是创造一个超级"目录。这与"客户"处于同一水平你模块的目录。

在此目录中,您将创建一个java.util.logging.Level类,其内容与在GWT源中找到的内容相同。但随着水平的提高。

例如:

/*
 * Copyright 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package java.util.logging;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.logging.impl.LevelImpl;
import com.google.gwt.logging.impl.LevelImplNull;

import java.io.Serializable;

/**
 *  An emulation of the java.util.logging.Level class. See
 *  <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/Level.html">
 *  The Java API doc for details</a>
 */
public class Level implements Serializable {
  private static LevelImpl staticImpl = GWT.create(LevelImplNull.class);
  public static Level ALL = new LevelAll();
  public static Level CONFIG = new LevelConfig();
  public static Level FINE = new LevelFine();
  public static Level FINER = new LevelFiner();
  public static Level FINEST = new LevelFinest();
  public static Level INFO = new LevelInfo();
  public static Level OFF = new LevelOff();
  public static Level EXCEPTION = new LevelException();
  public static Level WARNING = new LevelWarning();
  public static Level ERROR = new LevelError();
  public static Level SEVERE = new LevelSevere();

  private static class LevelAll extends Level {
    @Override public String getName() { return "ALL"; }
    @Override public int intValue() { return Integer.MIN_VALUE; }
  }

  private static class LevelConfig extends Level {
    @Override public String getName() { return "CONFIG"; }
    @Override public int intValue() { return 700; }
  }

  private static class LevelFine extends Level {
    @Override public String getName() { return "FINE"; }
    @Override public int intValue() { return 500; }
  }

  private static class LevelFiner extends Level {
    @Override public String getName() { return "FINER"; }
    @Override public int intValue() { return 400; }
  }

  private static class LevelFinest extends Level {
    @Override public String getName() { return "FINEST"; }
    @Override public int intValue() { return 300; }
  }

  private static class LevelInfo extends Level {
    @Override public String getName() { return "INFO"; }
    @Override public int intValue() { return 800; }
  }

  private static class LevelOff extends Level {
    @Override public String getName() { return "OFF"; }
    @Override public int intValue() { return Integer.MAX_VALUE; }
  }

  private static class LevelException extends Level {
    @Override public String getName() { return "EXCEPTION"; }
    @Override public int intValue() { return 1000; }
  }

  private static class LevelSevere extends Level {
     @Override public String getName() { return "SEVERE"; }
     @Override public int intValue() { return 1000; }
   }

  private static class LevelError extends Level {
     @Override public String getName() { return "ERROR"; }
     @Override public int intValue() { return 950; }
   }

  private static class LevelWarning extends Level {
    @Override public String getName() { return "WARNING"; }
    @Override public int intValue() { return 900; }
  }

  public static Level parse(String name) {
    return staticImpl.parse(name);
  }

  protected Level() { }

  public String getName() {
    return "DUMMY";
  }

  public int intValue() {
    return -1;
  }

  @Override
  public String toString() {
    return getName();
  }

  /* Not Implemented */
  // public boolean equals(Object ox) {}
  // protected Level(String name, int value, String resourceBundleName) {}
  // public String getLocalizedName() {}
  // public String getResourceBundleName() {}
  // public int  hashCode() {}
}

现在你必须标记这个&#34; super&#34;目录作为gwt.xml中的超级源

<super-source path="super"/>

如果您使用的是Eclipse,则必须从源路径中排除此超级目录,以避免eclipse发生错误。并将其再次添加为单独的源路径,以便eclipse检查文件是否有错误。

最后,如果您在子模块中执行所有这些操作,则必须确保在编译主模块时,它优先于rt.jar中的类。这可以通过在编译时使用支持的目录来完成。