aspose file tools*
The moose likes I/O and Streams and the fly likes re: Java Logger Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "re: Java Logger Class" Watch "re: Java Logger Class" New topic
Author

re: Java Logger Class

joew weakers
Ranch Hand

Joined: Aug 21, 2003
Posts: 48
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);
...
public void actionPerformed(ActionEvent e){
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 looks 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");
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. Sorry for the longwinded post. Joe.
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
I'm not completely familiar with the logger package, but what if you put an if statement in the setFileHandler method, like:
if( fh == null )
{
fh = new FileHandler( "c://log.txt" );
}
so that you are only creating a new FileHandler if one doesn't already exist?
joew weakers
Ranch Hand

Joined: Aug 21, 2003
Posts: 48
Cheers Jason. Woll give it a try. Joe
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: re: Java Logger Class