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 final String sel = PropertiesUtil.getProperties().getStringProperty(Constants.LOG4J_CONTEXT_SELECTOR);
48 if (sel != null) {
49 try {
50 selector = Loader.newCheckedInstanceOf(sel, ContextSelector.class);
51 } catch (final Exception ex) {
52 LOGGER.error("Unable to create context {}", sel, ex);
53 }
54 }
55 if (selector == null) {
56 selector = new ClassLoaderContextSelector();
57 }
58 }
59
60
61
62
63
64
65
66
67
68
69 @Override
70 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
71 final boolean currentContext) {
72 final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext);
73 ctx.setExternalContext(externalContext);
74 if (ctx.getState() == LifeCycle.State.INITIALIZED) {
75 ctx.start();
76 }
77 return ctx;
78 }
79
80
81
82
83
84
85
86
87
88
89
90 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
91 final boolean currentContext, final ConfigurationSource source) {
92 final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext, null);
93 if (externalContext != null && ctx.getExternalContext() == null) {
94 ctx.setExternalContext(externalContext);
95 }
96 if (ctx.getState() == LifeCycle.State.INITIALIZED) {
97 if (source != null) {
98 ContextAnchor.THREAD_CONTEXT.set(ctx);
99 final Configuration config = ConfigurationFactory.getInstance().getConfiguration(source);
100 LOGGER.debug("Starting LoggerContext[name={}] from configuration {}", ctx.getName(), source);
101 ctx.start(config);
102 ContextAnchor.THREAD_CONTEXT.remove();
103 } else {
104 ctx.start();
105 }
106 }
107 return ctx;
108 }
109
110
111
112
113
114
115
116
117
118
119
120 @Override
121 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
122 final boolean currentContext, final URI configLocation, final String name) {
123 final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext, configLocation);
124 if (externalContext != null && ctx.getExternalContext() == null) {
125 ctx.setExternalContext(externalContext);
126 }
127 if (ctx.getState() == LifeCycle.State.INITIALIZED) {
128 if (configLocation != null || name != null) {
129 ContextAnchor.THREAD_CONTEXT.set(ctx);
130 final Configuration config = ConfigurationFactory.getInstance().getConfiguration(name, configLocation);
131 LOGGER.debug("Starting LoggerContext[name={}] from configuration at {}", ctx.getName(), configLocation);
132 ctx.start(config);
133 ContextAnchor.THREAD_CONTEXT.remove();
134 } else {
135 ctx.start();
136 }
137 }
138 return ctx;
139 }
140
141
142
143
144
145 public ContextSelector getSelector() {
146 return selector;
147 }
148
149
150
151
152
153
154 @Override
155 public void removeContext(final org.apache.logging.log4j.spi.LoggerContext context) {
156 if (context instanceof LoggerContext) {
157 selector.removeContext((LoggerContext) context);
158 }
159 }
160 }