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
This commit is contained in:
@@ -69,6 +69,7 @@
|
||||
<exclude name="com/samskivert/test/**" unless="build.test"/>
|
||||
<exclude name="com/samskivert/util/**" unless="build.util"/>
|
||||
<exclude name="com/samskivert/util/FileUtil*" unless="build.fileutil"/>
|
||||
<exclude name="com/samskivert/util/Log4JLogger*" unless="log4j.present"/>
|
||||
<exclude name="com/samskivert/velocity/**" unless="build.velocity"/>
|
||||
<exclude name="com/samskivert/xml/**" unless="build.xml"/>
|
||||
<exclude name="com/samskivert/**/tests/**" unless="build.tests"/>
|
||||
|
||||
@@ -70,6 +70,10 @@
|
||||
<available property="junit.present" classpathref="classpath"
|
||||
classname="junit.framework.Test"/>
|
||||
<echo level="info">JUnit: ${junit.present}</echo>
|
||||
|
||||
<available property="log4j.present" classpathref="classpath"
|
||||
classname="org.apache.log4j.Logger"/>
|
||||
<echo level="info">log4j: ${log4j.present}</echo>
|
||||
</target>
|
||||
|
||||
<!-- combines package availability into build controls -->
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
<include name="ehcache-1.2.jar"/>
|
||||
<include name="junit-3.7.jar"/>
|
||||
<include name="junit4.jar"/>
|
||||
<include name="log4j.jar"/>
|
||||
<include name="mail.jar"/>
|
||||
<include name="servlet-api.jar"/>
|
||||
<include name="velocity-1.5-dev.jar"/>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user