Create a File logging Utility using Java Code
Many times during coding, we need to log our outputs or comments. Usual way to do that is to print the log on the Console using System.out.println() function.
However, there can be a requirement to print the output or comments to a file so that it can be referenced later. One option is to use log4j library which is a standard logging utility.
However, you can also create your own utility using a few lines of Java Code! Let's check out how.
We'll try to cover the following utilities which can form the base of a logging tool:
1. File logging by passing the value and the file path.
2. File logging by passing the value and setting the log path in a properties file.
3. Clear the log file.
Let's first take a look at a basic File logging utility.
1. File logging by passing the value and the file path.
/** * Writes data/logs to a file. We'll pass both the filePath where the log needs to be written, and the fileValue which needs to be written. * * @author computengine.com * @param filePath - path + filename * @param fileValue - data to be logged */ public static void fileLogger(String filePath, String fileValue) { try { // open the file at file path File file = new File(filePath); // enable writer FileWriter fr = new FileWriter(file, true); // write the file value and a line separator fileValue = fileValue + System.lineSeparator(); // write to file fr.write(fileValue); // close the file fr.close(); } catch (Exception e) { e.printStackTrace(); } }
In the above function, we are passing the filePath. Please note that you have to pass the complete path of the file where the file needs to be stored. If you are storing the log file in your project folder, you can pass a relative path as well.
Eg. ./DataSources/log.txt
This relative path will create the log file in a DataSources folder within the Project folder.
The above code will create a file if not present, and open existing file if already present and append the new log to the existing lines. It will then save and close the file.
Coming to the next part, i.e. read the Properties file to get the log file path, and simply pass the value to be written.
2. File logging by passing the value and setting the log path in a properties file.
This code is split into two parts. The logging utility will be similar to shown above, but we will modify it to read a properties file instead.
In order to read a Properties file, we have written some code earlier which you can refer.
Here is the code for the logger
/** * Writes data/logs to default filepath. We'll pass the fileValue which needs to be written and read the filePath from a Properties file. * * @author computengine.com * @param fileValue - data to be logged */ public static void fileLogger(String fileValue) { try { // open the file at file path File file = new File(FileUtils.getPropertyValue("logFilePath")); // enable writer FileWriter fr = new FileWriter(file, true); // write the file value and a line separator fileValue = fileValue + System.lineSeparator(); // write to file fr.write(fileValue); // close the file fr.close(); } catch (Exception e) { e.printStackTrace(); } }
We will set the following property in the properties file which is being used above.
#Logger logFilePath=./DataSources/log.txt
Now coming to the final part of the tutorial, i.e. clearing the log file. It is quite possible that you want to start with a fresh log file when you are running your program. You can clear the file like this.
/** * Clear the contents of a File * * @author Computengine.com * @param fileName */ public static void clearTheFile(String fileName) { System.out.println("Clearing the File: " + fileName); try { FileWriter fwOb = new FileWriter(fileName, false); PrintWriter pwOb = new PrintWriter(fwOb, false); pwOb.flush(); pwOb.close(); fwOb.close(); } catch (Exception e) { e.printStackTrace(); } }
Thanks for Reading the Article. If you have reached this far, we hope that the article was useful to you! Please Like/Share/Follow us on Facebook, Twitter, Tumblr.
Comments
Post a Comment