954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Limiting log size

Hi,

I'm trying to reproduce a Production Exception. In order to reproduce it I want to limit the size of my log file. Is there a way so that I can limit the size of my log i.e., my log should not grow beyond a limit. Is there a java class available for it? Please help.

lookof2day
Junior Poster in Training
83 posts since Aug 2007
Reputation Points: 16
Solved Threads: 11
 

What are you using for logging currently? You might take a look at http://java.sun.com/javase/6/docs/api/java/util/logging/package-summary.html

or Log4J.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Here is a crude example assuming that you are using the Logging API of Java and not some third party logging library.

public class Main {

    private static Logger _log = Logger.getLogger(Main.class.toString());

    public static void main(String[] args) throws Exception {
        FileHandler handler = new FileHandler("c:/abcd", 512, 512);
        handler.setFormatter(new SimpleFormatter());
        _log.addHandler(handler);
        LogManager manager = LogManager.getLogManager();
        manager.addLogger(_log);
        for (int i = 0; i < 128; ++i) {
            _log.info("hello to all the people out there: " + i);
        }
        handler.close();
    }
}


Here we use the FileHandler class instance to set the logger to write to the file system. It's constructor takes three parameters in our case: the path or the pattern with which you want to name your log files, the limit on the size of log files in bytes and the number of files to use when rotating.

From all the log files created, the file with the highest count will contain the most recent log information i.e. abcd1.log will contain the most recent log information.

Just fiddle around with the program and the API's and you would be just file.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You