Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8450519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:59:23+00:00 2026-06-10T10:59:23+00:00

Log a stack trace in Java: new Throwable.printStackTrace() To see SQL statements issued by

  • 0

Log a stack trace in Java:

new Throwable.printStackTrace()

To see SQL statements issued by Hibernate, set show_sql to true.

However, how do I log a stack trace every time a SQL query is issued? I’d like to use this for performance monitoring (i.e. find which parts of our source code generate the most queries).

I looked at interceptors and event listeners, and none of them seem to give a hook at the query level.

I also looked through the source (SQLStatementLogger, Loader) somewhat. I don’t see any hooks.

I could perhaps try a logging jdbc driver, but it’s unclear to me if the stack of the caller would be preserved properly.

I could rewrite byte code, but that seems excessive.

Edit: I could also try AspectJ to advise the SQL executing methods.

Has anyone done this? What’s the best way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:59:25+00:00Added an answer on June 10, 2026 at 10:59 am

    I ended up attaching a new log Appender to the org.hibernate.SQL logger used by org.hibernate.jdbc.util.SQLStatementLogger (and no other class) in Hibernate 3.6.8.

    Logger logger = Logger.getLogger("org.hibernate.SQL");
    org.apache.log4j.rolling.RollingFileAppender appender =
        new org.apache.log4j.rolling.RollingFileAppender();
    appender.setLayout(new SqlLogLayout());
    // .. set appender options ..
    appender.activateOptions();
    logger.addAppender(appender);
    

    Then in my SqlLogLayout I can get the stack trace, and the SQL I get from the log message.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get a detailed log about my stack trace. I can get
Suppose I want to reflect exception in the log. Should I pass stack trace
I want to log the stack trace of exceptions into a file. Is there
In my REST WCF service I log all exceptions on WCF stack level (
I am working on a largish Java web application with Spring, Hibernate, and some
I'm dealing with a null pointer exception for which I have a stack trace.
I catch NullPointerException but log4j does not print stack trace, I aspect number of
I have been trying to parse Java exceptions that appear in a log for
I am new to Multi-threading in JAVA and am facing a problem. I am
I get a java.lang.NoClassDefFoundError: android/util/Log error when I try to run my unit test

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.