How to use log4j to see into Jersey
Asked Answered
M

5

7

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.

Marvin answered 15/3, 2013 at 16:7 Comment(2)
Do you see other logging information? Not sure about your logging problem but your Jersey problem is most likely that you are missing accept or content-type header in your request and your service requires a specific one.Inhabited
No, I don't see anything about the request. That's why I'm trying to enable logging for it. :-\Marvin
M
-5

I couldn't get logging to work, but I was able to determine how to get the ContentType which is what I needed. I had to add this to my class...

@Context
UriInfo uriInfo;

@PostConstruct
public void myfunc() {
    if (uriInfo == null) { //breakpoint on this line, so I can see what uriInfo is

    }
}
Marvin answered 15/3, 2013 at 17:24 Comment(1)
This does not answer the question "How to use log4j to see into Jersey?" in any way.Chophouse
M
10

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
Minacious answered 15/3, 2013 at 16:51 Comment(4)
Thanks for the feedback. I only enabled log4j because I thought it would offer the ability for me to debug jersey better. How can I view more details about the request without using log4j? My concern is that currently NOTHING shows up in the application console from any kind of logging when the system throws the 415 header.Marvin
I have a debugger attached, and it shows nothing. I thought log4j might provide more debugging/logging options.Marvin
I have updated my answer as I suspected you just wanted to do some quick-and-dirty debugging, in which case just work with Java logging directly.Minacious
I did attempt to add these lines to logging.properties and used the ConsoleHandler instead of FileHandler, but didn't see any output in my log. I found a workaround, thanks for the help. +1Marvin
M
6

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
Mucilage answered 4/9, 2014 at 12:56 Comment(2)
+1 for the static initializer for configuring the SLF4JBridgeHandler which in our case means less moving partsAstute
Great answer, should be the checked oneChemaram
N
0

you can see on here it's run you must configure your

  • web.xml
  • log4j.properties
  • Log4JInitServlet.java
  • Log4JTestServlet.java
Nordine answered 18/4, 2013 at 11:15 Comment(0)
T
0

I followed the advice in this answer to create a custom logging feature class and register it:

https://mcmap.net/q/1480609/-how-to-print-server-responses-using-loggingfeature-in-dropwizard-1-0-2

Then modified the logger that it's using to be log4j.

Tattle answered 29/7, 2022 at 16:53 Comment(0)
M
-5

I couldn't get logging to work, but I was able to determine how to get the ContentType which is what I needed. I had to add this to my class...

@Context
UriInfo uriInfo;

@PostConstruct
public void myfunc() {
    if (uriInfo == null) { //breakpoint on this line, so I can see what uriInfo is

    }
}
Marvin answered 15/3, 2013 at 17:24 Comment(1)
This does not answer the question "How to use log4j to see into Jersey?" in any way.Chophouse

© 2022 - 2024 — McMap. All rights reserved.