1

Here is my code, I am wondering why it isn't logging anything. When I type into the console it says:

Hello

Apr 08, 2013 10:13:47 PM java.util.logging.LogManager$RootLogger log

INFO: Hello

However, nothing is being logged to any files.

import java.io.IOException;
import java.util.Scanner;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class main {
    public static void main(String[] args) throws IOException{

        while (1 == 1) {
            String text;
            Scanner in = new Scanner(System.in);
            text = in.nextLine();
            FileHandler fileTxt;
            SimpleFormatter formatterTxt;
            Logger logger = Logger.getLogger("");
            logger.setLevel(Level.INFO);
            fileTxt = new FileHandler("../loggedText.txt");
            formatterTxt = new SimpleFormatter();
            fileTxt.setFormatter(formatterTxt);
            logger.addHandler(fileTxt);
            logger.info(text);
        }
    }
}
kuuy123
  • 11
  • 1
  • Not wanting to be one of "those" people, but have you considered using a dedicated logging library like log4j instead of the built-in logging classes? – Catchwa Apr 09 '13 at 04:59

1 Answers1

0

Your loop never terminates which means the JVM never performs a proper shutdown. The FileHandler is only closed and synched if all the logging shutdown hook executes normally. It is possible that the data was written but is never synched with the filesystem.

Let's create a corrected example that can terminate normally:

public class main {

    private static final Logger logger = Logger.getLogger(""); //Prevent G.C.

    public static void main(String[] args) throws IOException {
        logger.setLevel(Level.INFO);
        FileHandler fileTxt = new FileHandler("../loggedText.txt");
        fileTxt.setFormatter(new SimpleFormatter());
        logger.addHandler(fileTxt);

        System.out.println("root.level " + logger.getLevel());
        System.out.println("FileHandler.level " + fileTxt.getLevel());
        Scanner in = new Scanner(System.in);
        String text;
        while (true) {
            System.out.print("Enter text: ");
            text = in.nextLine();
            if (text != null && !text.isEmpty() 
                    && !"quit".equalsIgnoreCase(text)) {
                logger.info(text);
            } else {
                System.out.println("Done!");
                break;
            }
        }
    }
}

Will output the following to the console:

root.level INFO
FileHandler.level ALL
Enter text: hello
Nov 22, 2016 10:39:19 AM java.util.logging.LogManager$RootLogger log
INFO: hello
Enter text: quit
Done!

On the filesystem is loggedText.txt containing:

Nov 22, 2016 10:39:19 AM java.util.logging.LogManager$RootLogger log
INFO: hello

If you can't terminate normally you need to explicitly call FileHandler.close() on the handlers you create.

Note: The logged class name and method name are incorrect which is known as JDK-8152389.

Community
  • 1
  • 1
jmehrens
  • 10,580
  • 1
  • 38
  • 47