I am using an SDK that uses Apache CXF internally. Apache CXF uses the java.util.logging
package for logging by default.
I want to change the logging level from INFO
to WARNING
or turn it off completely. Can I do this programmatically in the main method of my application?
You can use Log4j instead of java.util.logging as is documented. This is a 100% programmatic way to configure a Log4j appender for apache CXF based on this answer
You will need log4j.jar in the classpath and execute the code before any CXF initialization
The link below from Apache CXF has a well understood documentation whether it's java.util.logging or log4j or sl4j:
http://cxf.apache.org/docs/general-cxf-logging.html
It would be very easy if you are using log4j.
You can configure log4j for Apache CXF and then append something like as follows in log4j.properties file which will turn off apache cxf logging.
There is a way to do this programatically, but it's not intuitive. Changing to SLF4J or Log4J may be better if you end up needing control at a a finer level or over multiple classes
The issue is that Loggers are cached using WeakReferences. From the time you call
Logger.setLevel()
until the Logger is actually used, it may be GC'ed. So, the Logger that logs isn't the Logger you set the level on! This is especially true in frameworks where there is a lot of GC at startup.The solution is to programatically set the configuration, not the actual Loggers.
Note there are a few issues with this approach:
-Djava.util.logging.config.file
, it will also be overwritten.\n
.main()
. If this is not possible, you may also need to iterate all the existing Loggers and update their config usingsetLevel()
too.Configuring logging levels
In the
/etc
folder of the CXF distribution there is a sample Java SElogging.properties
file you can use to configure logging. For example, if you want to change the console logging level fromWARNING
toFINE
, you need to update two properties in thislogging.properties
file as below:N.B: If you want to change
FINE
to other LEVEL, just changeFINE
to othersOnce this is done, you will need to set the -Djava.util.logging.config.file property to the location of the
logging.properties
file. As an example, the Ant target below has this property set:Alternatively, for SOAP clients, you can modify the Java-wide
logging.properties
file in theJDK_HOME/jre/lib
folder, or for servlet-hosted web service providers, placing alogging.properties
file in theWEB-INF/classes
folder (see here for more details.)Resource Link:
Enables/Disable CXF Logging (payload)
Turn off extra logging:
To turn off extra logging, change the
org.apache.cxf
Logger level toERROR
. Add this line tolog4j.properties
:Resource Link:
How can I turn off extra logging?