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.

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.

Limiting the Size of a Log File in Java

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.