001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 package org.apache.logging.log4j.core.config; 018 019 import org.apache.logging.log4j.LogManager; 020 import org.apache.logging.log4j.core.LoggerContext; 021 import org.apache.logging.log4j.status.StatusLogger; 022 023 import java.net.URI; 024 import java.net.URISyntaxException; 025 026 /** 027 * Initializes and configure the Logging system. 028 */ 029 public final class Configurator { 030 031 private static final StatusLogger LOGGER = StatusLogger.getLogger(); 032 033 private Configurator() { 034 } 035 036 /** 037 * Initializes the Logging Context. 038 * @param name The Context name. 039 * @param loader The ClassLoader for the Context (or null). 040 * @param configLocation The configuration for the logging context. 041 * @return The LoggerContext. 042 */ 043 public static LoggerContext initialize(final String name, final ClassLoader loader, final String configLocation) { 044 045 try { 046 final URI uri = configLocation == null ? null : new URI(configLocation); 047 return initialize(name, loader, uri); 048 } catch (final URISyntaxException ex) { 049 ex.printStackTrace(); 050 } 051 return null; 052 } 053 054 /** 055 * Initializes the Logging Context. 056 * @param name The Context name. 057 * @param loader The ClassLoader for the Context (or null). 058 * @param configLocation The configuration for the logging context. 059 * @return The LoggerContext. 060 */ 061 public static LoggerContext initialize(final String name, final ClassLoader loader, final URI configLocation) { 062 063 try { 064 org.apache.logging.log4j.spi.LoggerContext context = LogManager.getContext(loader, false, configLocation); 065 if (context instanceof LoggerContext) { 066 final LoggerContext ctx = (LoggerContext) context; 067 final Configuration config = ConfigurationFactory.getInstance().getConfiguration(name, configLocation); 068 ctx.start(config); 069 return ctx; 070 } else { 071 LOGGER.error("LogManager returned an instance of {} which does not implement {}. Unable to initialize Log4j", 072 context.getClass().getName(), LoggerContext.class.getName()); 073 } 074 } catch (final Exception ex) { 075 ex.printStackTrace(); 076 } 077 return null; 078 } 079 080 /** 081 * Initializes the Logging Context. 082 * @param loader The ClassLoader for the Context (or null). 083 * @param source The InputSource for the configuration. 084 * @return The LoggerContext. 085 */ 086 public static LoggerContext initialize(final ClassLoader loader, 087 final ConfigurationFactory.ConfigurationSource source) { 088 089 try { 090 URI configLocation = null; 091 try { 092 configLocation = source.getLocation() == null ? null : new URI(source.getLocation()); 093 } catch (Exception ex) { 094 // Invalid source location. 095 } 096 org.apache.logging.log4j.spi.LoggerContext context = LogManager.getContext(loader, false, configLocation); 097 if (context instanceof LoggerContext) { 098 final LoggerContext ctx = (LoggerContext) context; 099 final Configuration config = ConfigurationFactory.getInstance().getConfiguration(source); 100 ctx.start(config); 101 return ctx; 102 } else { 103 LOGGER.error("LogManager returned an instance of {} which does not implement {}. Unable to initialize Log4j", 104 context.getClass().getName(), LoggerContext.class.getName()); 105 } 106 } catch (final Exception ex) { 107 ex.printStackTrace(); 108 } 109 return null; 110 } 111 112 /** 113 * Shuts down the given logging context. 114 * @param ctx the logging context to shut down, may be null. 115 */ 116 public static void shutdown(final LoggerContext ctx) { 117 if (ctx != null) { 118 ctx.stop(); 119 } 120 } 121 }