gpt4 book ai didi

java - 属性的级别值错误,无法为 java.util.logging.FileHandler 设置级别

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:48:25 27 4
gpt4 key购买 nike

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

除了 java.util.logger,我不能使用其他任何东西。我在我的项目中使用位于/conf 文件夹中的属性文件。

我收到这个错误:

Bad level value for property: java.util.logging.FileHandler.level

Can't set level for java.util.logging.FileHandler

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

我无法理解我可能在 conf 文件或代码中犯了什么错误:导入是正确的,我没有关于代码的错误/警告。

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

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

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

WARNING: attention please

please 03-Jan-2018 14:01:03 logger.classLogger error

SEVERE: error

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

INFO: 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");

}
}

最佳答案

在我的 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");
}
}

日志记录属性

.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]

关于java - 属性的级别值错误,无法为 java.util.logging.FileHandler 设置级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48079213/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com