我正在尝试从 log4j 进行日志记录以转到插件中的 Eclipse错误日志视图。
我有两个外部包:
- Bundle 1:基于现有 JAR 创建了一个插件开发项目,其中包含 log4j 库。
- Bundle 2:创建了一个空的插件开发项目。添加了一个 log4j.properties 和一个新类“VirtualConsole”,它扩展了“ConsoleAppender”。
这就是我的 log4j.properties 的样子:
# Set root logger level to debug and its only appender to default.
log4j.rootLogger=debug, default
# default is set to be a ConsoleAppender.
log4j.appender.default=VirtualConsole
# default uses PatternLayout.
log4j.appender.default.layout=org.apache.log4j.PatternLayout
log4j.appender.default.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
这就是我的 VirtualConsole 的样子:
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.statushandlers.StatusManager;
public class VirtualConsole extends ConsoleAppender {
@Override
public void append(LoggingEvent event) {
int level = IStatus.INFO;
if (event.getLevel().equals(Level.ERROR))
level = IStatus.ERROR;
IStatus status = new Status(level, "myplugin",
event.getMessage().toString());
StatusManager.getManager().handle(status, StatusManager.LOG);
//and the normal logging
super.append(event);
}
}
这行得通。我已经将我的第一个包作为依赖项包含在其他包中,当我通过“右键单击 > 运行为 > Eclipse 应用程序”从 Eclipse 中运行我的插件时,我可以运行我的插件,并且当我(故意)在哪里做某事时我现在正在记录,日志出现在它们应该出现的位置 - 在错误日志视图中。
一旦我导出我的插件,运行 Eclipse 的全新安装并通过“安装软件”系统安装插件,它就不再工作了......任何地方都没有出现。我无法让它工作,老实说,我看不出我哪里出错了。
任何人都可以给我一些指示?谢谢。
更新所以这就是现在一切的样子:
来自捆绑包“bundle.log4jProperties”的 Manifest.MF:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Log4jProperties
Fragment-Host: bundle.slf4j
Bundle-SymbolicName: bundle.log4jProperties
Bundle-Version: 1.0.1
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Require-Bundle: org.eclipse.core.runtime;bundle-version="3.7.0",
bundle.slf4j
Eclipse-RegisterBuddy: bundle.slf4j
捆绑包“bundle.log4jProperties”中的 log4j.properties 文件:
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
log4j.logger.org.hibernate.type=ERROR
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=bundle.log4jProperties.ErrorLogAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
包“bundle.log4jProperties”中的ErrorLogAppender.java(也在包“bundle.log4jProperties”中,在src文件夹中):
package bundle.log4jProperties;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
public class ErrorLogAppender extends AppenderSkeleton {
@Override
public void close() {}
@Override
public boolean requiresLayout() {
return false;
}
@Override
protected void append(LoggingEvent event) {
//get the platform log
ILog log = Platform.getLog(Platform.getBundle
("bundle.log4jProperties"));
//create an IStatus status
IStatus status = new Status(getLevel(event.getLevel()),
"myPlugin",
getCode(event),
getMessage(event),
getThrowable(event));
//log the status
log.log(status);
}
private int getLevel(Level level) {
int severity;
if (level.equals(Level.ALL) ||
level.equals(Level.ERROR) ||
level.equals(Level.FATAL))
severity = IStatus.ERROR;
else if (level.equals(Level.WARN))
severity = IStatus.WARNING;
else if (level.equals(Level.INFO))
severity = IStatus.INFO;
else severity = IStatus.INFO;
return severity;
}
private int getCode(LoggingEvent event) {
return (int) event.getTimeStamp();
}
private String getMessage(LoggingEvent event) {
return event.getMessage().toString();
}
private Throwable getThrowable(LoggingEvent event) {
ThrowableInformation info = event.getThrowableInformation();
if (info != null)
return info.getThrowable();
else return null;
}
}
转到捆绑包“bundle.slf4j”,其中包含 3 个 jar 文件:Manifest.MF:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Slf4j
Bundle-SymbolicName: bundle.slf4j
Bundle-Version: 1.0.1
Bundle-ClassPath: slf4j-api-1.7.5.jar,
slf4j-log4j12-1.7.5.jar,
log4j-1.2.17.jar
Bundle-Vendor: apache
Export-Package: org.apache.log4j,
org.apache.log4j.chainsaw,
org.apache.log4j.config,
org.apache.log4j.helpers,
org.apache.log4j.jdbc,
org.apache.log4j.jmx,
org.apache.log4j.lf5,
org.apache.log4j.lf5.util,
org.apache.log4j.lf5.viewer,
org.apache.log4j.lf5.viewer.categoryexplorer,
org.apache.log4j.lf5.viewer.configure,
org.apache.log4j.net,
org.apache.log4j.nt,
org.apache.log4j.or,
org.apache.log4j.or.jms,
org.apache.log4j.or.sax,
org.apache.log4j.pattern,
org.apache.log4j.rewrite,
org.apache.log4j.spi,
org.apache.log4j.varia,
org.apache.log4j.xml,
org.slf4j,
org.slf4j.helpers,
org.slf4j.impl,
org.slf4j.spi
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Eclipse-BuddyPolicy: registered
当从 Eclipse 内部作为 Eclipse 应用程序运行时仍然可以正常工作,但在部署到更新站点并从那里安装时无法正常工作。我认为这可能与找不到属性文件有关,但是如果我想找到该文件,我必须更改什么?
注意:我要导出到更新站点的 feature.xml 也包含此内容(截断并省略了不必要的内容):
<requires>
<import plugin="bundle.slf4j" version="1.0.1" match="greaterOrEqual"/>
</requires>
<plugin
id="bundle.slf4j"
download-size="0"
install-size="0"
version="0.0.0"/>
<plugin
id="bundle.log4jProperties"
download-size="0"
install-size="0"
version="0.0.0"
fragment="true"
unpack="false"/>
我无法将 bundle.log4jProperties 添加到所需的插件,因为这给了我无法解析该插件的警告。不知何故,我认为这与我的问题有关,但是我到底应该如何让我的 feature.xml(在包“功能”中,单独)“解决”另一个在同一工作区中工作的包?特别奇怪,因为相同的 feature.xml 确实在插件语句中旋转插件。
请,任何人。帮助。我正在为几乎所有插件中都完成的事情而苦苦挣扎,这真是太棒了,但我无法在任何地方找到解决方案。底线:我想将错误记录到错误日志视图中。这应该是可能的,对吧?