Limiting the Size of a Log File in Java

This section illustrates you how to limit the size of a log file in Java. Log files haven't any boundation at the file creation time but Java provides the facility for limiting the size of a log file with the help of FileHandler.


 

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

Limiting the Size of a Log File in Java

     

This section illustrates you how to limit the size of a log file in Java. Log files haven't any boundation at the file creation time but Java provides the facility for limiting the size of a log file with the help of FileHandler. If log records are smaller than limiting log file then spaces are worst but it is larger than some information's to be lost or not mentioning in the log file. See detail information  and example:

Descriptions of program:

Program takes a file name that should have '.log' extension. If file exists then the capacity of log file is limited or bounded and shows a message "Operation successfully" otherwise it shows message "File not found!" and no any action executed.

Descriptions of code:

FileHandler(String file_name, int limit, int no_file):
This is the constructor of Logging class which is used for declaring a file handler for writing to the set of files. It takes following arguments:
String file_name: File name which has to be written
int limit: The maximum size of file
int no_file: Number of file that has been used by user.

Here is the code of program:

import java.io.*;
import java.util.logging.*;

public class LimitLogFile{
  public static void main(String[] args) {
  try{
  Logger log = Logger.getLogger("RoseIndia.Net");
  BufferedReader buf = 
  new 
BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter file name which has '.log' extension");
  String str = buf.readLine();
  File file = new File(str + ".log");
  if(file.exists())
  {
  int limit = 10;
  FileHandler hand = new FileHandler(str + ".log", limit,1);
  System.out.println("Operation successfully");
  log.addHandler(hand);
  }
  else{
  System.out.println("File not found!");
  }
  }
  catch (IOException e){}
  }
}

Download this example.

Advertisements

Share on Google+Share on Google+

Limiting the Size of a Log File in Java

Posted on: February 22, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Limiting the Size of a Log File in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads