Limiting log size

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2007
Posts: 81
Reputation: lookof2day is an unknown quantity at this point 
Solved Threads: 10
lookof2day lookof2day is offline Offline
Junior Poster in Training

Limiting log size

 
0
  #1
Dec 6th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,493
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 520
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Limiting log size

 
0
  #2
Dec 6th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,629
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Limiting log size

 
0
  #3
Dec 6th, 2007
Here is a crude example assuming that you are using the Logging API of Java and not some third party logging library.

  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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC