• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

re: help appreciated

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't much about the Logger class, but I can try to help you. What you could do is create your logger in a static block in the LogFile class, and then use it to get access to the logger. Try using something like this:

Angel
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joew,
Can you not put this line of code

into your LogFile constructor:

With this approach, if you have something like this in your LogFile class:

you need not even use static for fh. You create an object of LogFile once and initialize its FileHandler instance member once. Then, you only return it. Please let me know if this is helpful ideas wise.
 
joew weakers
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers Vad, Angel. Thanks a lot. They appear to be good approaches. Will try them both out and let you know if they work. Much appreciated, Joe
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic