001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.logging.log4j2;
018
019import org.apache.logging.log4j.core.LoggerContext;
020import org.apache.logging.log4j.core.config.Configuration;
021import org.apache.logging.log4j.core.config.ConfigurationFactory;
022import org.apache.logging.log4j.core.config.ConfigurationSource;
023import org.apache.logging.log4j.core.config.DefaultConfiguration;
024import org.apache.logging.log4j.core.config.Order;
025import org.apache.logging.log4j.core.config.plugins.Plugin;
026
027import org.springframework.boot.logging.LoggingSystem;
028
029/**
030 * Spring Boot {@link ConfigurationFactory} that customizes Log4J2's default configuration
031 * to:
032 *
033 * <ol>
034 * <li>Prevent logger warnings from being printed when the application first starts.
035 * <li>Disable its shutdown hook
036 * </ol>
037 *
038 * This factory is ordered last and is triggered by a {@code log4j2.springboot} classpath
039 * resource (which is bundled in this jar). If the {@link Log4J2LoggingSystem} is active,
040 * a custom {@link DefaultConfiguration} is returned with the expectation that the system
041 * will later re-initialize Log4J2 with the correct configuration file.
042 *
043 * @author Phillip Webb
044 * @since 1.5.0
045 */
046@Plugin(name = "SpringBootConfigurationFactory", category = ConfigurationFactory.CATEGORY)
047@Order(0)
048public class SpringBootConfigurationFactory extends ConfigurationFactory {
049
050        private static final String[] TYPES = { ".springboot" };
051
052        @Override
053        protected String[] getSupportedTypes() {
054                return TYPES;
055        }
056
057        @Override
058        public Configuration getConfiguration(LoggerContext loggerContext,
059                        ConfigurationSource source) {
060                if (source != null && source != ConfigurationSource.NULL_SOURCE
061                                && LoggingSystem.get(loggerContext.getClass().getClassLoader()) != null) {
062                        return new SpringBootConfiguration();
063                }
064                return null;
065        }
066
067        private static final class SpringBootConfiguration extends DefaultConfiguration {
068
069                private SpringBootConfiguration() {
070                        this.isShutdownHookEnabled = false;
071                }
072
073        }
074
075}