1

I am working on a java project where I have created a SOAP service. What I have a problem with is when I want to create a file from the service class, while communicating with a client, it gets created on my Desktop. I want it to be created inside my project folder.

Here is a part of my code which creates the file:

private static Logger LOGGER = Logger.getLogger(AttractionSetup.class.getName());
 static{

    try {
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss");
        String date = dateFormat.format(new Date());

        FileHandler handler = new FileHandler(File.separator + date + ".log", true);
        LOGGER.addHandler(handler);

        SimpleFormatter formatter = new SimpleFormatter();
        handler.setFormatter(formatter);

    } catch(IOException ex) {
        ex.printStackTrace();
    }
 }

I need this file for logging exceptions and errors, so it would make much more sense if it was inside the project folder. If someone could explain why it goes to the Desktop it would be really appreciated.

ivaa14
  • 138
  • 10

1 Answers1

1

According to the Oracle spec java.util.logging.FileHandler.pattern specifies the location and pattern of the output file. And the default setting is your home directory.

Now, you should externalize the configuration using a .properties file. With all the necessary configurations in place, you could set the file location for example with java.util.logging.FileHandler.pattern=/whole/path/of/logging.properties.

Reference: Oracle Spec

Best regards

Jason Canto
  • 126
  • 1
  • 5
  • Thanks for the idea. But could you be a bit more specific? I am not sure I fully understand what I am supposed to do with the pattern. I am still new to this. Thanks – ivaa14 Aug 25 '18 at 09:08
  • 1
    The pattern specifies the location and pattern of the output file. For example: `java.util.logging.FileHandler.pattern=/path/to/log/files/java-example-%g.log`. This pattern is simply a way to include variables for the filename. Another example, including the home folder, the current date and the iteration number of the file: `${user.home}/archived/debug.%d{yyyy-MM-dd}.%i.log`. Here you can find a very complete answer and examples about this: https://stackoverflow.com/a/8249319/1905066 Best regards! – Jason Canto Aug 26 '18 at 21:44