Getting log file name from FileHandler?

R

Rhino

Does anyone know how I could interrogate an existing FileHandler object to
determine the name of the file that it is using to write its log?

I am showing some error information in a dialog but I'd like to be able to
add a footnote to the GUI that says something like "For more details, you or
your system administrator should look in the log at c:\mypath\Error.log."

The only problem is that I don't see any way in the API to ask the
FileHandler what the file name is. FileHandler uses a String that contains
the log file name as its main parameter but, once created, I don't see
anything in FileHandler, Handler or other logging classes that gets the file
name used by the FileHandler. I *could* make the file name used to create
the FileHandler a variable and then interrogate that variable but I'd rather
just ask the FileHandler itself.

Does anyone have any idea how I can do that?
 
N

nos

Rhino said:
Does anyone know how I could interrogate an existing FileHandler object to
determine the name of the file that it is using to write its log?

I am showing some error information in a dialog but I'd like to be able to
add a footnote to the GUI that says something like "For more details, you or
your system administrator should look in the log at c:\mypath\Error.log."

The only problem is that I don't see any way in the API to ask the
FileHandler what the file name is. FileHandler uses a String that contains
the log file name as its main parameter but, once created, I don't see
anything in FileHandler, Handler or other logging classes that gets the file
name used by the FileHandler. I *could* make the file name used to create
the FileHandler a variable and then interrogate that variable but I'd rather
just ask the FileHandler itself.

Does anyone have any idea how I can do that?
Part of the problem might be related to the possibility
that the filename can be constructed entirely from the
LogManager properties, or it can also be a generic name
that maps to a set of N files.
 
J

Jose Rubio

Rhino said:
Does anyone know how I could interrogate an existing FileHandler object to
determine the name of the file that it is using to write its log?

I am showing some error information in a dialog but I'd like to be able to
add a footnote to the GUI that says something like "For more details, you or
your system administrator should look in the log at c:\mypath\Error.log."

The only problem is that I don't see any way in the API to ask the
FileHandler what the file name is. FileHandler uses a String that contains
the log file name as its main parameter but, once created, I don't see
anything in FileHandler, Handler or other logging classes that gets the file
name used by the FileHandler. I *could* make the file name used to create
the FileHandler a variable and then interrogate that variable but I'd rather
just ask the FileHandler itself.

Does anyone have any idea how I can do that?

--
You can inherit from FileHandler to add the functionality you need.

or if you don't use a pattern, you can just get the filenmae from the
properties file.
 
R

Rhino

Are you saying I should subclass FileHandler and make my new FileHandler
have a getPattern() method? Or do you mean something else?

Rhino
 
S

Sunny Anand

Rhino said:
Are you saying I should subclass FileHandler and make my new FileHandler
have a getPattern() method? Or do you mean something else?

Rhino

This is what you can do:
public String getLogFileName(FileHandler file){
Field pattern = getFieldFromClass("java.util.logging.FileHandler","pattern");
pattern.setAccessible(true);
String fileName = (String)pattern.get(file);
return fileName;
}

public Field getFieldFromClass(String className, String fieldName){
Class clazz = Class.forName(className);
Field[] fields = clazz.getDeclaredFields();

for (int i = 0; i<fields.length; i++){
if(fields.getName().equals(fieldName))
return fields;
}
return null;
}

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top