Turn off java.util.logging for a specific package programmatically

I use the SDK that uses Apache CXF internally. Apache CXF uses the java.util.logginglogging package by default .

I want to change the logging level from INFOto WARNINGor completely disable it. Can I do this programmatically in the main method of my application?

+4
source share
4 answers

There is a way to do this programmatically, but it is not intuitive. Migrating to SLF4J or Log4J might be better if you end up needing control at a more subtle level or over multiple classes.

, WeakReferences. Logger.setLevel() , Logger, GC'ed. , Logger, , Logger, ! , GC.

, , .

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();
}

, :

  • , . -Djava.util.logging.config.file, .
  • - , .
  • . , \n.
  • , main(). , , setLevel().
+1

Log4j java.util.logging, documented. 100% Log4j apache CXF .

//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);

log4j.jar CXF

+1

Apache CXF : java.util.logging log4j sl4j:

http://cxf.apache.org/docs/general-cxf-logging.html

, log4j.

log4j Apache CXF, log4j.properties - : apache cxf.

log4j.apache.cxf = WARN
0

/etc CXF Java SE logging.properties, . , WARNING FINE, logging.properties, :

.level= FINE
java.util.logging.ConsoleHandler.level = FINE

N.B: FINE , FINE

, -Djava.util.logging.config.file logging.properties. Ant :

<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>

SOAP logging.properties Java JDK_HOME/jre/lib -, , logging.properties WEB-INF/classes (. .)

:

/ CXF ( )

cxf, : xx xxf.

<cxf:bus>
      <cxf:features>
          <cxf:logging></cxf:logging>
      </cxf:features>
</cxf:bus>

:

, org.apache.cxf Logger ERROR. log4j.properties:

log4j.logger.org.apache.cxf=ERROR

:

?

0

Source: https://habr.com/ru/post/1660042/


All Articles