• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Logging file

 
Henry Le
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am using the java.util.logging to log my messages to log file, as well as showing in the console, eventhough this is not required. I am having one log file for each class. But I would like to have one log file only, and I think I should have one kind of global logger and/or handler. Can anyone tell me please? I am doing something like this for each class:
....
private static Logger logger = Logger.getLogger (suncertify.client.LocalClient.class.getName());
....
private static FileHandler fh;
fh = new FileHandler("mylog%u.%g.log", true);
logger.addHandler(fh);
....
and I have as a result:
mylog0.0.log
mylog1.0.log
mylog2.0.log
....
each log file is for one of my classes. Now if I want it globally, where do I create the logger, and how does it look like?

Thank you very much.........
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you could simply configure you logger properties in the logging.properties file in the lib directory of your JRE.

Or configure it in a new file and use the

-Djava.util.logging.config.file=myfile

to set up the file in your program.

If you JDK is installed in /opt/jdk then the default logging.properties file should be located in /opt/jdk/jre/lib.

Take a look at this file, it contains a lot of information about how to configure a logger.

You could also read the Logging Overview in the JDK help.

If you insist in doing this in code, set the FileHandler for the higher logger in the hierachy instead of setting the handler for every class that has a logger.

For instance, if your package hierachy is suncertify.db then declare a static logger for the package suncertify and add the handler to it.

Since loggers work in a hierachy, all the classes using a logger whose name is within this hierachy will use that file handler also. That you will learn in the Logging Overview in the JDK.
[ December 15, 2006: Message edited by: Edwin Dalorzo ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic