so I want my Logger class to be an singleton. This is the only problem that I have in this program. I succefully added singeton in my other classes but I'm stuck in the logger class. I triend many different things but it gave me error or wrong output. Can anyone help me with the code?
The output that I'm getting -
Logging to: /Users/john/Desktop/File1/myLogFile.log
Logging to: /Users/john/Desktop/File1/myLogFile.log
Logging to: /Users/john/Desktop/File1/myLogFile.log
Logging to: /Users/john/Desktop/File1/myLogFile.log
Logging to: /Users/john/Desktop/File1/myLogFile.log
Fido:
Speak: 'Woof!'
Fly: [I can't fly]
Logging to: /Users/john/Desktop/File1/myLogFile.log
Toonces:
Speak: 'Meow!'
Fly: [I can't fly]
Logging to: /Users/john/Desktop/File1/myLogFile.log
Tweety:
Speak: 'Tweet!'
Fly: Flappy!
Logging to: /Users/john/Desktop/File1/myLogFile.log
The output that I want -
Logging to: /Users/john/Desktop/File1/myLogFile.log
Fido:
Speak: 'Woof!'
Fly: [I can't fly]
Toonces:
Speak: 'Meow!'
Fly: [I can't fly]
Tweety:
Speak: 'Tweet!'
Fly: Flappy!
Process finished with exit code 0
This is my Logger class -
import org.omg.CORBA.Environment;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Logger {
private String logFileName = "myLogFile.log";
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
Logger() {
File logFile = new File(logFileName);
if (!logFile.isFile()) {
try {
logFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Logging to: " + logFile.getAbsolutePath());
}
public void log(String message) {
try {
String formattedDate = LocalDateTime.now().format(formatter);
String fullMessage = System.lineSeparator() + formattedDate + " - " + message;
// NOTE, this is not the best way. But good enough for this assignment
Files.write(Paths.get(logFileName), fullMessage.getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Main class -
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import java.util.Scanner;
public class Main
{
private static Scanner keyboard = new Scanner (System.in);
public static void showOff(Animal animal) {
System.out.println(animal.getName() + ":");
System.out.println(" Speak: '" + animal.speak() + "'");
System.out.println(" Fly: " + animal.fly());
System.out.println();
}
public static void main(String[] args) {
Logger logger = new Logger();
logger.log("Program started");
Animal dog = SimpleAnimalFactory.buildAnimal("dog" );
Animal cat = SimpleAnimalFactory.buildAnimal("cat" );
Animal bird = SimpleAnimalFactory.buildAnimal("bird" );
dog.setName("Fido");
showOff(dog);
cat.setName("Toonces");
showOff(cat);
bird.setName("Tweety");
showOff(bird);
Logger logger2 = new Logger();
logger2.log("Program ended");
}
}