Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2004

Tip: Looking for answers? Try searching our database.

jdk1.4 logging , singleton implementation

Thread view: 
robert walker - 14 Apr 2004 18:28 GMT
hi all, from reading the jdk1.4 logging docs
they use an example like

public class HelloWorld {
 private static Logger theLogger =
     Logger.getLogger(HelloWorld.class.getName());

 public static void main(String[] args) {
     HelloWorld hello = new HelloWorld();
     hello.sayHello();
 }
 public void sayHello() {
     theLogger.finest("Hello logging!");
 }
}

I only want one logger for all class files, and do not want to
have to have a static instance for each class I need to do logging

does anyone have an example of how to setup a singleton,
I will be running this from tomcat, so would like an init servlet
to setup the logging configuration, (basically just the level)
and then let all servlets use this singlteon to log
Dave Monroe - 15 Apr 2004 13:29 GMT
> hi all, from reading the jdk1.4 logging docs
> they use an example like
[quoted text clipped - 19 lines]
> to setup the logging configuration, (basically just the level)
> and then let all servlets use this singlteon to log

Generally, singletons have a private constructor.

You would do something like this:

public class MyClass {
   private MyClass _myclass;

   private MyClass() {
       .
       .
       .
   }

   public getInstance() {
       if(_myclass == null) {
           _myclass = new MyClass();
       }
       return(_myclass);
   }
}

Voila!  A singleton is born.

Dave Monroe
iksrazal - 15 Apr 2004 21:40 GMT
> hi all, from reading the jdk1.4 logging docs
> they use an example like
[quoted text clipped - 19 lines]
> to setup the logging configuration, (basically just the level)
> and then let all servlets use this singlteon to log

A few quick things:

1) Check out http://protomatter.sourceforge.net/ - which uses static
classes for logging. Only one class - like syslog - will do the trick.
Sorta like:

Syslog.debug(this, "Hello");

Which brings up the problem you may have with your idea - how to know
automatically which class generated the log. The 'this' above does
that for you.

2) You're idea will work in tomcat due to the classloader sequence,
but on other servers your mileage will vary. A singleton is only
unique in its JVM.

3) Often you would load log4j or protomatter via a servlet anyways
like so (uses xdoclet tags to put config in web.xml):

/**
* <P>
* Load log4j
* </P>
*
* @web.servlet
*      display-name="log4j-init"
*      load-on-startup="1"
*      name="com.infoseg.mr.atualiza.util.Log4jInit"
*
* @web.servlet-init-param name="log4j-init-file"
*                         value="WEB-INF/properties/log4j.properties"
*
*/
public class Log4jInit extends HttpServlet {

   public void init() {
       String prefix =  getServletContext().getRealPath("/");
       String file = getInitParameter("log4j-init-file");
       // if the log4j-init-file is not set, then no point in trying
       if(file != null) {
           PropertyConfigurator.configure(prefix+file);
       }
   }

   public void doGet(HttpServletRequest req, HttpServletResponse
res) {}

}

Typically the log level is set in the config file.

HTH,
iksrazal


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.