tags:

views:

151

answers:

1

I am creating a java.util.logging.FileHandler that is allowed to cycle through files. When multiple instances of my application are run, a new log file is created for each instance of the application. I need to know what file is being used by the application because I want to upload the log file to my servers for further review. How can I tell what file is being used by a certain FileHandler?

A: 

The easiest way is to put some kind of identifier in the file name itself, i.e. the pattern argument when you create the FileHandler. Since these are instances of the same application, one way to distinguish them is by their process id, so you could make that part of the pattern. A better approach is to pass in an identifier through the command line and use that to make your filename. That way you control the files being created in some sense. Finally, if your application has some knowledge of why it's different from all the others, for example it connects to a particular database server, then you could just use that database server name as part of the filename.

EDIT: There does not seem to be any API to get the name of the file being used by a FileHandler. I would suggest looking into the logging extensions in x4juli (which ports a lot of the log4j functionality to the java.util.logging specs):

You should be able to substitute an instance of their FileHandler which provides a getFile() method:

ars
Well, every time a new instance is run, the log file is incremented as so:1st instance - log.02nd instance - log.13rd instance - log.2
AmandeepGrewal
Maybe I've misunderstood your question. It seems you can already associate an instance with a file - what else do you need? If you need further identifying information, like what does instance 1, 2, or 3 mean, then you need to enhance it in the way I've specified above. If not, then please clarify.
ars
Lets say I have 3 instances running, and each has its own log file, as stated in my last comment. Instance 2 encounters an error, and logs the error to its logfile (log.1), it will then need to upload the logfile to a server, but in order to upload it, it must know which logfile it just wrote to, which will be same one it will upload.
AmandeepGrewal
Thanks for clarifying. I've updated the answer.
ars
Thank you, the library you referred me to allowed me to use the same logfile for multiple instances, which doesn't require me to find the logfile anymore, which is even better. :)
AmandeepGrewal