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