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 */ 017package org.apache.logging.log4j.core.config; 018 019import java.net.URI; 020import java.net.URISyntaxException; 021 022import org.apache.logging.log4j.LogManager; 023import org.apache.logging.log4j.core.LoggerContext; 024import org.apache.logging.log4j.core.helpers.FileUtils; 025import org.apache.logging.log4j.core.impl.Log4jContextFactory; 026import org.apache.logging.log4j.spi.LoggerContextFactory; 027import org.apache.logging.log4j.status.StatusLogger; 028 029/** 030 * Initializes and configure the Logging system. 031 */ 032public final class Configurator { 033 034 protected static final StatusLogger LOGGER = StatusLogger.getLogger(); 035 036 private Configurator() { 037 } 038 039 040 /** 041 * Initializes the Logging Context. 042 * @param name The Context name. 043 * @param loader The ClassLoader for the Context (or null). 044 * @param configLocation The configuration for the logging context. 045 * @return The LoggerContext. 046 */ 047 public static LoggerContext initialize(final String name, final ClassLoader loader, final String configLocation) { 048 return initialize(name, loader, configLocation, null); 049 050 } 051 052 /** 053 * Initializes the Logging Context. 054 * @param name The Context name. 055 * @param loader The ClassLoader for the Context (or null). 056 * @param configLocation The configuration for the logging context. 057 * @param externalContext The external context to be attached to the LoggerContext 058 * @return The LoggerContext. 059 */ 060 public static LoggerContext initialize(final String name, final ClassLoader loader, final String configLocation, 061 final Object externalContext) { 062 063 try { 064 final URI uri = configLocation == null ? null : FileUtils.getCorrectedFilePathUri(configLocation); 065 return initialize(name, loader, uri, externalContext); 066 } catch (final URISyntaxException ex) { 067 ex.printStackTrace(); 068 } 069 return null; 070 } 071 072 /** 073 * Initializes the Logging Context. 074 * @param name The Context name. 075 * @param configLocation The configuration for the logging context. 076 * @return The LoggerContext. 077 */ 078 public static LoggerContext initialize(final String name, final String configLocation) { 079 return initialize(name, null, configLocation); 080 } 081 082 /** 083 * Initializes the Logging Context. 084 * @param name The Context name. 085 * @param loader The ClassLoader for the Context (or null). 086 * @param configLocation The configuration for the logging context. 087 * @return The LoggerContext. 088 */ 089 public static LoggerContext initialize(final String name, final ClassLoader loader, final URI configLocation) { 090 return initialize(name, loader, configLocation, null); 091 } 092 093 /** 094 * Initializes the Logging Context. 095 * @param name The Context name. 096 * @param loader The ClassLoader for the Context (or null). 097 * @param configLocation The configuration for the logging context. 098 * @param externalContext The external context to be attached to the LoggerContext 099 * @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}