View Javadoc

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