java.lang.ClassCastException:org.apache.log4j.Logger无法强制转换

时间:2017-08-10 14:05:16

标签: log4j java-7

我扩展了org.apache.log4j.Logger,用于实现方法启动和存在的日志记录。 当我没有在log4j.properties中为类设置loglevel时,它工作正常。 当我设置

log4j.logger.de.martinm.tools.UniCredit.ExportOperator=INFO

我得到一个例外:

  

线程中的异常" main" java.lang.ClassCastException:org.apache.log4j.Logger无法强制转换为de.martinm.tools.Logging.MMLogger           at de.martinm.tools.UniCredit.ExportOperator。(ExportOperator.java:21)           at de.martinm.tools.UniCredit.ExportOperator.main(ExportOperator.java:330)

public class MMLogger extends Logger {

    private static MyLoggerFactory myFactory = new MyLoggerFactory();

    public MMLogger(String name) {
        super(name);
    }

    public static Category getInstance(String name) {
        return Logger.getLogger(name, myFactory); 
      }

    public static Logger getLogger(String name) {
        return Logger.getLogger(name, myFactory); 
    }

    public void enter(Logger logger, String method) {
        super.debug(method+" enter");
    }

    public void exit(Logger logger, String method) {
        super.debug(method+" exit");
    }

    public void debug(Logger logger, String method, String text) {
        super.debug(method+" "+text);
    }

    public void warn(Logger logger, String method, String text) {
        super.warn(method+" "+text);
    }
    public void info(Logger logger, String method, String text) {
        super.info(method+" "+text);
    }
    public void error(Logger logger, String method, String text) {
        super.error(method+" "+text);
    }
}

public class MyLoggerFactory implements LoggerFactory {

  /**
     The constructor should be public as it will be called by
     configurators in different packages.  */
  public
  MyLoggerFactory() {
  }

  public
  Logger makeNewLoggerInstance(String name) {
    return new MMLogger(name);
  }
}

以下是我的代码的一部分

public class ExportOperator {

    //public static Logger logger = Logger.getLogger(ExportOperator.class.getName());
    public MMLogger Mylogger = (MMLogger) MMLogger.getLogger(ExportOperator.class.getName());

    public Connection db_con;
    static Utils my_utils = new Utils();
    public Properties props = new Properties();
    public String output_dir;
    public int mid;
    public String admin_id;
    public int op_id;

0 个答案:

没有答案