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
47 + " for more details.";
48
49 Util.report(part1);
50 Util.report(part2);
51 throw new IllegalStateException(part1 + part2);
52 } catch (ClassNotFoundException e) {
53
54 }
55 }
56
57 public static Logger getLogger(String name) {
58 org.apache.log4j.Logger instance = log4jLoggers.get(name);
59 if (instance != null) {
60 return instance;
61 } else {
62 Logger newInstance = new Logger(name);
63 Logger oldInstance = log4jLoggers.putIfAbsent(name, newInstance);
64 return oldInstance == null ? newInstance : oldInstance;
65 }
66 }
67
68 public static Logger getLogger(String name, LoggerFactory loggerFactory) {
69 org.apache.log4j.Logger instance = log4jLoggers.get(name);
70 if (instance != null) {
71 return instance;
72 } else {
73 Logger newInstance = loggerFactory.makeNewLoggerInstance(name);
74 Logger oldInstance = log4jLoggers.putIfAbsent(name, newInstance);
75 return oldInstance == null ? newInstance : oldInstance;
76 }
77 }
78 }