问题
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.
回答1:
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:
specify the logging properties file
-Djava.util.logging.config.file=/path/to/logging.propertiesadd the
SLF4JBridgeHandleras a logging handler in thelogging.propertiesfilehandlers=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:
attach a debugger to your application (assuming you have the application source code);
or work with Java logging directly. To configure Java logging to output to a file, add the following to your
logging.propertiesfile 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
回答2:
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
回答3:
you can see on here it's run you must configure your
- web.xml
- log4j.properties
- Log4JInitServlet.java
- Log4JTestServlet.java
回答4:
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
}
}
来源:https://stackoverflow.com/questions/15437043/how-to-use-log4j-to-see-into-jersey