Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printStackTrace to java.util.logging.Logger

How do I print the entire stack trace using java.util.Logger? (without annoying Netbeans).

The question should've originally specified staying within 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

like image 327
Thufir Avatar asked Sep 19 '13 08:09

Thufir


People also ask

How do I print exception stack trace in log file?

The function printStackTrace() of the Exception class can take one parameter, either a PrintStream or a PrintWriter. Thus, it is possible, using a StringWriter, to print the stack trace into a String: StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e. printStackTrace(pw);

What can I use instead of E printStackTrace?

Loggers should be used instead of printing the whole stack trace on stream. e. printStackTrace() prints a Throwable and its stack trace to stream which could inadvertently expose sensitive information. Loggers should be used instead to print Throwables, as they have many advantages.

What is Java Util logger?

A Logger object is used to log messages for a specific system or application component. Loggers are normally named, using a hierarchical dot-separated namespace. Logger names can be arbitrary strings, but they should normally be based on the package name or class name of the logged component, such as java.net or javax.

What is import Java Util logger?

Logger in Java - Java Log Manager java. util. logging. LogManager is the class that reads the logging configuration, create and maintains the logger instances. We can use this class to set our own application specific configuration.


2 Answers

Why don't you put the exception in the logger?

You can use this method :

logger.log(Level level, String msg, Throwable thrown)  
like image 29
Michael Laffargue Avatar answered Sep 29 '22 11:09

Michael Laffargue


The severe method is only used to log severe messages without associated throwable information. If you need to log throwable information then you should use the log method instead:

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); } 
like image 89
M. Abbas Avatar answered Sep 29 '22 13:09

M. Abbas