943,600 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 720
  • Java RSS
Dec 6th, 2007
0

Limiting log size

Expand Post »
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.
Reputation Points: 16
Solved Threads: 11
Junior Poster in Training
lookof2day is offline Offline
83 posts
since Aug 2007
Dec 6th, 2007
0

Re: Limiting log size

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Dec 6th, 2007
0

Re: Limiting log size

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

java Syntax (Toggle Plain Text)
  1. public class Main {
  2.  
  3. private static Logger _log = Logger.getLogger(Main.class.toString());
  4.  
  5. public static void main(String[] args) throws Exception {
  6. FileHandler handler = new FileHandler("c:/abcd", 512, 512);
  7. handler.setFormatter(new SimpleFormatter());
  8. _log.addHandler(handler);
  9. LogManager manager = LogManager.getLogManager();
  10. manager.addLogger(_log);
  11. for (int i = 0; i < 128; ++i) {
  12. _log.info("hello to all the people out there: " + i);
  13. }
  14. handler.close();
  15. }
  16. }

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: viewing video in applet
Next Thread in Java Forum Timeline: help on optimizing java code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC