1

I am trying to configure my multi-stage application to also output console messages (log) to a file.

So here is my main class:

public class Main extends Application {

    private Stage stage;
    private User loggedUser;
    ...
    public static void main(String[] args) throws IOException {

        Application.launch(Main.class, (java.lang
            .String[])null);
    }

    @Override
    public void start(Stage primaryStage) throws IOException {

        try {
            stage = primaryStage;
            stage.setTitle("FXML Login Sample");
            gotoLogin();
            primaryStage.show();
        } catch (Exception ex) {

            Logger.getLogger(Main.class.getName()).log(Level.INFO, null, ex);
        }
    }
    ...
    private void gotoProfile() {
        try {
            FXMLDocumentController fxmlDocument = (FXMLDocumentController)    replaceSceneContent("FXMLDocument.fxml");
            fxmlDocument.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.INFO, null, ex);

        }
    }

   private void gotoLogin() {
       try {
           LoginController login = (LoginController) replaceSceneContent("login.fxml");
           login.setApp(this);
       } catch (Exception ex) {
           Logger.getLogger(Main.class.getName()).log(Level.INFO, null, ex);
       }
   }

How could i write console's output in a file also? Should i configure a second logger of just use the existing one and append its content to a file?

thanili
  • 777
  • 4
  • 26
  • 57
  • Which Logger API are you using? – ItachiUchiha Jan 23 '15 at 12:00
  • 1
    What does your logging configuration look like? – Puce Jan 23 '15 at 12:01
  • using java java.util.logging – thanili Jan 23 '15 at 12:24
  • I am using the default Javafx logging configuration i guess since all my logging statements are like: Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.ALL, null, ex); – thanili Jan 23 '15 at 12:26
  • you might wonna have a look at this thread: http://stackoverflow.com/questions/15758685/how-to-write-logs-in-text-file-when-using-java-util-logging-logger. Since you want to log the same output to console and file, its probably prefered to use the same logger object. Usually I prefer log4j as logging API. – crusam Jan 23 '15 at 13:45

1 Answers1

1

As I understood from your question and comments you haven't setup a configuration file.

Provide a logging.properties file alonside your application (not inside a jar).

I have only a sample using ConsoleHandler at hand, but you could add additional handlers such as a FileHandler:

handlers= java.util.logging.ConsoleHandler


.level= INFO

java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

java.util.logging.SimpleFormatter.format=%1$tF %1$tT %4$S %2$s - %5$s%6$s%n


my.package.x.level = SEVERE
my.package.y.level = INFO

Then pass the following command line argument when you start your application:

-Djava.util.logging.config.file=<path to configuration file>/logging.properties

You can find more documentation here: https://docs.oracle.com/javase/8/docs/technotes/guides/logging

Puce
  • 37,247
  • 13
  • 80
  • 152
  • And where am i setting the actual log filename? Just once and not for each class in my project? E.g. do i have to implement a Log Manager class and instantiate it in every class of my project? – thanili Jan 23 '15 at 15:58
  • @thanili you pass the config file location as a command line argument to the "java" command as described in my answer - so just once. You don't have to implement any class for this. – Puce Jan 23 '15 at 16:19
  • I can see that in the command line as argument i am passing the logging.properties file, in my comment before i mean where can i set the log.txt file ... the actual log file – thanili Jan 23 '15 at 17:23
  • @thanili have a look at the pattern property of the FileHandler: https://docs.oracle.com/javase/8/docs/api/java/util/logging/FileHandler.html – Puce Jan 23 '15 at 17:31
  • log4j seem to be quite easy to configure for using and more scalable than jata.util.logging ... thanks for your answers... – thanili Jan 24 '15 at 17:32
  • @thanili You could also consider SLF4J (a logger wrapper library). Like this you can change the logging framework later without changing the code. – Puce Jan 24 '15 at 20:34