1

New job, new language...java this time. I'm working on integrating log4j into a new product and have after some issues managed to get it sort of working but a problem remain and I'm not sure how to go about solving them.

Using log4j 1.2.17 with log4j-extras 1.2.17 for RollingFileAppender.

Problem

It's not rolling. I wanted to set it to rolling every minute while developing this to check that everything works, but all I get it logs/stuff.log.

LogTest.java

package logTest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class LogTest {

   static Logger logger = LogManager.getLogger(LogTest.class);

   public static void main(String[] args) {
      DOMConfigurator.configure("log4j.xml");

      logger.trace("trace");
      logger.debug("debug");
      logger.info("info");
      logger.warn("warn");
      logger.error("error");
   }
}

log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
                     xmlns:log4j='http://jakarta.apache.org/log4j/'>

   <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{yyyy MM dd HH:mm:ss} %5p %c{1} - %m%n"/>
      </layout>
   </appender>

    <appender name="rollingAppender" class="org.apache.log4j.rolling.RollingFileAppender">
        <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="FileNamePattern" value="logs/stuff.%d{yyyy-MM_HH-mm}.gz"/>
        <param name="ActiveFileName" value="logs/stuff.log"/>
        </rollingPolicy>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{yyyy MM dd HH:mm:ss} %5p %c{1} - %m%n"/>
        </layout>
    </appender>
   <root>
      <level value="INFO"/>
      <appender-ref ref="consoleAppender"/>
      <appender-ref ref="rollingAppender"/>
   </root>
</log4j:configuration>
dutt
  • 7,909
  • 11
  • 52
  • 85

1 Answers1

1

Your pattern is wrong for the rollingAppender...

%%d{yyyy MM dd HH:mm:ss}

You need to drop the first "%"

For the second problem, (and I've not used TimeBasedRollingPolicy) do you need to set the actual time window as a parameter?

I answered a post similar to this the other day, not sure if it is correct yet though. See Log4j2 auto rollover after specified duration

Community
  • 1
  • 1
BretC
  • 4,141
  • 13
  • 22
  • I take it the DailyRotatingFileAppender doesn't support compression, but that's an acceptable loss for now. Things are working, thanks! – dutt Apr 17 '15 at 12:25
  • Sidenote, should I switch to log4j2 instead? – dutt Apr 17 '15 at 12:47
  • @dutt I prefer Log4J2 personally and would use that if starting a new project, it depends on how much stuff you have that depends on the old impl. If you are using SLF4J or something, switching implementations to Log4J2 probably won't be too much of a problem – BretC Apr 17 '15 at 12:48
  • Well, there is no current stuff so I'll look into log4j2 instead then. Thanks. – dutt Apr 18 '15 at 20:54