Logger places a
very important part in java application. We can have many types of
messages like Error Message, Warning message etc. Thus it will keep
track of all errors and warnings that occurred during program
execution in our Application.
This article will
help you to understand what is Logger and how we use it in our
program to log sever and warning messages in a file called log file.
import java.io.IOException; import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.Logger; public class LoggerExample{ public static void main(String[] args) { Logger logger = Logger.getLogger("LogCreation"); try { FileHandler fhandler = new FileHandler("LogFile.log", true); // addHandler method is for adding a log handler logger.addHandler(fhandler ); logger.log(Level.SEVERE,"Log Created"); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e. printStackTrace(); } } }