001/*
002 * Copyright 2012-2017 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.diagnostics;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021
022import org.springframework.util.StringUtils;
023
024/**
025 * {@link FailureAnalysisReporter} that logs the failure analysis.
026 *
027 * @author Andy Wilkinson
028 * @since 1.4.0
029 */
030public final class LoggingFailureAnalysisReporter implements FailureAnalysisReporter {
031
032        private static final Log logger = LogFactory
033                        .getLog(LoggingFailureAnalysisReporter.class);
034
035        @Override
036        public void report(FailureAnalysis failureAnalysis) {
037                if (logger.isDebugEnabled()) {
038                        logger.debug("Application failed to start due to an exception",
039                                        failureAnalysis.getCause());
040                }
041                if (logger.isErrorEnabled()) {
042                        logger.error(buildMessage(failureAnalysis));
043                }
044        }
045
046        private String buildMessage(FailureAnalysis failureAnalysis) {
047                StringBuilder builder = new StringBuilder();
048                builder.append(String.format("%n%n"));
049                builder.append(String.format("***************************%n"));
050                builder.append(String.format("APPLICATION FAILED TO START%n"));
051                builder.append(String.format("***************************%n%n"));
052                builder.append(String.format("Description:%n%n"));
053                builder.append(String.format("%s%n", failureAnalysis.getDescription()));
054                if (StringUtils.hasText(failureAnalysis.getAction())) {
055                        builder.append(String.format("%nAction:%n%n"));
056                        builder.append(String.format("%s%n", failureAnalysis.getAction()));
057                }
058                return builder.toString();
059        }
060
061}