| | |
Limiting log size
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
What are you using for logging currently? You might take a look at http://java.sun.com/javase/6/docs/ap...e-summary.html
or Log4J.
or Log4J.
Here is a crude example assuming that you are using the Logging API of Java and not some third party logging library.
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.
java Syntax (Toggle Plain Text)
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.
I don't accept change; I don't deserve to live.
![]() |
Other Threads in the Java Forum
- Previous Thread: viewing video in applet
- Next Thread: help on optimizing java code
| Thread Tools | Search this Thread |
actionlistener android api applet application array arrays automation binary block bluetooth character chat class classes client code compile component consumer database desktop developmenthelp draw eclipse error event exception fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaee javaprojects jmf jni jpanel julia lego linked linux list loop loops mac map method methods mobile netbeans newbie number online oracle print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update windows working






