Java Programing laungage

java.util Projects

java.util Project 1

Writing Log Records in a Log File

In This page of the programming tutorials, we are going to demonstrates for writing log records into a log file. The Logger is provide different types of level like: warning, info and severe that have log records. Log records are written into a log file.

Previous Home Next

Description of program

This program is take a file name and check it for this operation it is used the exists() method. If the file is exist then records will be mentioned into the log file otherwise log record not written. it displays a message "Operation successfully!" if file is exist. Otherwise shows a message "File is not exist" and log records don't written into a log file.

hand.publish(LogRecord rec): This method is used to take a LogRecord and publish it into specific file that means log records are written into the given file.


package r4r;
import java.util.*;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
public class logrecordtest {
public static void main(String[] args) {
logrecordtest lg = new logrecordtest();
public logrecordtest(){ try{ BufferedReader br = new BufferedReader(new InputStreamReader(; System.out.println("Enter the file name which contain '.log' extension"); String str = br.readLine(); File file1 = new File(str + ".log"); if(file1.exists()) { FileHandler hd = new FileHandler(str + ".log", true); Logger log = Logger.getLogger("aman raj"); LogRecord rec1 = new LogRecord(Level.WARNING,"Do some operation here!"); LogRecord rec2 = new LogRecord(Level.INFO,"Do some operation here!"); LogRecord rec3 = new LogRecord(Level.SEVERE,"Do some operation here!"); hd.publish(rec1); hd.publish(rec2); hd.publish(rec3); log.addHandler(hd); System.out.println("Operation is successfully done!"); } else{ System.out.println("File is not exist"); } } catch (IOException e){} } }
Previous Home Next