1

I need to add to my logging.properties the line:

sun.net.www.protocol.http.HttpURLConnection.level = ALL

But this file is generated using log4j.xml, which is in xml format. And I'm not finding how to correctly format this property to xml to generate this line.

using log4j-1.2.8, JRE and JDK 1.8_181, Wildfly 10.1.0.Final

Olivier
  • 13,283
  • 1
  • 8
  • 24
The Student
  • 27,520
  • 68
  • 161
  • 264
  • Well ... I guess, the [official documentation](https://logging.apache.org/log4j/2.x/manual/configuration.html#XML) tells you how ... – Seelenvirtuose Dec 22 '22 at 09:55
  • @Seelenvirtuose sorry I really know nothing about log4j configurations. I'm trying to find a patter here to set a new one, but looks like each one have a specific tag, and there's no tag for "HttpURLConnection", also there's no "HttpURLConnection" on the official documentation. I'll read to try to find where it would fit. – The Student Dec 22 '22 at 13:17
  • What does this have to do with WildFly? – James R. Perkins Dec 22 '22 at 15:25
  • Not every class support log4j , if the class does not use log4j to logging something, even you add `xxx.level=ALL`, it will output nothing. HttpURLConnection.java (https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java) – life888888 Dec 23 '22 at 01:23
  • google `github log4j.xml` have a lots of example code. example (https://gist.github.com/mwicat/1460284), you can find `` – life888888 Dec 23 '22 at 01:27
  • @Student: what version of Log4j are you using (by the name `log4j.xml` of the config file it appears it is the EOLed Log4j 1.2)? What version of the JRE? JRE components log in different ways (but never use an external library) and Log4j 1.2 has a different configuration than Log4j 2.x. – Piotr P. Karwasz Dec 23 '22 at 05:01
  • @PiotrP.Karwasz log4j-1.2.8, jre 1.8_181 – The Student Jan 19 '23 at 12:12

2 Answers2

1

At first glance, you will not be able to configure the logging level for sun.net.www.protocol.http.HttpURLConnection using Log4j, no matter if you use properties or xml configuration files.

As you can see in the source code of the HttpURLConnection class, its logging is based in PlatformLogger, which in turn uses java.util.logging.

The PlatformLogger javadoc describes how it should be configured:

Platform logger provides an API for the JRE components to log messages. This enables the runtime components to eliminate the static dependency of the logging facility and also defers the java.util.logging initialization until it is enabled. In addition, the PlatformLogger API can be used if the logging module does not exist.

If the logging facility is not enabled, the platform loggers will output log messages per the default logging configuration (see below). In this implementation, it does not log the the stack frame information issuing the log message.

When the logging facility is enabled (at startup or runtime), the java.util.logging.Logger will be created for each platform logger and all log messages will be forwarded to the Logger to handle.

Logging facility is "enabled" when one of the following conditions is met:

  1. a system property "java.util.logging.config.class" or "java.util.logging.config.file" is set
  2. java.util.logging.LogManager or java.util.logging.Logger is referenced that will trigger the logging initialization.

Default logging configuration: global logging level = INFO handlers = java.util.logging.ConsoleHandler java.util.logging.ConsoleHandler.level = INFO java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

Basically, you need to configure the system property java.util.logging.config.file pointing to an appropriate properties file with the required properties:

handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=INFO
sun.net.www.protocol.http.HttpURLConnection.level=ALL

Please, consider review this related SO question, it provides different examples and further information about how to do it.

As suggested in that question, you can use the system property javax.net.debug as well:

-Djavax.net.debug=all

If you want to integrate Log4j and java.util.logging, you can try using libraries like SLF4J and the corresponding bridges. This related SO question provides more information.

jccampanero
  • 50,989
  • 3
  • 20
  • 49
1

Remark: Since Log4j 1.2.8 is almost 20 years old, declared end-of-life 8 years ago and with several outstanding vulnerabilities, I don't believe you want to use it.

As you can see in jccampanero's answer, the HttpURLConnection class uses PlatformLogger for logging, which in Java 7 or 8 delegates all logging to java.util.logging.LogManager.

Several alternative implementations of java.util.logging.LogManager exist, but WildFly uses its own implementation called JBoss LogManager, which is the easiest to use. To modify your logging configuration, you just need to edit your server's configuration and add:

<subsystem xmlns="urn:jboss:domain:logging:3.0">
    ...
    <logger category="sun.net.www.protocol.http.HttpURLConnection">
        <level name="DEBUG"/>
    </logger>
</subsystem>

(cf. documentation).

Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43