[英]printStackTrace to java.util.logging.Logger
How do I print the entire stack trace using java.util.Logger?如何使用java.util.Logger 打印整个堆栈跟踪? (without annoying Netbeans).
(没有烦人的Netbeans)。
The question should've originally specified staying within Java SE.该问题最初应该指定留在Java SE 中。 Omitting that requirment was an error on my part.
省略该要求是我的错误。
-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
code with the error:有错误的代码:
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 {
}
}
}
see also:也可以看看:
https://stackoverflow.com/a/7100975/262852 https://stackoverflow.com/a/7100975/262852
The severe
method is only used to log severe messages without associated throwable information. severe
方法仅用于记录没有关联的可抛出信息的严重消息。 If you need to log throwable information then you should use the log
method instead:如果您需要记录可抛出信息,则应改用
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);
}
Why don't you put the exception in the logger?你为什么不把异常放在记录器中?
You can use this method :您可以使用此方法:
logger.log(Level level, String msg, Throwable thrown)
Maybe a duplicated question?也许是重复的问题? Java - Need a logging package that will log the stacktrace
Java - 需要一个记录堆栈跟踪的日志包
Below the explanation from the given url在给定网址的解释下方
Using log4j this is done with:
使用 log4j,这是通过以下方式完成的:
logger.error("An error occurred", exception);
The first argument is a message to be displayed, the second is the exception (throwable) whose stacktrace is logged.
第一个参数是要显示的消息,第二个参数是记录堆栈跟踪的异常(可抛出)。
Another option is commons-logging, where it's the same:
另一种选择是 commons-logging,它是相同的:
log.error("Message", exception);
With
java.util.logging
this can be done via:使用
java.util.logging
这可以通过以下方式完成:logger.log(Level.SEVERE, "Message", exception);
You don't explicitly print the stack trace;您没有明确打印堆栈跟踪;
Throwable
s have stack traces attached to them, and you can pass a Throwable
to the log methods: Throwable
附有堆栈跟踪,您可以将Throwable
传递给日志方法:
log(Level level, String msg, Throwable thrown)
You could use Apache ExceptionUtils.您可以使用 Apache ExceptionUtils。 In your case
在你的情况下
try {
data = new GameData.Builder().enemy(enemy).build();
log.fine("new data object\t\t" + data.getEnemy());
setChanged();
notifyObservers(data);
} catch (NullPointerException npe) {
logger.info(**ExceptionUtils.getFullStackTrace(npe)**);
}
You should redirect the System.err to the logger, the process is not too simple but you can use this code:您应该将 System.err 重定向到记录器,该过程不是太简单,但您可以使用以下代码:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LogOutputStream extends ByteArrayOutputStream {//java.io.OutputStream {
private String lineSeparator;
private Logger logger;
private Level level;
public LogOutputStream(Logger logger, Level level) {
super();
this.logger = logger;
this.level = level;
this.lineSeparator = System.getProperty("line.separator");
}
@Override
public void flush() throws IOException {
String record;
synchronized (this) {
super.flush();
record = this.toString();
super.reset();
if ((record.length() == 0) || record.equals(this.lineSeparator)) {
// avoid empty records
return;
}
this.logger.logp(this.level, "", "", record);
}
}
}
And The code to set this (that should called the when you first create the logger和设置这个的代码(应该在你第一次创建记录器时调用
Logger logger = Logger.getLogger("Exception");
LogOutputStream los = new LogOutputStream(logger, Level.SEVERE);
System.setErr(new PrintStream(los, true));
This will redirect the System.err stream to the logger.这会将 System.err 流重定向到记录器。
您也可以尝试使用apache commons 中的 ExceptionUtils
The exception is due to the printstacktrace
method being void
, meaning it doesn't return anything.异常是由于
printstacktrace
方法为void
,这意味着它不返回任何内容。 You are trying to do:您正在尝试执行以下操作:
log.severe(npe.printStackTrace(System.out));
My guess is that the severe
method needs a String
and not void
.我的猜测是,
severe
方法需要一个String
而不是void
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.