Log4j: Issues about the FallbackErrorHandler

2019-07-14 03:38发布

I am working on a client-server-application and wanted to implement a flexible Loggingframework, so I chose log4j, which doesn´t really evolve anymore, but it is still handy framework.

Because the Logging happens along the network, i wanted a solution for the case, that the network drive isn´t available, so the Logger has to change its destination file(s).

Now I wanted to use the FallbackErrorHandler (configured with a XML-File) from the Log4j-library and the implementation worked: When my network drive isn´t available, it switches to a local Logfile, so no logging should be lost. But I headded two problems since yesterday and couldn´t figure or find out, how to solve it.

No return to initial Logging Configuration:

When the network drive is on again and the Logger could write to the old destinations, log4j still logs at the local drive and I can´t figure out, how to notify the original (primary) Logger to start again. I also tried to attach a second Appender to the ErrorHandler, which should mirror the failed primary Logger, that it tries to write on the network destination and when the network is on again, it logs in both files, on the local and on the network drive. But unfortunately it didn´t work out, I only got a failure message that the ErrorHandler-content doesn´t fit.

log4j:WARN The content of element type "errorHandler" must match "(param*,root-ref?,logger-ref*,appender-ref?)". 

This is the responsible code.

<appender name="TraceAppender" class="org.apache.log4j.DailyRollingFileAppender">
    <!-- The second appender-ref "TestAppender" leads to the error. -->
    <errorHandler class="org.apache.log4j.varia.FallbackErrorHandler">
        <logger-ref ref="com.idoh"/>
        <appender-ref ref="TraceFallbackAppender"/>
        <appender-ref ref="TestAppender"/>
    </errorHandler>
    <param name="datePattern" value=".yyyy-MM-dd" />
    <param name="file" value="logs/Trace.txt" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%-6r %d{HH:mm:ss,SSS} [%t] %-5p - %m%n"/>
    </layout>
</appender>

So, how could I trigger log4j to reset to initial configuration or hold a second appender parallel to the "Local-Logger". My Application should work by itself and shouldn´t have to be restarted often.

EDIT: Another related, but more general Question: After a disconnect, all my Fileappenders (which couldn´t write to there destination) stop working, even after the reconnect. The thrown error is:

log4j:ERROR Failed to flush writer,
java.io.IOException: Der angegebene Netzwerkname ist nicht mehr verfügbar
     (which means, that the stated networkname isn´t reachable)

The Error itself is quiet expectable, but that no Logger is working after this is not.

/EDIT

First Error message is swallowed:

I recognized, that the first message, which leads to the switching between the primary logger and the FallbackErrorHandler (for example a logging-request to a readonly-File), is swallowed, so neither the primary logger logs it (because it can´t) nor the backup-Logger knows what it missed. So anybody else ran in this problem and could solve it? Or has any suggestions?

标签: java log4j
0条回答
登录 后发表回答