Support log4j 2.x.

This is an RC dependency. We can update that when log4j 2.0 is release, but that shouldn't be
necessary.
This commit is contained in:
Jamie Doornbos
2014-05-27 14:41:54 -07:00
parent bba7ee14be
commit 1c4c379627
3 changed files with 74 additions and 0 deletions
+7
View File
@@ -55,6 +55,13 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0-rc1</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
@@ -0,0 +1,56 @@
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2012 Michael Bayne, et al.
// http://github.com/samskivert/samskivert/blob/master/COPYING
package com.samskivert.util;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
/**
* A factory for creating log4j2 logger implementations.
*/
public class Log4J2Logger implements Logger.Factory
{
// from interface Logger.Factory
public void init ()
{
}
// from interface Logger.Factory
public Logger getLogger (String name)
{
return new Impl(LogManager.getLogger(name));
}
// from interface Logger.Factory
public Logger getLogger (Class<?> clazz)
{
return new Impl(LogManager.getLogger(clazz));
}
protected static class Impl extends Logger
{
public Impl (org.apache.logging.log4j.Logger impl)
{
_impl = impl;
}
@Override // from Logger
protected boolean shouldLog (int levIdx)
{
return _impl.isEnabled(LEVELS[levIdx]);
}
@Override // from Logger
protected void doLog (int levIdx, String formatted, Throwable throwable)
{
_impl.log(LEVELS[levIdx], formatted, throwable);
}
protected final org.apache.logging.log4j.Logger _impl;
protected static final Level[] LEVELS = {
Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR };
}
}
@@ -178,6 +178,17 @@ public abstract class Logger
System.err.println("Unable to instantiate Log4JLogger: " + t);
}
// create and a log4j2 logger if the log4j2 configuration system property is set
try {
if (factory == null && System.getProperty("log4j.configurationFile") != null) {
factory = (Factory)Class.forName("com.samskivert.util.Log4J2Logger").newInstance();
}
} catch (SecurityException se) {
// in a sandbox, no biggie
} catch (Throwable t) {
System.err.println("Unable to instantiate Log4J2Logger: " + t);
}
// lastly, fall back to the Java logging system
if (factory == null) {
factory = new JDK14Logger();