user1234
user1234

Reputation: 167

Unable to do logging to a file in a jsf web application

Im using websphere server and JSF 2.0

In my managed bean i have declared the following:

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

The following code is in the constructor of the bean.

try {
FileHandler handler = new FileHandler("logging.txt");
LOGGER.addHandler(handler);
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String stacktrace = sw.toString();
LOGGER.log(Level.SEVERE, stacktrace);
} catch (Exception logException) {
System.err.println("Logging error");
}

The code is writing all the logs to the console but not to the file. The log file logging.txt is also not getting created.

Should I create the file by myself or the path is wrong. Help me

Upvotes: 0

Views: 1040

Answers (2)

Udo Held
Udo Held

Reputation: 12548

As BalusC already pointed out. The log-file will probably created in the already deployed application.

With websphere you can usually find your applications under:

WEBSPHERE_INSTALLATION_PATH/profiles/YOURPROFILE/installedApps/YOURAPPNAME.

After switching to that folder search it and the subfolders for your files. I'm not sure, but I wouldn't be suprised if you find it in the WEB-INF/classes.

Upvotes: 0

BalusC
BalusC

Reputation: 1108802

From your code:

FileHandler handler = new FileHandler("logging.txt");

That will create a logging.txt file in the appserver's default working directory. You can determine the exact path to the default working directory as follows:

System.out.println(new File(".").getAbsolutePath());

The logging.txt file should be in that folder.

However, this isn't the best idea. Using relative paths is a bad idea when you cannot control the default working directory from inside your application. You should use absolute paths instead. I.e., start with /:

FileHandler handler = new FileHandler("/path/to/logging.txt");

In Windows, this will be saved in the same disk as where the current working directory is. Otherwise, when you want to change disks as well, you would also need to prefix with C: or something.

Note that this problem is in no way related to JSF. You would have exactly the same problem with any other Java API/framework.

Upvotes: 3

Related Questions