1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.log4j;
18
19 import org.apache.log4j.spi.LoggerFactory;
20 import org.slf4j.helpers.Util;
21
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
24
25
26
27
28
29
30
31
32
33
34 class Log4jLoggerFactory {
35
36
37 private static ConcurrentMap<String, Logger> log4jLoggers = new ConcurrentHashMap<String, Logger>();
38
39 private static final String LOG4J_DELEGATION_LOOP_URL = "http://www.slf4j.org/codes.html#log4jDelegationLoop";
40
41
42 static {
43 try {
44 Class.forName("org.slf4j.impl.Log4jLoggerFactory");
45 String part1 = "Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError. ";
46 String part2 = "See also " + LOG4J_DELEGATION_LOOP_URL + " for more details.";
47
48 Util.report(part1);
49 Util.report(part2);
50 throw new IllegalStateException(part1 + part2);
51 } catch (ClassNotFoundException e) {
52
53 }
54 }
55
56 public static Logger getLogger(String name) {
57 org.apache.log4j.Logger instance = log4jLoggers.get(name);
58 if (instance != null) {
59 return instance;
60 } else {
61 Logger newInstance = new Logger(name);
62 Logger oldInstance = log4jLoggers.putIfAbsent(name, newInstance);
63 return oldInstance == null ? newInstance : oldInstance;
64 }
65 }
66
67 public static Logger getLogger(String name, LoggerFactory loggerFactory) {
68 org.apache.log4j.Logger instance = log4jLoggers.get(name);
69 if (instance != null) {
70 return instance;
71 } else {
72 Logger newInstance = loggerFactory.makeNewLoggerInstance(name);
73 Logger oldInstance = log4jLoggers.putIfAbsent(name, newInstance);
74 return oldInstance == null ? newInstance : oldInstance;
75 }
76 }
77 }