Hey there. OK. First of all I am not too sure if this is the correct forum
for this particular query. Secondly this is a long query so I am grateful if anyone has the patience to read it. OK here goes. I have several classes interacting together which firstly render a map to a GUI and then allow the user to zoom the map, pan the map in any direction and turn features on and off if they so wish. What I am doing right now is recording every single mouse event (user interaction) using the java.util.logging package. This logger class can log all the user interactions, via mouse events, into an XML file format using what is known as a FileHandler. You can choose what information it is that you want to log (I am hoping somebody out there has some experience with
Java Logging although my query is not solely on this topic).
Basically I have set up a LogFile class which has the following code:
public class LogFile
{
private Logger logger = null; //Logger.getLogger("mypackage3.logging");
protected FileHandler fh = null;
...
public LogFile(Logger logger1, FileHandler fileH)
{
this.logger = logger1;
this.fh = fileH;
}
}
If I want to record a specific user interaction and I know the class that houses the associated mouse event information I call a new instance of the LogFile class using the above constructor, e.g. in my FileMenu class:
public class FileMenu
{
protected Logger logger = Logger.getLogger("mypackage3.logging");
protected FileHandler fH;
protected LogFile logFile = new LogFile(logger, fH); //new instance called
...
public void actionPerformed(ActionEvent e){ //this is the user event
try{
logger.addHandler(new FileHandler("C://log.txt"));
logger.setLevel(Level.SEVERE); //logger specific code
} catch (Exception v){
v.printStackTrace();
}
logger.severe("currentUser logged out"); //this is recorded in log.txt
Here is where the problem arises. Every time I call a new instance of LogFile a new File is created. However, if all the methods that house the required mouse events lie in different classes surely then I will have to keep calling new instances of this LogFile class in order to record the necessary information. This then will cause numerous files to be created.
I tried one other approach using "static" (excuse my use of static variables and functions but if I dont use these surely I will have to call many instances of my LogFile class). The revised LogFile class now looks as follows:
public class LogFile
{
private Logger logger = null; //Logger.getLogger("mypackage3.logging");
protected static FileHandler fh = null;
...
public static void setFileHandler()
{
try
{
fh = new FileHandler("C://log.txt");
}
catch(Exception g)
{
g.printStackTrace();
}
}
}
Now my FileMenu class would look as follows:
public class FileMenu
{
protected Logger logger = Logger.getLogger("mypackage3.logging");
...
public void actionPerformed(ActionEvent e) {
try{
LogFile.setFileHandler();
logger.addHandler(LogFile.fh);
logger.setLevel(Level.SEVERE);
}
catch (Exception v){
v.printStackTrace();
}
logger.severe("currentUser logged out"); //this is recorded
If I set up the code like the above in the FileMenu class in all classes where I need to log user events it still results in many files being created (one for each mouse event) rather than just a single file holding all the interactions, i.e. there are many log.txt files being created each time I interact with my map. Can anyone offer me any assistance here. Any help will be much appreciated as I cannot figure this one out on my own.
Also for each log.txt file being created there are these LCK files (always empty) and log.txt.i files being created where i is the ith user event of a session (can be any number). Sorry for the longwinded post. Joe.