Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use log4j to see into Jersey

Tags:

java

jersey

log4j

I'm new to log4j and am trying to use it to better understand why my resource is providing a 415 Media Type Not Supported header.

I'm using the following:

log4j.rootCategory=WARN, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c:%L - %m%n

log4j.category.com.sun.jersey=DEBUG,stdout

This seems like it should work but I'm showing nothing in the console about the POST reaching this application. I know it is reaching the application because if I turn the application off, the POSTer throws a "connection refused" when trying to POST.

like image 907
Webnet Avatar asked Mar 15 '13 16:03

Webnet


People also ask

How do I view Log4j in a project?

Navigate into the "META-INF" sub-directory and open the file "MANIFEST. MF" in a text editor. Find the line starting with "Implementation-Version", this is the Log4j version.

How do I view Log4j logs?

The Log4j logging settings are stored in the file app_data /conf/server/log4j. properties, where app_data is the application data folder. You can edit this file directly on the server or open it by clicking Settings > Logging.

How do I know if an application uses Log4j?

Scan for Log4j with open source tools Syft generates a software bill of materials (SBOM) and Grype is a vulnerability scanner. Both of these tools are able to inspect multiple nested layers of JAR archives to uncover and identify versions of Log4j.


2 Answers

I'm afraid it's not as straightforward as adding the Jersey packages to your Log4J config. Jersey does not use Log4J internally; instead it uses Java logging.

If you prefer to work with Log4J then your option is to configure Java logging to use the SLF4JBridgeHandler as a logging handler.

Typically this is done by specifying a logging properties file via JVM property, and adding handlers to the properties file, like so:

  1. specify the logging properties file

    -Djava.util.logging.config.file=/path/to/logging.properties

  2. add the SLF4JBridgeHandler as a logging handler in the logging.properties file

    handlers=org.slf4j.bridge.SLF4JBridgeHandler

SLF4J can then bind to Log4J, and you can then use your log4j.properties as usual to specify the logging level for Jersey classes.

As an aside, my advice -- if you're just doing this for quick-and-dirty debugging -- is to either:

  1. attach a debugger to your application (assuming you have the application source code);

  2. or work with Java logging directly. To configure Java logging to output to a file, add the following to your logging.properties file as specified earlier --

    com.sun.jersey.level=DEBUG
    com.sun.jersey.handlers=java.util.logging.FileHandler
    java.util.logging.FileHandler.pattern=/path/to/debugging.log
    java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
like image 144
pestrella Avatar answered Sep 21 '22 18:09

pestrella


You can use JUL to SLF4J Bridge for this first add it to your classpath. You can use this if you are using maven:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jul-to-slf4j</artifactId>
    <version>1.7.5</version>
</dependency>

Add those lines to a static initializer block or in your main method:

LogManager.getLogManager().reset();
SLF4JBridgeHandler.install();

Now you can control the jersey logs from your log4j.properties file:

log4j.category.org.glassfish.jersey=WARN,stdout
like image 24
Thunder Avatar answered Sep 17 '22 18:09

Thunder