1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.impl;
18
19 import java.net.URI;
20
21 import org.apache.logging.log4j.core.LifeCycle;
22 import org.apache.logging.log4j.core.LoggerContext;
23 import org.apache.logging.log4j.core.config.Configuration;
24 import org.apache.logging.log4j.core.config.ConfigurationFactory;
25 import org.apache.logging.log4j.core.config.ConfigurationSource;
26 import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector;
27 import org.apache.logging.log4j.core.selector.ContextSelector;
28 import org.apache.logging.log4j.core.util.Constants;
29 import org.apache.logging.log4j.core.util.Loader;
30 import org.apache.logging.log4j.spi.LoggerContextFactory;
31 import org.apache.logging.log4j.status.StatusLogger;
32 import org.apache.logging.log4j.util.PropertiesUtil;
33
34
35
36
37 public class Log4jContextFactory implements LoggerContextFactory {
38
39 private static final StatusLogger LOGGER = StatusLogger.getLogger();
40
41 private ContextSelector selector;
42
43
44
45
46 public Log4jContextFactory() {
47 this(createContextSelector());
48 }
49
50
51
52
53
54 public Log4jContextFactory(final ContextSelector selector) {
55 this.selector = selector;
56 }
57
58 private static ContextSelector createContextSelector() {
59 final String sel = PropertiesUtil.getProperties().getStringProperty(Constants.LOG4J_CONTEXT_SELECTOR);
60 if (sel != null) {
61 try {
62 return Loader.newCheckedInstanceOf(sel, ContextSelector.class);
63 } catch (final Exception ex) {
64 LOGGER.error("Unable to create context {}", sel, ex);
65 }
66 }
67 return new ClassLoaderContextSelector();
68 }
69
70
71
72
73
74
75
76
77
78
79 @Override
80 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
81 final boolean currentContext) {
82 final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext);
83 ctx.setExternalContext(externalContext);
84 if (ctx.getState() == LifeCycle.State.INITIALIZED) {
85 ctx.start();
86 }
87 return ctx;
88 }
89
90
91
92
93
94
95
96
97
98
99
100 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
101 final boolean currentContext, final ConfigurationSource source) {
102 final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext, null);
103 if (externalContext != null && ctx.getExternalContext() == null) {
104 ctx.setExternalContext(externalContext);
105 }
106 if (ctx.getState() == LifeCycle.State.INITIALIZED) {
107 if (source != null) {
108 ContextAnchor.THREAD_CONTEXT.set(ctx);
109 final Configuration config = ConfigurationFactory.getInstance().getConfiguration(source);
110 LOGGER.debug("Starting LoggerContext[name={}] from configuration {}", ctx.getName(), source);
111 ctx.start(config);
112 ContextAnchor.THREAD_CONTEXT.remove();
113 } else {
114 ctx.start();
115 }
116 }
117 return ctx;
118 }
119
120
121
122
123
124
125
126
127
128
129
130 @Override
131 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
132 final boolean currentContext, final URI configLocation, final String name) {
133 final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext, configLocation);
134 if (externalContext != null && ctx.getExternalContext() == null) {
135 ctx.setExternalContext(externalContext);
136 }
137 if (ctx.getState() == LifeCycle.State.INITIALIZED) {
138 if (configLocation != null || name != null) {
139 ContextAnchor.THREAD_CONTEXT.set(ctx);
140 final Configuration config = ConfigurationFactory.getInstance().getConfiguration(name, configLocation);
141 LOGGER.debug("Starting LoggerContext[name={}] from configuration at {}", ctx.getName(), configLocation);
142 ctx.start(config);
143 ContextAnchor.THREAD_CONTEXT.remove();
144 } else {
145 ctx.start();
146 }
147 }
148 return ctx;
149 }
150
151
152
153
154
155 public ContextSelector getSelector() {
156 return selector;
157 }
158
159
160
161
162
163
164 @Override
165 public void removeContext(final org.apache.logging.log4j.spi.LoggerContext context) {
166 if (context instanceof LoggerContext) {
167 selector.removeContext((LoggerContext) context);
168 }
169 }
170 }