| | |
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.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
![]() |
Other Threads in the Java Forum
- Previous Thread: viewing video in applet
- Next Thread: help on optimizing java code
Views: 589 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addball addressbook android api append apple applet application arguments array arrays automation binary bluetooth button chat class classes client code component css csv database draw eclipse ee error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javaarraylist javaprojects jmf jni jpanel julia jvm key linux list loan loop map method methods mobile netbeans newbie number object oracle output packets phone print problem program programming project recursion reporting robot scanner screen se server service set size sms socket software sort sql stream string swing test threads time transfer tree ubuntu windows wrong






