/*
 * Copyright 2012-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.logging;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.util.ClassUtils;

/**
 * Common abstraction over logging systems.
 *
 * @author Phillip Webb
 * @author Dave Syer
 */
public abstract class LoggingSystem {

	private static final Map<String, String> SYSTEMS;
	static {
		Map<String, String> systems = new LinkedHashMap<String, String>();
		systems.put("ch.qos.logback.core.Appender",
				"org.springframework.boot.logging.logback.LogbackLoggingSystem");
		systems.put("org.apache.log4j.PropertyConfigurator",
				"org.springframework.boot.logging.log4j.Log4JLoggingSystem");
		systems.put("org.apache.logging.log4j.LogManager",
				"org.springframework.boot.logging.log4j2.Log4J2LoggingSystem");
		systems.put("java.util.logging.LogManager",
				"org.springframework.boot.logging.java.JavaLoggingSystem");
		SYSTEMS = Collections.unmodifiableMap(systems);
	}

	/**
	 * Reset the logging system to be limit output. This method may be called before
	 * {@link #initialize(String, LogFile)} to reduce logging noise until the system has
	 * been fully Initialized.
	 */
	public abstract void beforeInitialize();

	/**
	 * Fully initialize the logging system.
	 * @param configLocation a log configuration location or {@code null} if default
	 * initialization is required
	 * @param logFile the log output file that should be written or {@code null} for
	 * console only output
	 */
	public abstract void initialize(String configLocation, LogFile logFile);

	/**
	 * Sets the logging level for a given logger.
	 * @param loggerName the name of the logger to set
	 * @param level the log level
	 */
	public abstract void setLogLevel(String loggerName, LogLevel level);

	/**
	 * Detect and return the logging system in use. Supports Logback, Log4J, Log4J2 and
	 * Java Logging.
	 * @return The logging system
	 */
	public static LoggingSystem get(ClassLoader classLoader) {
		for (Map.Entry<String, String> entry : SYSTEMS.entrySet()) {
			if (ClassUtils.isPresent(entry.getKey(), classLoader)) {
				try {
					Class<?> systemClass = ClassUtils.forName(entry.getValue(),
							classLoader);
					return (LoggingSystem) systemClass.getConstructor(ClassLoader.class)
							.newInstance(classLoader);
				}
				catch (Exception ex) {
					throw new IllegalStateException(ex);
				}
			}
		}
		throw new IllegalStateException("No suitable logging system located");
	}

}

@@ -786,16 +786,61 @@
 ssUtils;
+%0Aimport org.springframework.util.StringUtils;
 %0A%0A/**%0A *
@@ -972,16 +972,189 @@
 stem %7B%0A%0A
+%09/**%0A%09 * A System property that can be used to indicate the %7B@link LoggingSystem%7D to use.%0A%09 */%0A%09public static final String SYSTEM_PROPERTY = LoggingSystem.class.getName();%0A%0A
 %09private
@@ -1424,30 +1424,28 @@
 .log
-4j.PropertyConfigurato
+ging.log4j.LogManage
 r%22,%0A
@@ -1487,22 +1487,24 @@
 ng.log4j
+2
 .Log4J
+2
 LoggingS
@@ -1541,36 +1541,38 @@
 ache.log
-ging.log4j.LogManage
+4j.PropertyConfigurato
 r%22,%0A%09%09%09%09
@@ -1606,32 +1606,30 @@
 ogging.log4j
-2
 .Log4J
-2
 LoggingSyste
@@ -2812,141 +2812,513 @@
 %7B%0A%09%09
-for (Map.Entry%3CString, String%3E entry : SYSTEMS.entrySet()) %7B%0A%09%09%09if (ClassUtils.isPresent(entry.getKey(), classLoader)) %7B%0A%09%09%09%09try %7B%0A%09%09
+String loggingSystem = System.getProperty(SYSTEM_PROPERTY);%0A%09%09if (StringUtils.hasLength(loggingSystem)) %7B%0A%09%09%09return get(classLoader, loggingSystem);%0A%09%09%7D%0A%09%09for (Map.Entry%3CString, String%3E entry : SYSTEMS.entrySet()) %7B%0A%09%09%09if (ClassUtils.isPresent(entry.getKey(), classLoader)) %7B%0A%09%09%09%09return get(classLoader, entry.getValue());%0A%09%09%09%7D%0A%09%09%7D%0A%09%09throw new IllegalStateException(%22No suitable logging system located%22);%0A%09%7D%0A%0A%09private static LoggingSystem get(ClassLoader classLoader, String loggingSystemClass) %7B%0A%09%09try %7B%0A
 %09%09%09C
@@ -3362,33 +3362,28 @@
 ame(
-entry.getValue(),%0A%09%09%09%09%09%09%09
+loggingSystemClass, 
 clas
@@ -3395,18 +3395,16 @@
 er);%0A%09%09%09
-%09
-%09
 return (
@@ -3469,18 +3469,16 @@
 s)%0A%09%09%09%09%09
-%09%09
 .newInst
@@ -3496,24 +3496,20 @@
 oader);%0A
-%09%09
 %09%09%7D%0A
-%09%09
 %09%09catch 
@@ -3528,18 +3528,16 @@
 x) %7B%0A%09%09%09
-%09
-%09
 throw ne
@@ -3571,93 +3571,9 @@
 ;%0A%09%09
-%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09throw new IllegalStateException(%22No suitable logging system located%22);
+%7D
 %0A%09%7D%0A

Move Log4J2LoggingSystem above Log4JLoggingSystem