gpt4 book ai didi

java - printStackTrace 到 java.util.logging.Logger

转载 作者:IT老高 更新时间:2023-10-28 20:59:30 26 4
gpt4 key购买 nike

如何使用 java.util.Logger 打印整个堆栈跟踪? (没有恼人的 Netbeans)。

问题最初应该指定留在 Java SE 中。忽略该要求是我的错误。

-do-compile:
[mkdir] Created dir: /home/thufir/NetBeansProjects/rainmaker/build/empty
[mkdir] Created dir: /home/thufir/NetBeansProjects/rainmaker/build/generated-sources/ap-source-output
[javac] Compiling 13 source files to /home/thufir/NetBeansProjects/rainmaker/build/classes
[javac] /home/thufir/NetBeansProjects/rainmaker/src/model/TelnetEventProcessor.java:44: error: 'void' type not allowed here
[javac] log.severe(npe.printStackTrace(System.out));
[javac] ^
[javac] 1 error

BUILD FAILED

有错误的代码:

package model;

import java.util.Observable;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TelnetEventProcessor extends Observable {

private static Logger log = Logger.getLogger(TelnetEventProcessor.class.getName());
private String string = null;

public TelnetEventProcessor() {
}

private void stripAnsiColors() {
Pattern regex = Pattern.compile("\\e\\[[0-9;]*m");
Matcher regexMatcher = regex.matcher(string);
string = regexMatcher.replaceAll(""); // *3 ??
}

public void parse(String string) {
this.string = string;
ifs();
}

// [\w]+(?=\.)
private void ifs() {
log.fine("checking..");
if (string.contains("confusing the hell out of")) {
Pattern pattern = Pattern.compile("[\\w]+(?=\\.)"); //(\w+)\.
Matcher matcher = pattern.matcher(string);
String enemy = null;
GameData data = null;
while (matcher.find()) {
enemy = matcher.group();
}
try {
data = new GameData.Builder().enemy(enemy).build();
log.fine("new data object\t\t" + data.getEnemy());
setChanged();
notifyObservers(data);
} catch (NullPointerException npe) {
log.severe(npe.printStackTrace(System.out));
}

} else if (string.contains("Enter 3-letter city code:")) {
log.fine("found enter city code");
} else {
}
}
}

另见:

https://stackoverflow.com/a/7100975/262852

最佳答案

severe 方法仅用于记录没有相关可抛出信息的严重消息。如果你需要记录 throwable 信息,那么你应该使用 log替代方法:

try {
data = new GameData.Builder().enemy(enemy).build();
log.fine("new data object\t\t" + data.getEnemy());
setChanged();
notifyObservers(data);
} catch (NullPointerException npe) {
log.log(Level.SEVERE, npe.getMessage(), npe);
}

关于java - printStackTrace 到 java.util.logging.Logger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18889941/

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