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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.