log4j:ERROR Attempted to append to closed appender

2019-04-03 04:47发布

问题:

I am getting following errors on my console repeatedly

log4j:ERROR Attempted to append to closed appender named [ConsoleAppender].
log4j:ERROR Attempted to append to closed appender named [FixedWindowRollingFile].

used log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
    <appender class="org.apache.log4j.rolling.RollingFileAppender" name="FixedWindowRollingFile">
        <param name="Append" value="true"/>
        <param name="ImmediateFlush" value="true"/>

        <rollingPolicy class="org.apache.log4j.rolling.FixedWindowRollingPolicy">
            <param name="fileNamePattern" value="logs/StandardizeAccountService.%i.log"/>
            <param name="minIndex" value="1"/>
            <param name="maxIndex" value="10"/>
        </rollingPolicy>
        <triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
            <param name="MaxFileSize" value="1002400"/>
        </triggeringPolicy>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %p %c{1}:%L - %m%n"/>
        </layout>
    </appender>

    <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.SimpleLayout"/>
    </appender>

    <logger name="com.arosys" additivity="false" >
        <level value="INFO" />
        <appender-ref ref="ConsoleAppender" />
        <appender-ref ref="FixedWindowRollingFile"/>
    </logger>

    <root>
        <priority value="INFO"/>
        <appender-ref ref="ConsoleAppender"/>
        <appender-ref ref="FixedWindowRollingFile"/>
    </root>
</log4j:configuration>

please help me where the problem.

回答1:

I've answered similar question here: https://stackoverflow.com/a/9973283/340290

In my case, I've two log4j.properties available to the Log4J: one via placing it in classpath and other being loaded programmatically (using PropertyConfigurator.configure(..)).

And in the two files, I've ConsoleAppender registered with same name stdout and used for same category twice (one per each properties file). Removing config or the properties file solved my issue.



回答2:

I got the same error:

log4j:ERROR Attempted to append to closed appender named [rollingFileAppender].

In my log4j.xml

I have two loggers with the same name like below

<logger name="java.sql.PreparedStatement" additivity="false">
    <level value="INFO"/>
    <appender-ref ref="rollingFileAppender"/>
</logger>

 <logger name="java.sql.PreparedStatement">
    <level value="INFO"/>
    <appender-ref ref="rollingFileAppender"/>
</logger>

I removed the duplicate, it worked.



回答3:

One could overwrite the configuration using:

BasicConfigurator.resetConfiguration();
PropertyConfigurator.configure(props);


回答4:

I'm not saying this is the cause of the behaviour you describe (but it might), but this part:

<logger name="com.arosys" additivity="false" >
   <level value="INFO" />
   <appender-ref ref="ConsoleAppender" />
   <appender-ref ref="FixedWindowRollingFile"/>
</logger>

is pointless. Normally, you'd set a non-additive logger to redirect it somewhere else than the default place (your root logger), but you still send the output to the default place. You might as well delete this fragment.



回答5:

Just to clarify because I was mislead by MaDa answer, additivity=false redirects the output to another place than the default (root logger) and NOT to the default. See http://logging.apache.org/log4j/1.2/manual.html chapter "Appenders and Layouts"



回答6:

This may also mean that you already have your server running, and you're trying to run it again. The second instance can't write to the log file because it's already open in your server.

solution: check to see if your server is already running, and restart it if neccessary.



标签: log4j