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 org.apache.logging.log4j.LogManager;
20  import org.apache.logging.log4j.core.LoggerContext;
21  import java.net.URI;
22  import java.net.URISyntaxException;
23  
24  /**
25   * Initializes and configure the Logging system.
26   */
27  public final class Configurator {
28  
29      private Configurator() {
30      }
31  
32      /**
33       * Initializes the Logging Context.
34       * @param name The Context name.
35       * @param loader The ClassLoader for the Context (or null).
36       * @param configLocation The configuration for the logging context.
37       * @return The LoggerContext.
38       */
39      public static LoggerContext initialize(String name, ClassLoader loader, String configLocation) {
40  
41          try {
42              URI uri = configLocation == null ? null : new URI(configLocation);
43              return initialize(name, loader, uri);
44          } catch (URISyntaxException ex) {
45              ex.printStackTrace();
46          }
47          return null;
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       * @return The LoggerContext.
56       */
57      public static LoggerContext initialize(String name, ClassLoader loader, URI configLocation) {
58  
59          try {
60              LoggerContext ctx = (LoggerContext) LogManager.getContext(loader, false);
61              Configuration config = ConfigurationFactory.getInstance().getConfiguration(name, configLocation);
62              ctx.setConfiguration(config);
63              return ctx;
64          } catch (Exception ex) {
65              ex.printStackTrace();
66          }
67          return null;
68      }
69  
70      /**
71       * Initializes the Logging Context.
72       * @param loader The ClassLoader for the Context (or null).
73       * @param source The InputSource for the configuration.
74       * @return The LoggerContext.
75       */
76      public static LoggerContext initialize(ClassLoader loader, ConfigurationFactory.ConfigurationSource source) {
77  
78          try {
79              LoggerContext ctx = (LoggerContext) LogManager.getContext(loader, false);
80              Configuration config = ConfigurationFactory.getInstance().getConfiguration(source);
81              ctx.setConfiguration(config);
82              return ctx;
83          } catch (Exception ex) {
84              ex.printStackTrace();
85          }
86          return null;
87      }
88  
89      /**
90       * Shuts down the given logging context.
91       * @param ctx the logging context to shut down, may be null.
92       */
93      public static void shutdown(LoggerContext ctx) {
94          if (ctx != null) {
95              ctx.setConfiguration(new DefaultConfiguration());
96          }
97      }
98  
99  }