问题
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?
回答1:
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.
String logConfig = ".level=" + java.util.logging.Level.INFO + '\n';
logConfig += "handlers=java.util.logging.ConsoleHandler\n";
// ensure ConsoleHandler does not filter
logConfig += "java.util.logging.ConsoleHandler" + ".level=" + java.util.logging.Level.FINEST + '\n';
//set your custom levels
logConfig += "org.apache.cxf" + ".level=" + java.util.logging.Level.WARNING + "\n";
try {
java.util.logging.LogManager.getLogManager().readConfiguration(new java.io.ByteArrayInputStream(logConfig.getBytes("UTF-8")));
// no need to close ByteArrayInputStream -- it is a no-op
}
catch (IOException ioe) {
System.err.println("cannot fully configure logging");
ioe.printStackTrace();
}
Note there are a few issues with this approach:
- You are overwriting the built-in configuration, so you have to set the root handler. If you set
-Djava.util.logging.config.file
, it will also be overwritten. - Total hack - might not be maintainable or understandable later.
- Your config string has to be valid. Note all lines have to end with
\n
. - Has to be called very early, preferably the first line in
main()
. If this is not possible, you may also need to iterate all the existing Loggers and update their config usingsetLevel()
too.
回答2:
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
//equivalent to -Dorg.apache.cxf.Logger=org.apache.cxf.common.logging.Log4jLogger
System.setProperty("org.apache.cxf.Logger","org.apache.cxf.common.logging.Log4jLogger");
//Creates the file appender
FileAppender fa = new FileAppender();
fa.setName("FileLogger");
fa.setFile("mylog.log");
fa.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
fa.setThreshold(Level.WARN);
fa.setAppend(true);
fa.activateOptions();
//Add the appender to CXF logger
Logger.getLogger("org.apache.cxf").addAppender(fa);
You will need log4j.jar in the classpath and execute the code before any CXF initialization
回答3:
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.
log4j.apache.cxf = WARN
回答4:
Configuring logging levels
In the /etc
folder of the CXF distribution there is a sample Java SE logging.properties
file you can use to configure logging. For example, if you want to change the console logging level from WARNING
to FINE
, you need to update two properties in this logging.properties
file as below:
.level= FINE
java.util.logging.ConsoleHandler.level = FINE
N.B: If you want to change FINE
to other LEVEL, just change FINE
to others
Once 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:
<target name="runClient">
<java classname="client.WSClient" fork="true">
<classpath>
<pathelement location="${build.classes.dir}"/>
<fileset dir="${env.CXF_HOME}/lib">
<include name="*.jar"/>
</fileset>
</classpath>
<jvmarg value="-Djava.util.logging.config.file=/usr/myclientapp/logging.properties"/>
</java>
</target>
Alternatively, for SOAP clients, you can modify the Java-wide logging.properties
file in the JDK_HOME/jre/lib
folder, or for servlet-hosted web service providers, placing a logging.properties
file in the WEB-INF/classes
folder (see here for more details.)
Resource Link:
- http://cxf.apache.org/docs/general-cxf-logging.html
Enables/Disable CXF Logging (payload)
To remove cxf logging just comment out or remove code the following code from your cxf configuration xml file.
<cxf:bus> <cxf:features> <cxf:logging></cxf:logging> </cxf:features> </cxf:bus>
Turn off extra logging:
To turn off extra logging, change the org.apache.cxf
Logger level to ERROR
. Add this line to log4j.properties
:
log4j.logger.org.apache.cxf=ERROR
Resource Link:
How can I turn off extra logging?
来源:https://stackoverflow.com/questions/40461313/turn-off-java-util-logging-for-a-specific-package-programmatically