我有一个Eclipse E4应用程序,想要在其中添加一些自定义日志记录(带有Logback)。但是,当我通过应用程序生命周期的postContectCreate方法执行此操作时,它总是为我提供一个对话框,其中包含例外。我不希望该对话框,但应将日志写入日志文件。我该如何实现?
我尝试过:
@PostContextCreate
void postContextCreate(IEclipseContext workbenchContext) {
...
workbenchContext.set(IEventLoopAdvisor.class, new EclipseLogs());
}
public class EclipseLogs implements IEventLoopAdvisor {
private static final Logger logger = LoggerFactory.getLogger(EclipseLogs.class);
@Override
public void eventLoopException(Throwable e) {
logger.error(e.getMessage(), GlobalData.getInstance().getCurrentUserName(), e);
}
@Override
public void eventLoopIdle(Display display) {
display.sleep();
}
}
我的logback.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="TIME_BASED_FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logs/%d{yyyy-MM-dd}.log
</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} (%line\) -
%msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n
</pattern>
</encoder>
</appender>
<root level="ALL">
<appender-ref ref="STDOUT" />
<appender-ref ref="TIME_BASED_FILE" />
</root>
<root level="ERROR">
<appender-ref ref="org.eclipse.jetty.websocket.LEVEL" />
</root>
</configuration>
从Eclipse本身内部运行时,我想在控制台终端中引发异常,但是当作为独立应用程序运行时,应将异常写入日志文件。