Java 登录 Eclipse/OSGi 插件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/223875/
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
Logging in Eclipse/OSGi plugins
提问by rcreswick
I am starting to develop an Eclipse plugin (technically, an OSGi plugin) and one of the first problems I've run into is that I can't seem to control the commons-logging output as I normally would.
我开始开发一个 Eclipse 插件(从技术上讲,一个 OSGi 插件),我遇到的第一个问题是我似乎无法像往常一样控制 commons-logging 输出。
I've included the commons-logging package in the plugin dependencies, and indeed, when I log something (at INFO or higher severity) it is logged to the console. However, I can't seem to log at any lower level (such as DEBUG or TRACE).
我已经在插件依赖项中包含了 commons-logging 包,实际上,当我记录某些内容(以 INFO 或更高的严重性)时,它会记录到控制台。但是,我似乎无法在任何较低级别(例如 DEBUG 或 TRACE)进行登录。
I have specified a log4j.properties file, and it is on the classpath (for the runtime, just as the commons-logging package is) but none of the settings in that properties file have any impact on the behavior of the logger.
我已经指定了一个 log4j.properties 文件,它在类路径上(对于运行时,就像 commons-logging 包一样),但该属性文件中的任何设置都不会对记录器的行为产生任何影响。
Here's the log4j.properties file:
这是 log4j.properties 文件:
# Log4j Logging levels, in order of decreasing importance are:
# FATAL, ERROR, WARN, INFO, DEBUG, TRACE
#
# Root logger option
log4j.rootLogger=ERROR,stdout
#,LOGFILE
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %r (%l) %t%n - %m%n
What do I need to do so that I can actually control the output of the logger?
我需要做什么才能真正控制记录器的输出?
Here are some sample output messages, in the hopes that the formatting may coincide with a default for java.util.logging, or provide other hints to someone:
以下是一些示例输出消息,希望格式可以与 java.util.logging 的默认值一致,或者向某人提供其他提示:
Oct 21, 2008 11:01:23 PM com.stottlerhenke.sentinel.client.Activator start
SEVERE: fatal_message
Oct 21, 2008 11:01:23 PM com.stottlerhenke.sentinel.client.Activator start
WARNING: warn_message
Oct 21, 2008 11:01:23 PM com.stottlerhenke.sentinel.client.Activator start
INFO: info_message
Update:
更新:
I have now tried various combinations of:
我现在尝试了以下各种组合:
- org.osgi.service.log.LogService et al.
- slf4j
- Log4J
- Commons-logging
- java.util.logging
- org.osgi.service.log.LogService 等。
- slf4j
- 日志4J
- 共享日志
- java.util.logging
and I can onlyget DEBUG, or lower, level messages to appear if I am running OSGi manually from a prompt (which is impractical for what I am developing). Furthermore, I can't effect any other type of logging configuration via various properties files. Everything I try in that regard seems to be overridden by an eclipse setting.
如果我从提示手动运行 OSGi(这对我正在开发的内容来说是不切实际的),我只能显示DEBUG 或更低级别的消息。此外,我无法通过各种属性文件影响任何其他类型的日志记录配置。我在这方面尝试的一切似乎都被日食设置所覆盖。
I've also tried putting various config files for the above libraries in numerous places, including as plug-in fragments attached to their respective libraries as suggested here, and still, the same result happens.
我还尝试将上述库的各种配置文件放在许多地方,包括作为此处建议的附加到各自库的插件片段,但仍然发生了相同的结果。
I've implemented a custom LogListener, and traced the entire path of a log message (as well as I know how, anyway) with System.out.println's, and debug messages arepresent right up until they are output by whatever underlying logging API I'm using, then they disappear.
我已经实现了一个自定义 LogListener,并使用 System.out.println 跟踪了日志消息的整个路径(以及我知道的方法),并且调试消息一直存在,直到它们被任何底层日志 API 输出我正在使用,然后它们消失了。
采纳答案by rcreswick
3 days later...
3天后...
I found the problem! There were two things I needed to do, first off, there was a problem with one MANIFEST.MF file:
我发现了问题!我需要做两件事,首先,一个 MANIFEST.MF 文件有问题:
I had the following in the MANIFEST.MF for one bundle:
对于一个捆绑包,我在 MANIFEST.MF 中有以下内容:
Bundle-ClassPath: lib/jena.jar,
.,
org.apache.log4j-1.2.12.jar,
lib/google-collect-snapshot.jar
Import-Package: com.acme.client.translation,
com.acme.translation.interfaces,
com.acme.shared.osgi,
com.acme.utilities
That shouldhave been this:
那应该是这样的:
Bundle-ClassPath: lib/jena.jar,
.,
lib/google-collect-snapshot.jar
Import-Package: com.acme.client.translation,
com.acme.client.translation.interfaces,
com.acme.shared.osgi,
com.acme.utilities,
org.apache.log4j
The key difference is that the log4j was being used as a package, when it should have been used as a bundle. (I had a log4j jar in my lib dir from when I had expected Log4j to "just work" with OSGi.) The jar doeswork, sort-of. It evidently found some eclipse-level log4j configuration, and made use of that. Since it was just a jar (not a bundle) it didn't make use of any fragments that could specify a custom logging config, which leads us to the other thing that had to happen:
关键的区别在于 log4j 被用作包,而它应该被用作包。(我的 lib 目录中有一个 log4j jar,当时我预计 Log4j 可以与 OSGi“正常工作”。)这个 jar确实可以工作,有点。它显然找到了一些 eclipse 级别的 log4j 配置,并利用了它。由于它只是一个 jar(不是包),它没有使用任何可以指定自定义日志配置的片段,这导致我们不得不发生的另一件事:
I needed to set up a bundle fragment to specify the logging config. This linkfrom VonCgave me the info to do that. That entailed doing a number of things, unfortunately, the package with the incorrect MANIFEST.MF still had the log4j jar specified in the Bundle-ClassPath, and that seems to override the Import-Package list.
我需要设置一个包片段来指定日志配置。 此链接从VonC给我的信息,以做到这一点。这需要做很多事情,不幸的是,带有错误 MANIFEST.MF 的包仍然在 Bundle-ClassPath 中指定了 log4j jar,这似乎覆盖了 Import-Package 列表。
I finally figured out what was going on when I needed to log in another bundle (I had just given up at this point, and went back to using logs at the Warn level and higher.) This new bundle couldn't find a logging config! (so then I had three bundles running in the same OSGi environment, each with different log4j behavior -- one using my fragment settings, another using some random Eclipse logging settings, and finally the new bundle that didn't have any logging config.) Detailed comparisons of these three bundles revealed the difference in the Manifest.MF files, and now they all use the fragment bundle.
我终于弄清楚当我需要登录另一个包时发生了什么(此时我刚刚放弃,并回到使用警告级别和更高级别的日志。)这个新包找不到日志配置!(所以我在同一个 OSGi 环境中运行了三个包,每个包都有不同的 log4j 行为——一个使用我的片段设置,另一个使用一些随机的 Eclipse 日志设置,最后是没有任何日志配置的新包。)这三个包的详细比较揭示了 Manifest.MF 文件的差异,现在它们都使用片段包。
I owe a hugethanks to the authors of much of Eclipse Zone, VonC, Ekkes, and everyone in #eclipse on freenode for their help and patience :)
我非常感谢Eclipse Zone、VonC、Ekkes以及 freenode 上#eclipse 中的每个人的帮助和耐心:)
回答by VonC
This is not an actual answer to your question, but you might find some clues in this set of articles by ekke.
这不是您问题的实际答案,但您可能会在ekke 的这组文章中找到一些线索。
I suppose you read already "Using Log4J in Eclipse Equinox/OSGi":
我想您已经阅读过“在 Eclipse Equinox/OSGi 中使用 Log4J”:
Did you launch an osgi session in a console mode ?
您是否在控制台模式下启动了 osgi 会话?
java -jar org.eclipse.osgi_3.3.0.v20070530.jar -console -noExit -clean
That way, you may test log4j in a pure osgi environment and check if it works there.
这样,您可以在纯 osgi 环境中测试 log4j 并检查它是否在那里工作。
Let use know if you find a solution (publish it as an answer), and I will vote it up ;)
如果您找到解决方案,请告诉使用(将其发布为答案),我会投票给它;)