0

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");
    }

}
  • 2
    Can you post an example of how you call your logger class? – lucasvw Feb 19 '20 at 01:00
  • see this https://stackoverflow.com/a/70824/2310289 – Scary Wombat Feb 19 '20 at 01:11
  • By the way, never call `LocalDateTime.now()`. Use `Instant`, `OffsetDateTime`, or `ZonedDateTime` to track moments, actual points on the timeline. Tip: Always include the offset in your logging, to avoid ambiguity. Usually best to log in UTC, and to use the standard ISO 8601 format with `Z` on the end to indicate UTC (an offset of zero hours-minutes-seconds). Search to learn more, this has been covered many many times on Stack Overflow. – Basil Bourque Feb 19 '20 at 01:16

0 Answers0