28

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.

bobbel
  • 3,327
  • 2
  • 26
  • 43
Sameek Mishra
  • 9,174
  • 31
  • 92
  • 118

7 Answers7

29

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.

bobbel
  • 3,327
  • 2
  • 26
  • 43
satti
  • 506
  • 4
  • 6
26

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.

Community
  • 1
  • 1
manikanta
  • 8,100
  • 5
  • 59
  • 66
  • 6
    I don't see how that solves the original problem asked here, since they don't have multiple files, and they don't have multiple appenders defined with the same name. – Marcus Dec 20 '13 at 17:49
  • 1
    I had the same problem, one log4j in my application and another one inside tomcat/lib. Thanks for this answer!! – Capitan Empanada Dec 02 '19 at 23:42
8

One could overwrite the configuration using:

BasicConfigurator.resetConfiguration();
PropertyConfigurator.configure(props);
Dani
  • 3,744
  • 4
  • 27
  • 35
Mike
  • 20,010
  • 25
  • 97
  • 140
  • Marked this as "my" correct solution because my problem was that I have several potential application entry points, and it's very difficult to refactor it to have a single entry point. I was obtaining the same "Attempted to append to closed appender" error, but the reason was different. Restoring Log4J configuration prior to configuring it again each time has worked for me :-D Thanks! – Víctor Apr 13 '18 at 09:24
1

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"

1

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.

Brad Parks
  • 66,836
  • 64
  • 257
  • 336
1

In my case, I added by mistake 2 "root" elements.

jpprade
  • 3,497
  • 3
  • 45
  • 58
0

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.

MaDa
  • 10,511
  • 9
  • 46
  • 84