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

  • Home
  • SEARCH
  • 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 4347636
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:19:59+00:00 2026-05-21T12:19:59+00:00

I need to log all the queries to an Oracle database in my project

  • 0

I need to log all the queries to an Oracle database in my project to a log file.

What would be a good solution to achieve this? Some sample usage would be appreciated.

I have looked at SLF4J with jdbcdslog, but I’m not sure how I can log to a file with it. Moreover, I would need to “filter” some of the logs (because I don’t need to know when some getxxxx method get’s invoked)

Preferably, I’d prefer to use java.util.logging but it is not a requirement.

Thanks.

** UPDATE **

I found this Oracle article, however it does not really tell how to programatically do the same thing.

  • 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-05-21T12:19:59+00:00Added an answer on May 21, 2026 at 12:19 pm

    After much reading, this is how I got things working :


    NOTE : Fore more information, read the Oracle Diagnosability in JDBC document


    Properties prop = new Properties();
    prop.put ("user", USER);
    prop.put ("password", PASS);
    // prop.put(propname, propValue);
    
    Class.forName("oracle.jdbc.driver.OracleDriver");
    
    enableLogging(false);
    
    conn = DriverManager.getConnection("jdbc:oracle:thin:@"+HOST+":"+PORT+":"+SID, prop);
    

    And here’s the magic :

    static private void enableLogging(boolean logDriver) 
    throws MalformedObjectNameException, NullPointerException, 
           AttributeNotFoundException, InstanceNotFoundException, 
           MBeanException, ReflectionException, InvalidAttributeValueException, 
           SecurityException, FileNotFoundException, IOException 
    {
        oracle.jdbc.driver.OracleLog.setTrace(true);
    
        // compute the ObjectName
        String loader = Thread.currentThread().getContextClassLoader().toString().replaceAll("[,=:\"]+", "");
        javax.management.ObjectName name = new javax.management.ObjectName("com.oracle.jdbc:type=diagnosability,name="+loader);
    
        // get the MBean server
        javax.management.MBeanServer mbs = java.lang.management.ManagementFactory.getPlatformMBeanServer();
    
        // find out if logging is enabled or not
        System.out.println("LoggingEnabled = " + mbs.getAttribute(name, "LoggingEnabled"));
    
        // enable logging
        mbs.setAttribute(name, new javax.management.Attribute("LoggingEnabled", true));
    
        File propFile = new File("path/to/properties");
        LogManager logManager = LogManager.getLogManager();
        logManager.readConfiguration(new FileInputStream(propFile));
    
        if (logDriver) {
            DriverManager.setLogWriter(new PrintWriter(System.err));
        }
    }
    

    The properties file (from Oracle’s documentation) :

    .level=SEVERE
    oracle.jdbc.level=INFO
    oracle.jdbc.handlers=java.util.logging.ConsoleHandler
    java.util.logging.ConsoleHandler.level=INFO
    java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
    

    Basically, this is where the handlers are declared

    oracle.jdbc.handlers=java.util.logging.ConsoleHandler
    

    Declares the ConsoleHandler to be used by Oracle’s JDBC driver. Any and any number of handlers can be declared here, one per line, with the class’ full qualified name :

    oracle.jdbc.handlers=java.util.logging.ConsoleHandler
    oracle.jdbc.handlers=java.util.logging.FileHandler
    ...
    

    One can provide their own custom made handlers with the same rule. The following lines are to setup the handler

    java.util.logging.ConsoleHandler.level=INFO
    

    will call the methode setLevel(Level.INFO) of the ConsoleHandler handler instance.

    com.my.own.project.logging.handler.MyHandler.foo=Bar
    

    will call the method setFoo("Bar") of the MyHandler handler instance. And that’s it.

    Happy logging!

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

Sidebar

Related Questions

In my project I need to log all queries executed against my database. As
I need to log all post and get requests on web site in the
I'm trying to filter webserver log files using grep. I need to output all
I need to log some data when some functions are hit, both at the
I need to log a user out and redirect to a URL. How would
I'm writing an extension for Firefox, and I need to log some data to
I need to read through some gigantic log files on a Linux system. There's
I'm creating a table called news_feed , this will log all the actions a
I need to log all PreparedStatements in my application. So far I've done logging
I need to turn this on/enable it. I found some info on the net

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.