Question
I need help with this Java project. Do not use built-in Java logger. In the main method there is a: private static Logger logger =
I need help with this Java project. Do not use built-in Java logger. In the main method there is a: private static Logger logger = new Logger logger.log("program started")
logger.log("program ended") So there will be a message in the file anytime the program is open and ended and anytime an instance is created. Please explain what you are doing.
import java.io.File; // Import the File class import java.io.IOException; // Import the IOException class to handle errors
public class Logger { // TODO 12 - Logger.java: Modify this class to be a Singleton
public Logger(String logFilePath) { // TODO 05 - Logger.java: Create the empty log file if it does not exist. // Note that if you're unable to create the log file, then print a console error message saying so (along with the exception message).
// I have tried to start this but not sure if it is correct try { File file = new File("logger.txt"); if (file.createNewFile()) { System.out.println("File created: " + file.getName()); } else { System.out.println("File already exists."); } } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); }
// TODO 07 - Logger.java: Output (to console) the "Logging to" message you see in the assignment // Logging to: \path\to\somewhere ot\literally\this\myLogFile.log // That is: the full path to the log file }
public PsuLogger() { // TODO 08 - Logger.java: Write this parameterless constructor to call the other constructor with the default filename of "myLogFile.log" }
public void log(String logMessage) { // NOTE: Be sure to check the assignment to see the format of what you'll write to the log file // TODO 9 - Logger.java: Write the error message to the log file // Put that code here.
// TODO 10 - Logger.java: If you were unable to create the log file in the constructor, then write message to the console instead // Put that code here.
// TODO 11 - Logger.java: If there is a problem when writing to the file, print out the exception message // Put that code here. }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started