From 95452a37b8360f9d179a038ce8ec87d59809f351 Mon Sep 17 00:00:00 2001 From: mdb Date: Fri, 6 Jun 2008 21:54:38 +0000 Subject: [PATCH] Make it possible to configure our log factory programmatically. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2321 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/Logger.java | 47 +++++++++++++++--------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/src/java/com/samskivert/util/Logger.java b/src/java/com/samskivert/util/Logger.java index 7e5604fc..651c2a17 100644 --- a/src/java/com/samskivert/util/Logger.java +++ b/src/java/com/samskivert/util/Logger.java @@ -39,6 +39,17 @@ package com.samskivert.util; */ 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. */ @@ -55,6 +66,18 @@ public abstract class Logger 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 com.samskivert.util.Logger 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. * @@ -133,12 +156,12 @@ public abstract class Logger protected static void initLogger () { // 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 try { - if (_factory == null && System.getProperty("log4j.configuration") != null) { - _factory = (Factory)Class.forName("com.samskivert.util.Log4JLogger").newInstance(); + if (factory == null && System.getProperty("log4j.configuration") != null) { + factory = (Factory)Class.forName("com.samskivert.util.Log4JLogger").newInstance(); } } catch (SecurityException se) { // in a sandbox, no biggie @@ -147,12 +170,12 @@ public abstract class Logger } // lastly, fall back to the Java logging system - if (_factory == null) { - _factory = new JDK14Logger(); + if (factory == null) { + factory = new JDK14Logger(); } - // and finally initialize our factory - _factory.init(); + // and finally configure our factory + setFactory(factory); } /** @@ -178,16 +201,6 @@ public abstract class Logger 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; static {