1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.spi;
18
19 import java.util.HashSet;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
24 import java.util.concurrent.locks.ReadWriteLock;
25 import java.util.concurrent.locks.ReentrantReadWriteLock;
26
27 import org.apache.logging.log4j.LogManager;
28 import org.apache.logging.log4j.util.LoaderUtil;
29
30
31
32
33
34
35
36 public abstract class AbstractLoggerAdapter<L> implements LoggerAdapter<L>, LoggerContextShutdownAware {
37
38
39
40
41 protected final Map<LoggerContext, ConcurrentMap<String, L>> registry = new ConcurrentHashMap<>();
42
43 private final ReadWriteLock lock = new ReentrantReadWriteLock (true);
44
45 @Override
46 public L getLogger(final String name) {
47 final LoggerContext context = getContext();
48 final ConcurrentMap<String, L> loggers = getLoggersInContext(context);
49 final L logger = loggers.get(name);
50 if (logger != null) {
51 return logger;
52 }
53 loggers.putIfAbsent(name, newLogger(name, context));
54 return loggers.get(name);
55 }
56
57 @Override
58 public void contextShutdown(LoggerContext loggerContext) {
59 registry.remove(loggerContext);
60 }
61
62
63
64
65
66
67
68 public ConcurrentMap<String, L> getLoggersInContext(final LoggerContext context) {
69 ConcurrentMap<String, L> loggers;
70 lock.readLock ().lock ();
71 try {
72 loggers = registry.get (context);
73 } finally {
74 lock.readLock ().unlock ();
75 }
76
77 if (loggers != null) {
78 return loggers;
79 }
80 lock.writeLock ().lock ();
81 try {
82 loggers = registry.get (context);
83 if (loggers == null) {
84 loggers = new ConcurrentHashMap<> ();
85 registry.put (context, loggers);
86 if (context instanceof LoggerContextShutdownEnabled) {
87 ((LoggerContextShutdownEnabled) context).addShutdownListener(this);
88 }
89 }
90 return loggers;
91 } finally {
92 lock.writeLock ().unlock ();
93 }
94 }
95
96
97
98
99 public Set<LoggerContext> getLoggerContexts() {
100 return new HashSet<>(registry.keySet());
101 }
102
103
104
105
106
107
108
109
110 protected abstract L newLogger(final String name, final LoggerContext context);
111
112
113
114
115
116
117
118
119
120
121 protected abstract LoggerContext getContext();
122
123
124
125
126
127
128
129 protected LoggerContext getContext(final Class<?> callerClass) {
130 ClassLoader cl = null;
131 if (callerClass != null) {
132 cl = callerClass.getClassLoader();
133 }
134 if (cl == null) {
135 cl = LoaderUtil.getThreadContextClassLoader();
136 }
137 return LogManager.getContext(cl, false);
138 }
139
140 @Override
141 public void close() {
142 lock.writeLock ().lock ();
143 try {
144 registry.clear();
145 } finally {
146 lock.writeLock ().unlock ();
147 }
148 }
149 }