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