Log is a simple java class that provides methods to log messages to a text/log file. To log other types of data simple add the needed methods using the method ‘public static void write(boolean msg)’ as a template.
Usage:
To use this class simply import it in to any class that you wish to use its methods. to add to the log file add the following line to your code.
1 | Log.write("<PUT YOUR MESSAGE HERE>"); |
And here is the class code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.io.BufferedWriter; import java.io.FileWriter; public class Log { private logFilePath = "<path to your log file goes here>"; // set this to point to the log file you wish to use // default constructor public static void write(String msg) { try { // Create file FileWriter fstream = new FileWriter(logFilePath, true); BufferedWriter out = new BufferedWriter(fstream); DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = new Date(); msg = dateFormat.format(date) + " ---> " + msg; out.write(msg); out.newLine(); out.close(); } catch (Exception e) {// Catch exception if any System.err.println("Error: " + e.getMessage()); } } // method to log boolean values public static void write(boolean msg) { write("" + msg); } // method to log long values public static void write(long msg) { write("" + msg); } } |


