1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
16 */
17 package org.apache.logging.log4j.core.impl;
18
19 import org.apache.logging.log4j.core.LoggerContext;
20 import org.apache.logging.log4j.core.helpers.Constants;
21 import org.apache.logging.log4j.core.helpers.Loader;
22 import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector;
23 import org.apache.logging.log4j.core.selector.ContextSelector;
24 import org.apache.logging.log4j.status.StatusLogger;
25 import org.apache.logging.log4j.spi.LoggerContextFactory;
26 import org.apache.logging.log4j.util.PropertiesUtil;
27
28 /**
29 * Factory to locate a ContextSelector and then load a LoggerContext.
30 */
31 public class Log4jContextFactory implements LoggerContextFactory {
32
33 private static final StatusLogger LOGGER = StatusLogger.getLogger();
34
35 private ContextSelector selector;
36
37 /**
38 * Constructor that initializes the ContextSelector.
39 */
40 public Log4jContextFactory() {
41 final String sel = PropertiesUtil.getProperties().getStringProperty(Constants.LOG4J_CONTEXT_SELECTOR);
42 if (sel != null) {
43 try {
44 final Class clazz = Loader.loadClass(sel);
45 if (clazz != null && ContextSelector.class.isAssignableFrom(clazz)) {
46 selector = (ContextSelector) clazz.newInstance();
47 return;
48 }
49 } catch (final Exception ex) {
50 LOGGER.error("Unable to create context " + sel, ex);
51 }
52
53 }
54 selector = new ClassLoaderContextSelector();
55 }
56
57 /**
58 * Returns the ContextSelector.
59 * @return The ContextSelector.
60 */
61 public ContextSelector getSelector() {
62 return selector;
63 }
64
65 /**
66 * Load the LoggerContext using the ContextSelector.
67 * @param fqcn The fully qualified class name of the caller.
68 * @param loader The ClassLoader to use or null.
69 * @param currentContext If true returns the current Context, if false returns the Context appropriate
70 * for the caller if a more appropriate Context can be determined.
71 * @return The LoggerContext.
72 */
73 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext) {
74 final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext);
75 if (ctx.getStatus() == LoggerContext.Status.INITIALIZED) {
76 ctx.start();
77 }
78 return ctx;
79 }
80 }