Generate Log file through Java Programming Language

In any technology platform, generating Log file is very important from the application perspective. If any issues encountered in an application then the developers or application users look at the logger file, to understand the root cause for the issue, and this plays a vital role in expediting the entire resolution process. 

As we have discussed the importance of the Log file, now we will see how to generate log through Java Programming language. 

For the generation of Log Java has provided a Logger class, Below mentioned java class needs to be imported in your project to generate log file 

import java.util.logging.Logger;

/*Java code to generate Log file*/


import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class LogFileInJava {
    public LogFileInJava() {
    }

    public static void main(String[] args) {
        LogFileInJava logFileInJava = new LogFileInJava();
        FileHandler fh;  
        Logger logger = Logger.getLogger("MyLog");
        try {  

            // This block configure the logger with handler and formatter  
    fh = new FileHandler("C:/Users/pranayt/Desktop/Logfile3.log");   

            logger.addHandler(fh);
            SimpleFormatter formatter = new SimpleFormatter();  
            fh.setFormatter(formatter);  

            // the following statement is used to log any messages  
            logger.info("My first log");  

        } catch (SecurityException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  

        logger.info("log file infor?mation--code by pranay tiwari");  
    }

}


Here is the output of below code 




Logfile output 




Comments

Post a Comment

Popular posts from this blog

Sample HZ API's script to Create Contacts and Contact Points in Oracle

REST integration built-in OIC to read Large files with size more than 10MB