jdk1.4 logging , singleton implementation

R

robert walker

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
 
D

Dave Monroe

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

Generally, singletons have a private constructor.

You would do something like this:

public class MyClass {
private MyClass _myclass;

private MyClass() {
 
I

iksrazal

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top