How do I log SQL Statements in Dropwizard - java

How do I turn on SQL query logging for a Dropwizard application? I would like it to only log SQL in certain environments.

In your application YAML file add a "logging:" definition like the following:
# Logging settings.
logging:
# The default level of all loggers. Can be OFF, ERROR, WARN, INFO, DEBUG, TRACE, or ALL.
level: INFO
# Logger-specific levels.
loggers:
# Overrides the levels of certain packages or files.
"org.skife.jdbi.v2": TRACE

Are you using jdbi?, if so, this is working for me:
Set a logger when you create the DBI instance:
DBI dbi = new DBI(dataSource);
dbi.setSQLLog(new SLF4JLog());
Add this configuration to your config file:
logging:
level: INFO
loggers:
"org.skife": TRACE
I guess this idea should be also valid for Hibernate or any other DB access framework.

Related

Spring Logging setting Log level for JDK classes

currently I'm trying to set trace logging level for internal JDK classes, especially for the new HttpClient.
When I add to my application.yaml:
logging:
level:
root: INFO
Everything logs in trace.
But when I want to enable trace logging only for JDK packages like:
logging:
level:
root: INFO
jdk.internal.net.http: TRACE
Those packages don't start log in trace, the entire app uses root level, which is INFO.
What do I need to solve this problem? Thanks guys.

remove hibernate logging from Dropwizard app logs

I see a lot of logs logging al the queries that make the logs not very useful. I am trying to remove this logging from my dropwizard app logs I tried to do it through the yml file
logging:
level: "DEBUG"
loggers:
org.hibernate: ERROR
And also in the logback.xml
<logger name="org.hibernate">
<level value="ERROR" />
</logger>
I also tried appenders to the yml file as console and syslog. What is the way to remove these SELECT statements from the logs?
I dont want to move the logs to another file as I do want to see the errors
The logger isnt org.hibernate but I only see "Hibernate: select * FROM ....."
You should try changing your default application logging level to INFO instead
logging:
level: INFO
and further, modify log level of a package using
# Sets the level for 'org.hibernate' to ERROR
loggers:
org.hibernate: ERROR
Here is an effective example of the usage from dropwizard itself.
Or in your case probably the package contributing to the logs as
loggers:
org.hibernate.SQL: ERROR # note - not moving to another file either
You have to set hibernate.show_sql to false
database:
properties:
hibernate.show_sql: false
However, this alone may not work. I upvoted the other answer as it led me to this - you do indeed have to pay attention to your configured logging levels for the hibernate package because surprisingly, if your org.hibernate.SQL logging level is set to DEBUG then this will override the hibernate.show_sql: false config and log the SQL anyway!
You need to make sure it's set to INFO or greater
logging:
loggers:
"org.hibernate.SQL":
level: INFO
In my case, setting the "hibernate engine" level (in the application.yaml file) decreased the number of log messages:
loggers:
org.hibernate.engine: error

java.util.logging How to override the handler log level for specific packages?

In java.util.logging log configuration, I want to override the console log level specifically for some packages, how can I do that?
For example, I tried the configuration
handlers= java.util.logging.ConsoleHandler
.level= INFO
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
com.mypackage.MyClass.level= ALL
The MyClass log level being set to ALL, but it do not seem to log anything less than info on the console.
I think , you should give the log level at package level, not at the class level.
Try com.mypackage.level= ALL instead of com.mypackage.MyClass.level= ALL.
Also you can edit these settings by login to WAS admin console, under logs and traces.

How to set loglevel of the Appengine DataStore in Java

I have a logging.properties file in my Java AppEngine project (using java.util.logging) that looks like this:
# Set the default logging level for all loggers to WARNING
.level = INFO
# tighten logging on the DataNucleus Categories
DataNucleus.JDO.level=WARN
# - All messages general to JDO
DataNucleus.JPA.level=WARN
# - All messages general to JPA
DataNucleus.Persistence.level=WARN
# - All messages relating to the persistence process
DataNucleus.Query.level=WARN
# - All messages relating to queries
DataNucleus.Lifecycle.level=WARN
# - All messages relating to object lifecycle changes
DataNucleus.Cache.level=WARN
# - All messages relating to the DataNucleus Cache
DataNucleus.ClassLoading.level=WARN
# - All exceptions relating to class loading issues
DataNucleus.MetaData.level=WARN
# - All messages relating to MetaData
DataNucleus.Management.level=WARN
# - All messages relating to Management
DataNucleus.General.level=WARN
# - All general operational messages
DataNucleus.Connection.level=WARN
# - All messages relating to Connections.
DataNucleus.JCA.level=WARN
# - All messages relating to Connector JCA.
DataNucleus.Transaction.level=WARN
# - All messages relating to transactions
DataNucleus.Plugin.level=WARN
# - All messages relating to DataNucleus plug-ins
DataNucleus.ValueGeneration.level=WARN
# - All messages relating to value generation
DataNucleus.Datastore.level=WARN
# - All general datastore messages
DataNucleus.Datastore.Schema.level=WARN
# - All schema related datastore log messages
DataNucleus.Datastore.Persist.level=WARN
# - All datastore persistence messages
DataNucleus.Datastore.Retrieve.level=WARN
# - All datastore retrieval messages
DataNucleus.Datastore.Native.level=WARN
# - Log of all 'native' statements sent to the datastore
DataNucleus.Enhancer.level=WARN
# - All messages from the DataNucleus Enhancer.
DataNucleus.SchemaTool.level=WARN
# - All messages from DataNucleus SchemaTool
DataNucleus.IDE.level=WARN
# - Messages from the DataNucleus IDE.
Sadly, now I am getting log messages such as:
2012-04-17 16:13:45.112
org.datanucleus.plugin.NonManagedPluginRegistry resolveConstraints: Bundle "org.datanucleus.jpa" has an optional dependency to "org.datanucleus.enhancer" but it cannot be resolved
I 2012-04-17 16:13:45.115
org.datanucleus.plugin.NonManagedPluginRegistry resolveConstraints: Bundle "org.datanucleus" has an optional dependency to "org.eclipse.equinox.registry" but it cannot be resolved
I 2012-04-17 16:13:45.719
org.datanucleus.PersistenceConfiguration setProperty: Property datanucleus.rdbms.sql.allowAllSQLStatements unknown - will be ignored
I 2012-04-17 16:13:47.806
org.datanucleus.store.appengine.MetaDataValidator validate: Performing appengine-specific metadata validation for in.animeshpathak.nazdeeq.models.Note
Can someone please tell me how I can properly configure my logging.properties file? I need the general loglevel to INFO in order to see debug messages from my code at this moment, but want to set all DataNucleus loglevels to WARN.
Thanks.
Actually, if you want to see debug messages from your code you need FINEST level, not INFO.
Try replacing your logging.properties with this:
# Set the default logging level for all loggers to FINEST
.level=FINEST
# Set the default logging level for ORM, specifically, to WARNING
DataNucleus.JDO.level=WARNING
DataNucleus.Persistence.level=WARNING
DataNucleus.Cache.level=WARNING
DataNucleus.MetaData.level=WARNING
DataNucleus.General.level=WARNING
DataNucleus.Utility.level=WARNING
DataNucleus.Transaction.level=WARNING
DataNucleus.Datastore.level=WARNING
DataNucleus.ClassLoading.level=WARNING
DataNucleus.Plugin.level=WARNING
DataNucleus.ValueGeneration.level=WARNING
DataNucleus.Enhancer.level=WARNING
DataNucleus.SchemaTool.level=WARNING
Per DataNucleus's suggestion, this is much more concise:
# Set the default logging level for all loggers to FINEST
.level=FINEST
# Set the default logging level for ORM, specifically, to WARNING
DataNucleus.level=WARNING

Set differnet levels of logging for loggers in log4j

I am using log4j for my application - there I need to see all log messages, so I defined
# General configuration
log4j.rootLogger = ALL, ConsoleAppender
# Appender configuration
log4j.appender.ConsoleAppender = org.apache.log4j.ConsoleAppender
log4j.appender.ConsoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.ConsoleAppender.layout.ConversionPattern = %5p (%c) %m%n
But, after I started using Apache's HTTPClient library (which is awesome btw), my console got clouded up by its logging - everything was logged and I am not able to see the log output from my own application any more.
So how can I tell the HTTPClient library that it should only log WARN messages, while my own application still logs on ALL levels?
I create my application logger using
Logger logger = Logger.getLogger(MyClass.class);
PropertyConfigurator.configure("log4j.properties");
It's quite simple. Just add the below line.
log4j.logger.org.apache.commons.httpclient=WARN
Similarly, you can configure levels for any package com.foo or class com.foo.Bar by appending it to the prefix log4j.logger as follows:
log4j.logger.com.foo=MYLEVEL
log4j.logger.com.foo.Bar=DEBUG

Categories