在日志文件中记录JavaFX 8异常

时间:2014-07-09 19:13:04

标签: logging javafx logback javafx-8

有时JavaFX 8主线程会向控制台抛出一些特定的异常,但是我无法将这些异常重定向到我的日志文件中。无论我做什么,我都无法将异常记录到日志文件中,例外情况如下:

    at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
    at java.lang.Thread.run(Thread.java:744)
java.lang.IllegalArgumentException: Key already associated with a running event
loop: com.lutum.ui.controladores.dialogos.ControladorDialogoMensagem@4da2b8
    at com.sun.javafx.tk.quantum.QuantumToolkit.enterNestedEventLoop(Quantum
Toolkit.java:537)
    at com.lutum.ui.controladores.dialogos.ControladorDialogoInterno.abrirDi
alogoEEsperar(ControladorDialogoInterno.java:194)
    at com.lutum.ui.controladores.dialogos.ControladorDialogoInterno.abrirEE
sperar(ControladorDialogoInterno.java:173)
    at com.lutum.ui.controladores.dialogos.ControladorDialogoMensagem.exibir
Mensagem(ControladorDialogoMensagem.java:73)
    at com.lutum.ui.controladores.dialogos.ControladorDialogoMensagem.exibir
Alerta(ControladorDialogoMensagem.java:49)
    at com.lutum.lumen.caixa.controladores.gui.paineis.venda.cupom.Controlad
orPainelCupomFiscal$1.changed(ControladorPainelCupomFiscal.java:101)
    at com.lutum.lumen.caixa.controladores.gui.paineis.venda.cupom.Controlad
orPainelCupomFiscal$1.changed(ControladorPainelCupomFiscal.java:1)
    at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent
(ExpressionHelper.java:347)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Express
ionHelper.java:80)
    at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(Object
PropertyBase.java:105)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBa
se.java:112)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:
145)
    at jfxtras.labs.scene.control.BigDecimalField.setNumber(BigDecimalField.
java:174)
    at jfxtras.labs.scene.control.BigDecimalField.increment(BigDecimalField.
java:135)
    at jfxtras.labs.internal.scene.control.skin.BigDecimalFieldSkin.lambda$c
reateNodes$68(BigDecimalFieldSkin.java:122)
    at jfxtras.labs.internal.scene.control.skin.BigDecimalFieldSkin$$Lambda$
7/29282012.handle(Unknown Source)
    at com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(Time
lineClipCore.java:231)
    at com.sun.scenario.animation.shared.TimelineClipCore.playTo(TimelineCli
pCore.java:172)
    at javafx.animation.Timeline.impl_playTo(Timeline.java:161)
    at javafx.animation.AnimationAccessorImpl.playTo(AnimationAccessorImpl.j
ava:39)
    at com.sun.scenario.animation.shared.InfiniteClipEnvelope.timePulse(Infi
niteClipEnvelope.java:110)
    at javafx.animation.Animation.impl_timePulse(Animation.java:1104)
    at javafx.animation.Animation$1$1.run(Animation.java:187)
    at javafx.animation.Animation$1$1.run(Animation.java:185)
    at java.security.AccessController.doPrivileged(Native Method)
    at javafx.animation.Animation$1.timePulse(Animation.java:185)
    at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(Abstract
MasterTimer.java:346)
    at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractM
asterTimer.java:269)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:47
5)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:46
0)
    at com.sun.javafx.tk.quantum.QuantumToolkit$13.run(QuantumToolkit.java:3
27)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatch
er.java:95)
    at com.sun.glass.ui.win.WinApplication._enterNestedEventLoopImpl(Native
Method)
    at com.sun.glass.ui.win.WinApplication._enterNestedEventLoop(WinApplicat
ion.java:142)
    at com.sun.glass.ui.Application.enterNestedEventLoop(Application.java:49
5)
    at com.sun.glass.ui.EventLoop.enter(EventLoop.java:107)
    at com.sun.javafx.tk.quantum.QuantumToolkit.enterNestedEventLoop(Quantum
Toolkit.java:543)
    at com.lutum.ui.controladores.dialogos.ControladorDialogoInterno.abrirDi
alogoEEsperar(ControladorDialogoInterno.java:194)
    at com.lutum.ui.controladores.dialogos.ControladorDialogoInterno.abrirEE
sperar(ControladorDialogoInterno.java:173)
    at com.lutum.ui.controladores.dialogos.ControladorDialogoMensagem.exibir
Mensagem(ControladorDialogoMensagem.java:73)
    at com.lutum.ui.controladores.dialogos.ControladorDialogoMensagem.exibir
Alerta(ControladorDialogoMensagem.java:49)
    at com.lutum.lumen.caixa.controladores.gui.paineis.venda.cupom.Controlad
orPainelCupomFiscal$1.changed(ControladorPainelCupomFiscal.java:101)
    at com.lutum.lumen.caixa.controladores.gui.paineis.venda.cupom.Controlad
orPainelCupomFiscal$1.changed(ControladorPainelCupomFiscal.java:1)
    at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent
(ExpressionHelper.java:347)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Express
ionHelper.java:80)
    at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(Object
PropertyBase.java:105)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBa
se.java:112)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:
145)
    at jfxtras.labs.scene.control.BigDecimalField.setNumber(BigDecimalField.
java:174)
    at jfxtras.labs.scene.control.BigDecimalField.increment(BigDecimalField.
java:135)
    at jfxtras.labs.internal.scene.control.skin.BigDecimalFieldSkin.lambda$c
reateNodes$68(BigDecimalFieldSkin.java:122)
    at jfxtras.labs.internal.scene.control.skin.BigDecimalFieldSkin$$Lambda$
7/29282012.handle(Unknown Source)
    at jfxtras.labs.internal.scene.control.skin.BigDecimalFieldSkin.lambda$c
reateNodes$69(BigDecimalFieldSkin.java:130)
    at jfxtras.labs.internal.scene.control.skin.BigDecimalFieldSkin$$Lambda$
8/1347352.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Comp
ositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventH
andlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventH
andlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(C
ompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:204)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3746)
    at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotificatio
n.run(GlassViewEventHandler.java:314)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotificatio
n.run(GlassViewEventHandler.java:243)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Glas
sViewEventHandler.java:345)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
    at com.sun.glass.ui.View.notifyMouse(View.java:898)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39
)
    at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
    at java.lang.Thread.run(Thread.java:744)
java.lang.IllegalArgumentException: Key already associated with a running event
loop: com.lutum.ui.controladores.dialogos.ControladorDialogoMensagem@4da2b8
    at com.sun.javafx.tk.quantum.QuantumToolkit.enterNestedEventLoop(Quantum
Toolkit.java:537)
    at com.lutum.ui.controladores.dialogos.ControladorDialogoInterno.abrirDi
alogoEEsperar(ControladorDialogoInterno.java:194)
    at com.lutum.ui.controladores.dialogos.ControladorDialogoInterno.abrirEE
sperar(ControladorDialogoInterno.java:173)
    at com.lutum.ui.controladores.dialogos.ControladorDialogoMensagem.exibir
Mensagem(ControladorDialogoMensagem.java:73)
    at com.lutum.ui.controladores.dialogos.ControladorDialogoMensagem.exibir
Alerta(ControladorDialogoMensagem.java:49)
    at com.lutum.lumen.caixa.controladores.gui.paineis.venda.cupom.Controlad
orPainelCupomFiscal$1.changed(ControladorPainelCupomFiscal.java:101)
    at com.lutum.lumen.caixa.controladores.gui.paineis.venda.cupom.Controlad
orPainelCupomFiscal$1.changed(ControladorPainelCupomFiscal.java:1)
    at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent
(ExpressionHelper.java:347)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Express
ionHelper.java:80)
    at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(Object
PropertyBase.java:105)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBa
se.java:112)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:
145)
    at jfxtras.labs.scene.control.BigDecimalField.setNumber(BigDecimalField.
java:174)
    at jfxtras.labs.scene.control.BigDecimalField.increment(BigDecimalField.
java:135)
    at jfxtras.labs.internal.scene.control.skin.BigDecimalFieldSkin.lambda$c
reateNodes$68(BigDecimalFieldSkin.java:122)
    at jfxtras.labs.internal.scene.control.skin.BigDecimalFieldSkin$$Lambda$
7/29282012.handle(Unknown Source)
    at com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(Time
lineClipCore.java:231)
    at com.sun.scenario.animation.shared.TimelineClipCore.playTo(TimelineCli
pCore.java:172)
    at javafx.animation.Timeline.impl_playTo(Timeline.java:161)
    at javafx.animation.AnimationAccessorImpl.playTo(AnimationAccessorImpl.j
ava:39)
    at com.sun.scenario.animation.shared.InfiniteClipEnvelope.timePulse(Infi
niteClipEnvelope.java:110)
    at javafx.animation.Animation.impl_timePulse(Animation.java:1104)
    at javafx.animation.Animation$1$1.run(Animation.java:187)
    at javafx.animation.Animation$1$1.run(Animation.java:185)
    at java.security.AccessController.doPrivileged(Native Method)
    at javafx.animation.Animation$1.timePulse(Animation.java:185)
    at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(Abstract
MasterTimer.java:346)
    at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractM
asterTimer.java:269)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:47
5)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:46
0)
    at com.sun.javafx.tk.quantum.QuantumToolkit$13.run(QuantumToolkit.java:3
27)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatch
er.java:95)
    at com.sun.glass.ui.win.WinApplication._enterNestedEventLoopImpl(Native
Method)
    at com.sun.glass.ui.win.WinApplication._enterNestedEventLoop(WinApplicat
ion.java:142)
    at com.sun.glass.ui.Application.enterNestedEventLoop(Application.java:49
5)
    at com.sun.glass.ui.EventLoop.enter(EventLoop.java:107)
    at com.sun.javafx.tk.quantum.QuantumToolkit.enterNestedEventLoop(Quantum
Toolkit.java:543)
    at com.lutum.ui.controladores.dialogos.ControladorDialogoInterno.abrirDi
alogoEEsperar(ControladorDialogoInterno.java:194)
    at com.lutum.ui.controladores.dialogos.ControladorDialogoInterno.abrirEE
sperar(ControladorDialogoInterno.java:173)
    at com.lutum.ui.controladores.dialogos.ControladorDialogoMensagem.exibir
Mensagem(ControladorDialogoMensagem.java:73)
    at com.lutum.ui.controladores.dialogos.ControladorDialogoMensagem.exibir
Alerta(ControladorDialogoMensagem.java:49)
    at com.lutum.lumen.caixa.controladores.gui.paineis.venda.cupom.Controlad
orPainelCupomFiscal$1.changed(ControladorPainelCupomFiscal.java:101)
    at com.lutum.lumen.caixa.controladores.gui.paineis.venda.cupom.Controlad
orPainelCupomFiscal$1.changed(ControladorPainelCupomFiscal.java:1)
    at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent
(ExpressionHelper.java:347)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Express
ionHelper.java:80)
    at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(Object
PropertyBase.java:105)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBa
se.java:112)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:
145)
    at jfxtras.labs.scene.control.BigDecimalField.setNumber(BigDecimalField.
java:174)
    at jfxtras.labs.scene.control.BigDecimalField.increment(BigDecimalField.
java:135)
    at jfxtras.labs.internal.scene.control.skin.BigDecimalFieldSkin.lambda$c
reateNodes$68(BigDecimalFieldSkin.java:122)
    at jfxtras.labs.internal.scene.control.skin.BigDecimalFieldSkin$$Lambda$
7/29282012.handle(Unknown Source)
    at jfxtras.labs.internal.scene.control.skin.BigDecimalFieldSkin.lambda$c
reateNodes$69(BigDecimalFieldSkin.java:130)
    at jfxtras.labs.internal.scene.control.skin.BigDecimalFieldSkin$$Lambda$
8/1347352.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Comp
ositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventH
andlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventH
andlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(C
ompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:204)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3746)
    at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotificatio
n.run(GlassViewEventHandler.java:314)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotificatio
n.run(GlassViewEventHandler.java:243)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Glas
sViewEventHandler.java:345)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
    at com.sun.glass.ui.View.notifyMouse(View.java:898)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39
)
    at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
    at java.lang.Thread.run(Thread.java:744)

我的logback.xml以这种方式配置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="true" scanPeriod="30 seconds">
    <appender name="caixa" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/caixa.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>logs/caixa-%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%date{dd/MM/yyyy - HH:mm:ss} [%level] - %msg%n %ex{full}</pattern>
        </encoder>
    </appender>
    <root level="warn">
        <appender-ref ref="caixa"/>
    </root>
</configuration>

我做了什么:

我尝试使用SLF4JBridgeHandler进行回溯。 还尝试在JavaFXApplicationThread中设置defaultUncaughtExceptionHandler:

Platform.runLater(new Runnable() {
            @Override
            public void run() {
                Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
                    @Override
                    public void uncaughtException(Thread t, Throwable e) {
                        logger.error("Exceção não capturada: " + t, e);
                        if (e instanceof UnsatisfiedLinkError) {
                            ControladorDialogoMensagem dialogo = contextoEstatico.getBean(ControladorDialogoMensagem.class);
                            dialogo.exibirErro("Não foi possível carregar a biblioteca do ECF!", "Verifique o log para mais detalhes.");
                            System.exit(1);
                        }
                    }
                });
            }
        });

最后我尝试在Logger中添加一个FileHandler:

Handler fh = new FileHandler("c:/teste.log");
java.util.logging.Logger.getLogger("").addHandler(fh);
java.util.logging.Logger.getLogger("").setLevel(Level.WARNING);

如何强制将j​​avafx异常记录在文件中?

1 个答案:

答案 0 :(得分:1)

我不确定设置defaultUncaughtExceptionHandler会出现什么问题。对我来说,主应用程序类中的以下代码片段正确地将这些异常重定向到SLF4J。

public class MainApp extends Application {
  @Override
  public void init() throws Exception {
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
      @Override
      public void uncaughtException(Thread t, Throwable e) {
        LoggerFactory.getLogger(MainApp.class).error(
            "Exception in thread \"" + t.getName() + "\"", e);
      }
    });
    super.init();
  }
}

也许Platform.runLater太迟了你的任务?