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.config;
18
19 import java.net.URI;
20 import java.net.URISyntaxException;
21
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
24 import org.apache.logging.log4j.core.LoggerContext;
25 import org.apache.logging.log4j.core.impl.Log4jContextFactory;
26 import org.apache.logging.log4j.core.util.FileUtils;
27 import org.apache.logging.log4j.spi.LoggerContextFactory;
28 import org.apache.logging.log4j.status.StatusLogger;
29
30 /**
31 * Initializes and configure the Logging system. This class provides several ways to construct a LoggerContext using
32 * the location of a configuration file, a context name, and various optional parameters.
33 */
34 public final class Configurator {
35
36 private static final Logger LOGGER = StatusLogger.getLogger();
37
38 private static final String FQCN = Configurator.class.getName();
39
40 private Configurator() {
41 }
42
43 /**
44 * Initializes the Logging Context.
45 * @param name The Context name.
46 * @param loader The ClassLoader for the Context (or null).
47 * @param configLocation The configuration for the logging context.
48 * @return The LoggerContext.
49 */
50 public static LoggerContext initialize(final String name, final ClassLoader loader, final String configLocation) {
51 return initialize(name, loader, configLocation, null);
52
53 }
54
55 /**
56 * Initializes the Logging Context.
57 * @param name The Context name.
58 * @param loader The ClassLoader for the Context (or null).
59 * @param configLocation The configuration for the logging context.
60 * @param externalContext The external context to be attached to the LoggerContext
61 * @return The LoggerContext.
62 */
63 public static LoggerContext initialize(final String name, final ClassLoader loader, final String configLocation,
64 final Object externalContext) {
65
66 try {
67 final URI uri = configLocation == null ? null : FileUtils.getCorrectedFilePathUri(configLocation);
68 return initialize(name, loader, uri, externalContext);
69 } catch (final URISyntaxException ex) {
70 LOGGER.error("There was a problem parsing the configuration location [{}].", configLocation, ex);
71 }
72 return null;
73 }
74
75 /**
76 * Initializes the Logging Context.
77 * @param name The Context name.
78 * @param configLocation The configuration for the logging context.
79 * @return The LoggerContext.
80 */
81 public static LoggerContext initialize(final String name, final String configLocation) {
82 return initialize(name, null, configLocation);
83 }
84
85 /**
86 * Initializes the Logging Context.
87 * @param name The Context name.
88 * @param loader The ClassLoader for the Context (or null).
89 * @param configLocation The configuration for the logging context.
90 * @return The LoggerContext.
91 */
92 public static LoggerContext initialize(final String name, final ClassLoader loader, final URI configLocation) {
93 return initialize(name, loader, configLocation, null);
94 }
95
96 /**
97 * Initializes the Logging Context.
98 * @param name The Context name.
99 * @param loader The ClassLoader for the Context (or null).
100 * @param configLocation The configuration for the logging context.
101 * @param externalContext The external context to be attached to the LoggerContext
102 * @return The LoggerContext.
103 */
104 public static LoggerContext initialize(final String name, final ClassLoader loader, final URI configLocation,
105 final Object externalContext) {
106
107 try {
108 final Log4jContextFactory factory = getFactory();
109 return factory == null ? null :
110 factory.getContext(FQCN, loader, externalContext, false, configLocation, name);
111 } catch (final Exception ex) {
112 LOGGER.error("There was a problem initializing the LoggerContext [{}] using configuration at [{}].",
113 name, configLocation, ex);
114 }
115 return null;
116 }
117
118 /**
119 * Initializes the Logging Context.
120 * @param loader The ClassLoader for the Context (or null).
121 * @param source The InputSource for the configuration.
122 * @return The LoggerContext.
123 */
124 public static LoggerContext initialize(final ClassLoader loader,
125 final ConfigurationSource source) {
126 return initialize(loader, source, null);
127 }
128
129 /**
130 * Initializes the Logging Context.
131 * @param loader The ClassLoader for the Context (or null).
132 * @param source The InputSource for the configuration.
133 * @param externalContext The external context to be attached to the LoggerContext.
134 * @return The LoggerContext.
135 */
136
137 public static LoggerContext initialize(final ClassLoader loader,
138 final ConfigurationSource source,
139 final Object externalContext)
140 {
141
142 try {
143 final Log4jContextFactory factory = getFactory();
144 return factory == null ? null :
145 factory.getContext(FQCN, loader, externalContext, false, source);
146 } catch (final Exception ex) {
147 LOGGER.error("There was a problem obtaining a LoggerContext using the configuration source [{}]", source, ex);
148 }
149 return null;
150 }
151
152 private static Log4jContextFactory getFactory() {
153 final LoggerContextFactory factory = LogManager.getFactory();
154 if (factory instanceof Log4jContextFactory) {
155 return (Log4jContextFactory) factory;
156 } else if (factory != null) {
157 LOGGER.error("LogManager returned an instance of {} which does not implement {}. Unable to initialize Log4j.",
158 factory.getClass().getName(), Log4jContextFactory.class.getName());
159 return null;
160 } else {
161 LOGGER.fatal("LogManager did not return a LoggerContextFactory. This indicates something has gone terribly wrong!");
162 return null;
163 }
164 }
165
166 /**
167 * Shuts down the given logging context.
168 * @param ctx the logging context to shut down, may be null.
169 */
170 public static void shutdown(final LoggerContext ctx) {
171 if (ctx != null) {
172 ctx.stop();
173 }
174 }
175 }