Log4j2: trace log stack without exception

I am using Log4j2, some versions of beta-10 or so.

It is easy to write a stack trace if there is an exception:

} catch (Exception ex) {
  log.error("Doing stuff went wrong", ex);
}

Suppose there is no object Throwable- I realized that there is a problem and you want to register an error:

 if (stuffIsWrong()) {
   log.error("Stuff went wrong");
 }

How can I tell Log4j2 for the stack trace log starting from the current method?

+4
source share
3 answers

Just create a new exception

if (stuffIsWrong()) {
    log.error("Stuff went wrong", new Exception("Stracktracegenerator"));
}
+3
source

I had a similar need for a stack trace, although nothing "went wrong." I understand your reluctance to use the exception, although nothing happened.

, , .

"" , , Exception NoProblemJustShowingStackTrace, , , . , , "" /, , . .

, throw , , , .


, ,% location,% line,% file,% class, % . , .

+1

The stack trace of any method can be printed with the following code:

for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
    System.out.println(ste);
}

just check your status and, if it does, print a stack trace in the logs

0
source

Source: https://habr.com/ru/post/1538374/


All Articles