Make it possible to configure our log factory programmatically.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@2321 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2008-06-06 21:54:38 +00:00
parent e0a4516eaf
commit 95452a37b8
+30 -17
View File
@@ -39,6 +39,17 @@ package com.samskivert.util;
*/ */
public abstract class Logger public abstract class Logger
{ {
/**
* Used to create logger instances. This is only public so that the log factory can be
* configured programmatically via {@link Logger#setFactory}.
*/
public static interface Factory
{
public void init ();
public Logger getLogger (String name);
public Logger getLogger (Class clazz);
}
/** /**
* Obtains a logger with the specified name. * Obtains a logger with the specified name.
*/ */
@@ -55,6 +66,18 @@ public abstract class Logger
return _factory.getLogger(clazz); return _factory.getLogger(clazz);
} }
/**
* Configures the logging factory be used by the logging system. Normally you would not do this
* and would instead use the <code>com.samskivert.util.Logger</code> system property instead,
* but in some situations, like in an applet, you cannot pass system properties and we can't
* read them anyway because we're running in a security sandbox.
*/
public static void setFactory (Factory factory)
{
_factory = factory;
_factory.init();
}
/** /**
* Logs a debug message. * Logs a debug message.
* *
@@ -133,12 +156,12 @@ public abstract class Logger
protected static void initLogger () protected static void initLogger ()
{ {
// if a custom class was specified as a system property, use that // if a custom class was specified as a system property, use that
_factory = createConfiguredFactory(); Factory factory = createConfiguredFactory();
// create and a log4j logger if the log4j configuration system property is set // create and a log4j logger if the log4j configuration system property is set
try { try {
if (_factory == null && System.getProperty("log4j.configuration") != null) { if (factory == null && System.getProperty("log4j.configuration") != null) {
_factory = (Factory)Class.forName("com.samskivert.util.Log4JLogger").newInstance(); factory = (Factory)Class.forName("com.samskivert.util.Log4JLogger").newInstance();
} }
} catch (SecurityException se) { } catch (SecurityException se) {
// in a sandbox, no biggie // in a sandbox, no biggie
@@ -147,12 +170,12 @@ public abstract class Logger
} }
// lastly, fall back to the Java logging system // lastly, fall back to the Java logging system
if (_factory == null) { if (factory == null) {
_factory = new JDK14Logger(); factory = new JDK14Logger();
} }
// and finally initialize our factory // and finally configure our factory
_factory.init(); setFactory(factory);
} }
/** /**
@@ -178,16 +201,6 @@ public abstract class Logger
return null; return null;
} }
/**
* Used to create logger instances.
*/
protected static interface Factory
{
public void init ();
public Logger getLogger (String name);
public Logger getLogger (Class clazz);
}
protected static Factory _factory; protected static Factory _factory;
static { static {