0

I have used logger object and add file handler to log the exceptions in a log file of a Java project. It is working fine, when I run it from NetBeans IDE, but when I run it as a standalone application from console, the exceptions are logged in console and the file is not even created in the location I have specified.

private static final Logger LOGGER = Logger.getLogger(BuyerRegistration.class.getName());

    public static void main(String args[]) {
        
        Formatter simpleFormatter = null;
        Handler fileHandler = null;
        try {
            LOGGER.setUseParentHandlers(false);
            fileHandler = new FileHandler("dist/lib/CurryHouse.log", true);
            simpleFormatter = new SimpleFormatter();
            LOGGER.addHandler(fileHandler);
            fileHandler.setFormatter(simpleFormatter);
            fileHandler.setLevel(Level.ALL);
            LOGGER.setLevel(Level.ALL);
        } catch (IOException exception) {
            LOGGER.log(Level.SEVERE, "Error occur in FileHandler.", exception);
        }

The above code will come inside a BuyerRegistration class. Also, I have attached the following code in all catch blocks to log the exception in the file.

LOGGER.log(Level.SEVERE, "{0}\n", "Bills:352\t" + e.toString());

I want to the code to log all the exceptions in the location I specify rather than logging in the console

jmehrens
  • 10,580
  • 1
  • 38
  • 47
Hari
  • 3
  • 2

1 Answers1

0

I run it as a standalone application from console, the exceptions are logged in console and the file is not even created in the location I have specified.

You are using a relative path which is altered when the current directory is changed. Use a absolute path, a FileHandler pattern, or set the current directory in your launcher.

I want to the code to log all the exceptions in the location I specify rather than logging in the console

Add your handler to the root logger instead of your named logger. You can do this by supplying a logging.properties or a config class.

private static final Logger ROOT = Logger.getLogger("");
static {
    Formatter simpleFormatter = null;
    Handler fileHandler = null;
    try {
        //TODO: Fix file path
        fileHandler = new FileHandler("%h/CurryHouse.log", true);
        simpleFormatter = new SimpleFormatter();
        LOGGER.addHandler(fileHandler);
        fileHandler.setFormatter(simpleFormatter);
        fileHandler.setLevel(Level.ALL);
        ROOT.addHandler(fileHandler);
    } catch (IOException exception) {
        LOGGER.log(Level.SEVERE, "Error occur in FileHandler.", exception);
    }
}

public static void main(String args[]) {
   //Your app.
}

Also, I have attached the following code in all catch blocks to log the exception in the file.

You might want to consider logging the full stacktrace:

LOGGER.log(Level.SEVERE, "Bills:352", e);
jmehrens
  • 10,580
  • 1
  • 38
  • 47
  • I tried what you said, and it worked. I also tried what was wrong by doing a few combination of codes and found that adding a static block in the BuyerRegistration with all the same logging code as in question inside it, worked. Relative path didn't make a problem as I made sure the directory in which the log file is to be created in present in that location. Also, in your code `LOGGER.setUseParentHandlers(false);` was not present, cause of which it again printed in the console, but when I added it, it didn't print in console – Hari Jan 27 '21 at 20:10