Turning off logging for Hibernate c3p0 - java

I'm using Hibernate's c3p0 connection pooling and standard Java 1.4 java.util.logging. Upon startup, my app sets up it's logging properties (including formatter and log levels) in static block. Every time I start my app, I see the following:
2011-04-16 17-43-51 [com.mchange.v2.log.MLog] INFO: {MLog.<clinit>) MLog clients using java 1.4+ standard logging.
2011-04-16 17-43-51 [com.mchange.v2.c3p0.C3P0Registry] INFO: {C3P0Registry.banner) Initializing c3p0-0.9.1 [built 16-January-2007 14:46:42; debug? true; trace: 10]
2011-04-16 17-43-51 [com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource] INFO: {AbstractPoolBackedDataSource.getPoolManager)
...
I've tried
Logger.getLogger("com.mchange").setLevel(Level.WARNING);
com.mchange.v2.log.MLog.getLogger().setLevel(MLevel.WARNING);
System.setProperty("com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL", "WARNING");
but only way to prevent it that I found for now is
Logger.getLogger("").setLevel(Level.WARNING);
which affects everything - not a good side effect. Google didn't help. Could anyone help please?

The way I found is to set the system property
System.setProperty("com.mchange.v2.log.MLog", "com.mchange.v2.log.FallbackMLog");
in addition to
System.setProperty("com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL", "WARNING");
I thought, that absence of any other logging system wil make that optional, but it seems, that I was wrong.
P.S.
Damn those wheel-reinvented custom logging implementations, like the one used by c3p0...

The way I found for achieving this
Create in your classpath a file called mchange-log.properties and put into it properties suggested by Frozen Spider.
com.mchange.v2.log.MLog=com.mchange.v2.log.FallbackMLog
com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL=WARNING
Thats work fine even when you are not able to set system properties directly.

It appears that c3p0 logging defaults to DEBUG. That can result in a lot of noise.
By adding a line like this to log4j.properties, you are telling the logger not to bother you with c3p0 messages - unless it's something important:
log4j.logger.com.mchange.v2=WARN

Do you not want to see any c3p0 logging?
If so try:
Logger.getLogger("com.mchange.v2.c3p0").setLevel(Level.WARNING);
OR, if you don't even want to see the first line of the log:
Logger.getLogger("com.mchange.v2").setLevel(Level.WARNING);

This is probably really late, but according to the c3p0 project website it is possible to configure the logging inside the mchange-log.properties so that you can capture the information using slf4j or log4j (and thus also with Logback).
The link http://www.mchange.com/projects/c3p0/#configuring_logging provides this information that in your mchange-log.properties file set the property com.mchange.v2.log.MLog to equal com.mchange.v2.log.slf4j.Slf4jMLog then in your logback.xml you can provide a logger like this:
<logger name="com.mchange" level="warn" additivity="false">
<appender-ref ref="c3p0-log" />
</logger>
Note: you will need to create a logback appender called c3p0-log before you can use this exact piece of code.

create a file called log4j.properties in your root classpath
set the following in there,
# Configure the name of the file for the LOGGER appender
log4j.appender.LOGGER=org.apache.log4j.ConsoleAppender
log4j.appender.LOGGER.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGGER.layout.ConversionPattern=%d{MM-dd#HH:mm:ss} %-5p (%13F:%L) %3x - %m%n
log4j.appender.LOGGER.append=false
# this line logs everything from hibernate package at info level, you can refine this to include only some pachages like log4j.logger.org.hibernate.hql etc.,
log4j.logger.org.hibernate=INFO, LOGGER
log4j.logger.org.jboss.cache=INFO, LOGGER
this is a much better way of implementing the logging because if you set the logging strategy programmatically, then the config sometimes might not take effect at all (like in your case).. if you use the log4j.properties file ,the config is applied at application startup & everything works smoothly.

This only happens on older c3p0 version. So it might also be worth checking if you can just update to a newer version.

Related

Hazelcast log level flag

Here is my Hazelcast deployment procedure in two steps:
1) Download the Hazelcast jar at:
https://repo1.maven.org/maven2/com/hazelcast/hazelcast-all/$HAZELCAST_VERSION/hazelcast-all-$HAZELCAST_VERSION.jar
2) Deploy Hazelcast using the command:
java -server $JAVA_OPTS com.hazelcast.core.server.StartServer
Without deviating much from the described procedure, it there a way to set the log level?
More specifically what I'm looking for is something like adding a flag similar to -Djava.util.logging.level=WARN to filter out the info logs, but I have been unsuccessful so far.
I'm trying to avoid using alternative log libraries and xml configuration files for this purpose.
Thank you for your attention
Hazelcast provides multiple ways you can configure the logger. This may be helpful.
Using JVM parameter: java -Dhazelcast.logging.type=log4j
Using System class: System.setProperty( "hazelcast.logging.type", "log4j" );
and in the log4j, add the following.
<logger name="com.hazelcast">
<level value="warn" />
</logger>
Logging Configuration
I believe it's more like a logging config question, rather than Hazelcast. java.util.logging, like other logging frameworks, uses a config file, which you can point using java.util.logging.config.file param. Or you create a LogManager and point it with java.util.logging.config.class param.
Hazelcast doesn't have any internal logging library, just uses java.utl.logging. To configure it, you need to look that logging framework's config options.

Spring boot logging / Java logging - Tool to show config/setup

I'm used to using log4j and whenever there were setup/config problems I'd enable "-Dlog4j.debug" and all the config info would be dumped out at startup.
This was very useful on a number of occasions.
Now I'm working on a Spring boot application, which I've found uses:
Commons logging Logger statements in the client code
A bridge jar (jcl-over-slf4j-xxx.jar) which translates the commons logging calls into slf4j more info here
Finally slf4j uses "logback" as the underlying logging framework
I found it rather painful to figure all this out.
Is there an equivalent of -Dlog4j.debug which can show me how this is all hanging together at startup time?
This is the best/only option I've found so far, and it's logback specific.
Use this -D on the command line:
-Dlogback.statusListenerClass=ch.qos.logback.core.status.OnConsoleStatusListener
Taken from here
This essentially is the logback equivalent of log4j's -Dlog4j.debug.
It dumps the logback startup sequence to the console at startup time, as the logging system is initialised.
This is not a true answer as I'd like some messages to show before this to show why logback is being used, but I haven't found anything like that yet.
Aside: This may also be useful for Spring Boot:
-Ddebug
Taken from here and here
If you are using logback, I assume you are using the logback.xml file? Then if you start that file with <configuration debug="true"> then it should dump the status information. More information in the documentation under status data section.

Monitoring EclipseLink JPA progress

I'm back at my first EclipseLink JPA project and noticed some issues regarding the performance. Here's the situation:
The application runs with a local Apache Derby DB. During the creation of the EntityManager the console logs a few infos and warnings, which takes a few seconds but is fine so far. However, if I package the whole code into an executable jar and run it outside Eclipse, the whole process takes a lot more time.
Now while it would be wise to find the reason for this issues in the first place, I at least wanted to add a ProgressBar in order keep rough track of the progress.
I looked up the EclipseLink wiki and found out that it already features a PerformanceMonitor and some logging utilities. I am now stuck at the seemingly simple task of putting 1 and 1 together.
How can I hook up my ProgressBar with EclipseLink? Or another approach: How can I log ongoing activities of EclipseLink in the first place?
Any help is much appreciated.
Cheers
Here is the steps to produce a log file for an SE project (non-application server one) which has EclipseLink JPA:
Open the file "persistence.xml" and assign the following properties:
property name="eclipselink.logging.logger" value="JavaLogger"
property name="eclipselink.logging.level" value="FINEST"
Given that Eclipselink JavaLogger is in fact a java.util.logging one, refer to your JDK folder to locate the file logging.properties which is to be modified as follows:
handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
# Default global logging level.
.level= FINEST
# default file output is in user's home directory
# java.util.logging.FileHandler.pattern = %h/java%u.log
# but I prefer to hard wire it as follows:
java.util.logging.FileHandler.pattern = C\:/SamLogFile.log
java.util.logging.FileHandler.level = FINER
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
# I personally don't care about the stdout logging, so I turn it off.
java.util.logging.ConsoleHandler.level = OFF
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
Now you can place some logging within your class by importing:
import java.util.logging.Level;
import java.util.logging.Logger;
And calling:
public void yourMethod() {
Logger.getLogger( YourClassName.class.getName() ).log( Level.INFO, "I am logging well......!" ); }
Now you are set. Run/test your project and enjoy reading your log!
You can also take your logging code into a best practice by adding the 2 files slf4j-api-1.7.1.jar and the file slf4j-jdk14-1.7.1.jar to your classpath. These 2 files are to be downloaded from http://www.slf4j.org/download.html
Add to your class the imports:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Then add to your method this line:
LoggerFactory.getLogger(Mysafeperson.class.getName()).info("I am logging using the slf4j api");
This practice is making your code using the slf4j api instead of the java.util.logging (JUL) api. Thus your code is decoupled from JUL and ready for any logging framework to be bound as an slf4j implementation (choices are logback - log4j - or refer to this link.)
The idea I am promoting here is based upon the documentation in http://www.slf4j.org/legacy.html. So:
The file: slf4j-api-1.7.1 is your new api facade.
The file: slf4j-jdk14-1.7.1 makes a bridge from the existing logging implementation which is java.util.logging (JUL) and your application code calling the slf4j api.
Two tips remain:
Be aware that the available defined levels in JUL are:
SEVERE (highest value)
WARNING
INFO
CONFIG
FINE
FINER
FINEST (lowest value)
Thus don’t use the levels (Error – TRACE – DEBUG) of the slf4j api, they will not trigger any logging level. INFO and WARN are OK to use
As for EclipseLink, be aware that you are still tied to using the JUL configuration file logging.properties
You should enable the eclipselink logging by adding this in your persistent unit's properties in the persistence.xml file:
<property name="eclipselink.logging.level" value="DEBUG"/>
to enable a most detailed logging, or even 'ALL' instead of 'DEBUG' if not enough. But remove it when going to production or that line itself will make it slower.
Two common things that could make eclipse link slower are:
having this in in the persistence.xml when not needed:
<property name="eclipselink.ddl-generation" value="create-tables"/>
and NOT having this (if you declare de classes in the persistence.xml file. Set it to false if you do not!!):
<exclude-unlisted-classes>true</exclude-unlisted-classes>
as of the progress bar, I would say it will be very hard, and not precise since even eclipselink don't know the whole amount of classes it will process in he beginning, so I don't think it's worth the effort, but good luck if you find out a way to do it.

hsqldb internal event log configuration

How do I configure internal event monitoring in hsqldb? When I run my Java application, I get the following warnings:
log4j:WARN No appenders could be found for logger (HSQLDB2C7984E18B.org.hsqldb.persist.Logger).
log4j:WARN Please initialize the log4j system properly.
The documentation tells me log4j is not the only option, but it doesn't tell me how to configure my application. Can anyone point me to this documentation? Remember, I don't want to use log4j for hsqldb.
It bears mentioning that a 3rd-party jar I'm referencing requires log4j. Does hsqldb automatically detect that log4j is present and then attempt to use it? Or am I missing something fundamental about how logging works?
Check out this link. It says
The logging facility hands off to Log4j if Log4j is found in the classpath, and otherwise will hand off to java.util.logging.
The consequence of what the message indicates is that no logging for HSQLDB will take place because no appenders were found.
If you wish to suppress the message, add a line like the one below to the log4j.properties file:
log4j.logger.HSQLDB2C7984E18B.org.hsqldb.persist.Logger=FATAL
This will log only FATAL events, which wouldn't happen in normal operation.
You also state that you don't want to use log4j for HSQLDB. Software components that can use log4j leave the logging configuration (including level and where to log, etc.) to the log4j properties settings, which you can edit and configure.
In this case, the logger name is based on the "unique" database name which is initially autogenerated, but which you can change in HSQLDB.
Because as YWE noted hsqldb uses log4j by default if it is found in the classpath, I needed to figure out how to override the log4j.properties found in the 3rd-party library. This I managed to do as follows:
Copy the existing log4j.properties to my project, and add the following line at the beginning:
log4j.rootLogger=WARN, CONSOLE
Add the following VM Arguments:
-Dlog4j.log4j.defaultInitOverride=true
-Dlog4j.configuration=C:/full/path/to/my/log4j.properties
Make sure this line of code runs before anyone (e.g. hsqldb) attempts to use log4j:
org.apache.log4j.PropertyConfigurator.configure("log4j.properties");

How to disable runtime warnings in java?

I am using a jar file in a java program and it generates warnings during runtime. But I don't want that my customer sees these warnings.
How can I disable these warnings.
The warning is as below:
Sep 25, 2009 10:10:33 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://www.craigslist.org/js/jquery.js', but got 'application/x-javascript'.
From the appearance of the message, you are using HtmlUnit which uses Commons Logging for logging messages. Unless you configure Commons Logging to write to a file, the log messages will get logged by the simple logger of Commons Logging which writes out onto the console.
If you want to make the error messages go away you could adopt either of the options:
Configure Commons Logging to write to a file on disk (using log4j).
Redirect the console output to /dev/null or its equivalent, as sal pinpointed.
A much easier way, without having to add Log4j as a dependency, is to simply redirect STDERR.
System.setErr(new PrintStream("/dev/null"));
That's all you really need.
Note: Make sure you reset the stream after you are done with HtmlUnit:
final PrintStream err = new PrintStream(System.err);
System.setErr(new PrintStream("/dev/null"));
// ...
System.setErr(err);
Just copy first lines from the link Vineet posted:
If you don't explicitly configure commons logging to use LOG4J or another logging framework then it will use the simple logger. When using the simple logger, you can change the default logging level by setting the following system property:
System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog","fatal");
Add this code somewhere in the beginning of the program.
Assuming these are log messages, you could configure the logger to write everything to null or /dev/null
Putting a file like this in the path might help
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="NULL" class="org.apache.log4j.FileAppender">
<param name="File" value="/dev/null"/>
</appender>
<logger name="com.gargoylesoftware.htmlunit">
<level value="FATAL"/>
<appender-ref ref="NULL"/>
</logger>
</log4j:configuration>
Since the OP keeps asking how to redirect to /dev/null:
You can achieve a similar effect by calling System.setOut and System.setErr, passing in a PrintStream that does nothing with the output it's given.
This is a terrible hack, and the previous answers are far far cleaner.
I found that HtmlUnit does not include the log4j implementation, only the commons logging "interface" (and abstract classes). Once you toss log4j.jar on the path, HtmlUnit behaves itself much better, and honors its configs.

Categories