From 84597c3b990b35fd363633d8b6d1706fdcc82d09 Mon Sep 17 00:00:00 2001 From: mdb Date: Wed, 28 May 2008 18:02:37 +0000 Subject: [PATCH] Added log4j support to the log wrapper. It is enabled if the log4j configuration system property is set (and a particular logger is not specifically configured). git-svn-id: https://samskivert.googlecode.com/svn/trunk@2315 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- build.xml | 1 + etc/depends-incl.xml | 4 + etc/libs-incl.xml | 1 + src/java/com/samskivert/util/Log4JLogger.java | 88 +++++++++++++++++++ src/java/com/samskivert/util/Logger.java | 11 ++- 5 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/java/com/samskivert/util/Log4JLogger.java diff --git a/build.xml b/build.xml index 7bd03cb2..1c287ee1 100644 --- a/build.xml +++ b/build.xml @@ -69,6 +69,7 @@ + diff --git a/etc/depends-incl.xml b/etc/depends-incl.xml index b1e98097..df84d075 100644 --- a/etc/depends-incl.xml +++ b/etc/depends-incl.xml @@ -70,6 +70,10 @@ JUnit: ${junit.present} + + + log4j: ${log4j.present} diff --git a/etc/libs-incl.xml b/etc/libs-incl.xml index a7d93567..3507a5d4 100644 --- a/etc/libs-incl.xml +++ b/etc/libs-incl.xml @@ -10,6 +10,7 @@ + diff --git a/src/java/com/samskivert/util/Log4JLogger.java b/src/java/com/samskivert/util/Log4JLogger.java new file mode 100644 index 00000000..d3276788 --- /dev/null +++ b/src/java/com/samskivert/util/Log4JLogger.java @@ -0,0 +1,88 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001-2008 Michael Bayne +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.util; + +import org.apache.log4j.Level; + +/** + * A factory for creating log4j logger implementations. + */ +public class Log4JLogger implements Logger.Factory +{ + // from interface Logger.Factory + public void init () + { + } + + // from interface Logger.Factory + public Logger getLogger (String name) + { + return new Impl(org.apache.log4j.Logger.getLogger(name)); + } + + // from interface Logger.Factory + public Logger getLogger (Class clazz) + { + return getLogger(clazz.getName()); + } + + protected static class Impl extends Logger + { + public Impl (org.apache.log4j.Logger impl) + { + _impl = impl; + } + + @Override // from Logger + public void debug (Object message, Object... args) + { + if (_impl.isEnabledFor(Level.DEBUG)) { + _impl.log(Level.DEBUG, format(message, args), getException(message, args)); + } + } + + @Override // from Logger + public void info (Object message, Object... args) + { + if (_impl.isEnabledFor(Level.INFO)) { + _impl.log(Level.INFO, format(message, args), getException(message, args)); + } + } + + @Override // from Logger + public void warning (Object message, Object... args) + { + if (_impl.isEnabledFor(Level.WARN)) { + _impl.log(Level.WARN, format(message, args), getException(message, args)); + } + } + + @Override // from Logger + public void error (Object message, Object... args) + { + if (_impl.isEnabledFor(Level.ERROR)) { + _impl.log(Level.ERROR, format(message, args), getException(message, args)); + } + } + + protected org.apache.log4j.Logger _impl; + } +} diff --git a/src/java/com/samskivert/util/Logger.java b/src/java/com/samskivert/util/Logger.java index 602f01c4..c4bbe9f7 100644 --- a/src/java/com/samskivert/util/Logger.java +++ b/src/java/com/samskivert/util/Logger.java @@ -135,7 +135,16 @@ public abstract class Logger // if a custom class was specified as a system property, use that _factory = createConfiguredFactory(); - // TODO: create and try a log4j logger + // 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(); + } + } catch (SecurityException se) { + // in a sandbox, no biggie + } catch (Throwable t) { + System.err.println("Unable to instantiate Log4JLogger: " + t); + } // lastly, fall back to the Java logging system if (_factory == null) {