Skip to content

Commit

Permalink
Move delegation loop check from log4j-over-slf4j.jar to slf4j-log4j12…
Browse files Browse the repository at this point in the history
….jar, see also SLF4J-345
  • Loading branch information
Frans Orsel committed Dec 15, 2015
1 parent 96d32b3 commit 7a23ca7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
Expand Up @@ -17,7 +17,6 @@
package org.apache.log4j;

import org.apache.log4j.spi.LoggerFactory;
import org.slf4j.helpers.Util;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand All @@ -36,23 +35,6 @@ class Log4jLoggerFactory {
// String, Logger
private static ConcurrentMap<String, Logger> log4jLoggers = new ConcurrentHashMap<String, Logger>();

private static final String LOG4J_DELEGATION_LOOP_URL = "http://www.slf4j.org/codes.html#log4jDelegationLoop";

// check for delegation loops
static {
try {
Class.forName("org.slf4j.impl.Log4jLoggerFactory");
String part1 = "Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError. ";
String part2 = "See also " + LOG4J_DELEGATION_LOOP_URL + " for more details.";

Util.report(part1);
Util.report(part2);
throw new IllegalStateException(part1 + part2);
} catch (ClassNotFoundException e) {
// this is the good case
}
}

public static Logger getLogger(String name) {
org.apache.log4j.Logger instance = log4jLoggers.get(name);
if (instance != null) {
Expand Down
Expand Up @@ -46,7 +46,7 @@ public class JCLLoggerFactory implements ILoggerFactory {
static {
try {
Class.forName("org.apache.commons.logging.impl.SLF4JLogFactory");
String part1 = "Detected both jcl-over-slf4j.jar AND slf4j-jcl.jar on the class path, preempting StackOverflowError. ";
String part1 = "Detected both jcl-over-slf4j.jar AND bound slf4j-jcl.jar on the class path, preempting StackOverflowError. ";
String part2 = "See also " + JCL_DELEGATION_LOOP_URL + " for more details.";

Util.report(part1);
Expand Down
18 changes: 18 additions & 0 deletions slf4j-log4j12/src/main/java/org/slf4j/impl/Log4jLoggerFactory.java
Expand Up @@ -30,6 +30,7 @@
import org.apache.log4j.LogManager;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.helpers.Util;

/**
* Log4jLoggerFactory is an implementation of {@link ILoggerFactory} returning
Expand All @@ -42,6 +43,23 @@ public class Log4jLoggerFactory implements ILoggerFactory {
// key: name (String), value: a Log4jLoggerAdapter;
ConcurrentMap<String, Logger> loggerMap;

private static final String LOG4J_DELEGATION_LOOP_URL = "http://www.slf4j.org/codes.html#log4jDelegationLoop";

// check for delegation loops
static {
try {
Class.forName("org.apache.log4j.Log4jLoggerFactory");
String part1 = "Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. ";
String part2 = "See also " + LOG4J_DELEGATION_LOOP_URL + " for more details.";

Util.report(part1);
Util.report(part2);
throw new IllegalStateException(part1 + part2);
} catch (ClassNotFoundException e) {
// this is the good case
}
}

public Log4jLoggerFactory() {
loggerMap = new ConcurrentHashMap<String, Logger>();
}
Expand Down

0 comments on commit 7a23ca7

Please sign in to comment.