Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

How to log first n lines of a stack trace in Java?

+1
−0

How to log first n lines of a stack trace in Java?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

2 answers

+5
−0

It's not clear where this stack trace comes from (either from an exception or the current thread), but it doesn't matter, the way to do it is the same.

Both Exception's and Thread's have the getStackTrace() method. For the current thread, you can use Thread.currentThread().getStackTrace(), but you can call the method for any other thread as well.

In either case, the getStackTrace method will return an array of StackTraceElement's. Just remind that, for threads, it'll return a zero-length array if the thread has not started, has started but has not yet been scheduled to run by the system, or has terminated.

Anyway, once you have the array, it's very straighforward to log the first N lines:

int limit = 5; // number of elements to log

// Get the stack trace from an exception
// or use Thread.currentThread().getStackTrace() to get from the current thread
// or simply thread.getStackTrace() to get from any other thread
StackTraceElement[] elements = exception.getStackTrace();

// just in case the array has fewer elements than the limit
int len = Math.min(elements.length, limit);
for (int i = 0; i < len; i++) {
    log.error(elements[i].toString());
}
History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Throwable:

...
Arrays.stream(throwable.getStackTrace())
          .limit(yourIntLimit)
          .forEach(stackTraceElement ->
               log.error(stackTraceElement.toString()));
...

Thread:

...
Arrays.stream(Thread.currentThread().getStackTrace())
          .limit(yourIntLimit)
          .forEach(stackTraceElement ->
               log.error(stackTraceElement.toString()));
...

Contributing:
Thread.currentThread(),
thank you mikea,
https://stackoverflow.com/a/21706861

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »