java 如何使用 log4j 查看 Jersey
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15437043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use log4j to see into Jersey
提问by Webnet
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.
我是 log4j 的新手,正在尝试使用它来更好地理解为什么我的资源提供415 Media Type Not Supported
标题。
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 seemslike 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.
这似乎应该可以工作,但我没有在控制台中显示有关 POST 到达此应用程序的任何内容。我知道它正在到达应用程序,因为如果我关闭应用程序,POSTer 在尝试 POST 时会抛出“连接被拒绝”。
采纳答案by Webnet
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...
我无法让日志记录工作,但我能够确定如何获取我需要的 ContentType。我不得不把这个加到我的课上...
@Context
UriInfo uriInfo;
@PostConstruct
public void myfunc() {
if (uriInfo == null) { //breakpoint on this line, so I can see what uriInfo is
}
}
回答by pestrella
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.
恐怕这不像将 Jersey 包添加到您的 Log4J 配置那么简单。Jersey 在内部不使用 Log4J;相反,它使用Java 日志记录。
If you prefer to work with Log4J then your option is to configure Java logging to use the SLF4JBridgeHandleras a logging handler.
如果您更喜欢使用 Log4J,那么您的选择是配置 Java 日志记录以使用SLF4JBridgeHandler作为日志记录处理程序。
Typically this is done by specifying a logging properties file via JVM property, and adding handlers to the properties file, like so:
通常,这是通过通过 JVM 属性指定日志记录属性文件并将处理程序添加到属性文件来完成的,如下所示:
specify the logging properties file
-Djava.util.logging.config.file=/path/to/logging.properties
add the
SLF4JBridgeHandler
as a logging handler in thelogging.properties
filehandlers=org.slf4j.bridge.SLF4JBridgeHandler
指定日志属性文件
-Djava.util.logging.config.file=/path/to/logging.properties
SLF4JBridgeHandler
在logging.properties
文件中添加作为日志处理程序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.
然后 SLF4J 可以绑定到 Log4J,然后您可以log4j.properties
像往常一样使用您指定 Jersey 类的日志记录级别。
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.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
将调试器附加到您的应用程序(假设您有应用程序源代码);
或直接使用 Java 日志记录。要将Java 日志记录配置为输出到文件,请将以下内容添加到您
logging.properties
之前指定的文件中——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
回答by Thunder
You can use JUL to SLF4J Bridge
for this first add it to your classpath. You can use this if you are using maven:
您可以JUL to SLF4J Bridge
首先将其添加到您的类路径中。如果您使用的是 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.properties
文件控制球衣日志:
log4j.category.org.glassfish.jersey=WARN,stdout