属性的级别值不正确,无法为java.util.logging.FileHandler

时间:2018-01-03 14:21:29

标签: java logging properties java.util.logging

这是学校的作业:我必须构建一个能够从我的项目中的任何位置进行日志记录的自定义记录器类。

我不能使用除 java.util.logger 之外的任何其他内容。 我正在使用位于项目中/ conf文件夹中的属性文件。

我收到此错误:

  

属性的错误级别值:java.util.logging.FileHandler.level

     

无法为java.util.logging.FileHandler

设置级别

我认为这就是为什么我的debug()方法无法在我的文件中打印“ddd”以及为什么我不能用我设置的字符串格式化文件:“[%1 $ tc] - [%2 $ s ] [%4 $ s] [%5 $ s]%n“。

我无法弄清楚我在conf文件或代码中犯的错误:导入是正确的,我没有错误/警告代码。

我知道我永远不应该使用绝对路径,但我不认为这是我的问题,解决此问题后路径将被修复。

my / test文件夹中try.log的输出:

  

03-Jan-2018 14:01:03 logger.classLogger警告

     

警告:请注意

     

请03-Jan-2018 14:01:03 logger.classLogger错误

     

严重:错误

     

03-Jan-2018 14:01:03 logger.classLogger info

     

信息:信息

这是我的.properties文件:

.level=ALL
handlers = java.util.logging.FileHandler 
java.util.logging.FileHandler.level = FINE 
java.util.logging.FileHandler.append = true 
java.util.logging.FileHandler.limit = 1000000 
java.util.logging.FileHandler.count = 100 
java.util.logging.FileHandler.pattern =  %t/Log%u%g.log
java.util.logging.SimpleFormatter.format = "[%1$tc] - [%2$s] [%4$s] [%5$s] %n"
java.util.logging.FileHandler.formatter =  java.util.logging.SimpleFormatter

这是我的班级:

  public class classLogger {

  Logger logger;
  FileHandler fh;
  SimpleFormatter sf;

  public classLogger(String classname){

    this.logger = Logger.getLogger(classname);
    logger.setLevel(Level.ALL);

    try {
      String path = "D:\\myprojects\\logger\\conf\\logging.properties";
      FileInputStream configFile = new FileInputStream(path); 
      LogManager.getLogManager().readConfiguration(configFile);
    }
    catch (IOException ex)
    {
    }

    try {
      String directory = "C:\\Documents and Settings\\E520user\\Desktop\\test"; 
      String filename = "\\try.log";
      this.fh = new FileHandler(directory+filename);
    }
    catch (IOException ex1) {

    }

    this.sf = new SimpleFormatter();

    //set formatter
    fh.setFormatter(sf);

    //add handler
    logger.addHandler(fh);

  }

  public void error(String msg){
    logger.severe(msg);
  }

  public void warning(String msg){
    logger.warning(msg);
  }

  public void info(String msg){
    logger.info(msg);
  }

  public void debug(String msg){
    logger.fine(msg);
  }
  public static void main(String[] args) {

    IRSLogger log = new classLogger(classLogger.class.getName());
    log.warning("attention please");
    log.error("error");
    log.debug("dddd");
    log.info("info");

  }
}

1 个答案:

答案 0 :(得分:0)

在我的Mac上尝试过,它运行正常

$ cat try.log 
"[Wed Jan 03 14:35:22 GMT 2018] - [classLogger warning] [WARNING] [attention please] 
""[Wed Jan 03 14:35:22 GMT 2018] - [classLogger error] [SEVERE] [error] 
""[Wed Jan 03 14:35:22 GMT 2018] - [classLogger debug] [FINE] [dddd] 
""[Wed Jan 03 14:35:22 GMT 2018] - [classLogger info] [INFO] [info] 

但我也收到此消息:Can't set level for java.util.logging.FileHandler

您可以尝试从属性文件中删除java.util.logging.FileHandler.level = FINE吗?

提示

您应该删除此行的"表单:java.util.logging.SimpleFormatter.format = "[%1$tc] - [%2$s] [%4$s] [%5$s] %n"

编辑:这适用于我的电脑

import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.*;

public class ClassLogger {

  private Logger logger;
  private FileHandler fh;

  public ClassLogger(String classname){

    this.logger = Logger.getLogger(classname);

    try {
      String path = "/Users/myUser/Workspace/stackoverflow/test/src/main/java/logging.properties";
      FileInputStream configFile = new FileInputStream(path);
      LogManager.getLogManager().readConfiguration(configFile);
    } catch (IOException ex) {
      ex.printStackTrace();
    }

    try {
      String directory = "/Users/myUser/Workspace/stackoverflow/test/src/main/java/";
      String filename = "try.log";
      this.fh = new FileHandler(directory+filename);
    } catch (IOException ex1) {
      ex1.printStackTrace();
    }

    //add handler
    logger.addHandler(fh);
  }

  public void error(String msg) { logger.severe(msg);}
  public void warning(String msg) { logger.warning(msg); }
  public void info(String msg) { logger.info(msg); }
  public void debug(String msg) { logger.fine(msg); }

  public static void main(String[] args) {
    ClassLogger log = new ClassLogger(ClassLogger.class.getName());
    log.warning("attention please");
    log.error("error");
    log.debug("dddd");
    log.info("info");
  }
}

logging.properties

.level=ALL
handlers = java.util.logging.FileHandler
java.util.logging.FileHandler.append = true
java.util.logging.FileHandler.limit = 1000000
java.util.logging.FileHandler.count = 100
java.util.logging.FileHandler.pattern =  %t/Log%u%g.log
java.util.logging.SimpleFormatter.format = [%1$tc] - [%2$s] [%4$s] [%5$s] %n
java.util.logging.FileHandler.formatter =  java.util.logging.SimpleFormatter

try.log

输出
[Wed Jan 03 15:27:55 GMT 2018] - [ClassLogger warning] [WARNING] [attention please] 
[Wed Jan 03 15:27:55 GMT 2018] - [ClassLogger error] [SEVERE] [error] 
[Wed Jan 03 15:27:55 GMT 2018] - [ClassLogger debug] [FINE] [dddd] 
[Wed Jan 03 15:27:55 GMT 2018] - [ClassLogger info] [INFO] [info]