开发者

log4j - DailyRollingFileAppender, file not roll hourly

I have the following simple Test class for DailyRollingFileAppender to rolls the log file every hour. The problem I am facing is that, it doesn't seem to roll over to new log file every hour even though I h开发者_StackOverflow中文版ave set that to '.'yyyy-MM-dd-HH. Any idea where in the code I did wrongly?

public class Test {
  static Logger logger = Logger.getLogger(Test.class);

  public static void main(String args[]) throws Exception {
      String pattern = "%-20d{dd MMM yyyy HH:mm:ss} [%-5p] - %m%n";

    PatternLayout patternLayout = new PatternLayout(pattern);  

    //CREATE APPENDER.       
    DailyRollingFileAppender myAppender = new DailyRollingFileAppender(patternLayout, "TestOrig.log", "'.'yyyy-MM-dd-HH");

    //ADD APPENDER & LEVEL.
    logger.addAppender(myAppender);
    logger.setLevel   ((Level) Level.DEBUG);


//WRITE MESSAGES.
logger.debug("Successful");
logger.info ("Failed" );
logger.warn ("Failed" );
logger.error("Successful");
logger.fatal("Failed"); 

    while(true)
    {           
        Thread.sleep(1000);
    } 
  }
}


Use @Singleton and @Schedule to create an ejb cron like schedule for your timer service.

import javax.ejb.Schedule;
import javax.ejb.Singleton;


@Singleton
public class Cron {
        static Logger logger = Logger.getLogger(Test.class);
    @Schedule(second="0", minute="0", hour="0", dayOfWeek="*", persistent=false)
    public void rollLogs() {
        logger.info("midnight");
    }
}


I don't see any error here. I could see this is creating files when I tried for minutes.

DailyRollingFileAppender myAppender = new DailyRollingFileAppender(patternLayout, "TestOrig.log", "'.'yyyy-MM-dd-HH-mm");

Did you see any error on the console??

Possible reason of error for you could be , you are trying to run the same program multiple times, without ending the previously started program and that is resulting in file access permission issue.


Mike, you are correct in your comment above. You will not get a new file unless there is logging activity during that time. If you are required to force the issue, you'll need to start a thread with a runnable that posts a line to the log after the start of each new hour.

The goal is to make one post to your log every 59.5 minutes, starting from minute 1.

Basic standard knowledge of how to use Runnable and Thread are required for his solution. I am assuming you are running a standard application and not within a managed server environment

  1. create a class that implements Runnable
  2. overwrite the run() method with a while loop to a true boolean variable (isAlive) your app can set to false when your app shuts down.
  3. during the loop call the .info("Logger Text") method of a static Logger logger = Logger.getLogger(YourClassName.class); with a loop wait time of 60 minutes.
  4. Toss the Runnable into a new Thread() object at application start-up
  5. make one info() post to your log at startup.
  6. start the thread object when you start your app.

run() method of Runnable can be

public void run() { 
    while (isAlive) { // isAlive is a global-level (static, even) boolean 
                      // you declared earlier as true, your app should set it to false
                      // if your app decides to exit

        try { 
            logger.info("Rollover Log Text");
            Thread.currentThread().sleep(1000 * 60 * 60); // 60 minutes 
        } catch (InterruptedException ignore) { 
        }
    }
}

Remember to set isAlive to true before you start your Thread, set it to false on shutdown or error/exception close, and call the the interrupt() method of your thread after setting to false. This should log one time an hour.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜